This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

@@ -5,8 +5,10 @@
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
@@ -20,9 +22,12 @@

public class MainActivity extends BaseActivity<RedditsPresenter, RedditsView> implements RedditsView {

private final static String TAG = MainActivity.class.getSimpleName();

private SwipeRefreshLayout swipeContainer;
private RecyclerView redditListView;

private RedditsAdapter adapter;
private Snackbar snackbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -31,21 +36,22 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

redditListView = (RecyclerView) findViewById(R.id.reddit_list);
snackbar = Snackbar.make(findViewById(android.R.id.content), "Loading next page...", Snackbar.LENGTH_LONG);
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
//
// }
// });

swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
Log.i(TAG, "onRefresh");
presenter.requestUpdateReddits();
}
});
@@ -55,13 +61,80 @@ public void onRefresh() {
android.R.color.holo_orange_light,
android.R.color.holo_red_light);

redditListView = (RecyclerView) findViewById(R.id.reddit_list);
redditListView.setLayoutManager(new LinearLayoutManager(this));

presenter.requestUpdateReddits();

}

@Override
public BasePresenter createPresenter() {
return new RedditsPresenter();
}

//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------

@Override
public void updateReddits(List<RedditChild> reddits) {
Log.d(TAG, "updateReddits:" + reddits.size());
if(adapter == null) {
initAdapter(reddits);
} else {
adapter.updateItems(reddits);
}

}

@Override
public void addReddits(List<RedditChild> reddits) {
Log.d(TAG, "addReddits:" + reddits.size());
adapter.addItems(reddits);
adapter.setLoaded();
}

@Override
public void showToastMessage(String message) {

}

@Override
public void showRefreshingProgress() {
swipeContainer.setRefreshing(true);
}

@Override
public void hideRefreshingProgress() {
swipeContainer.setRefreshing(false);
}

@Override
public void showLoadMoreProgress() {
Log.d(TAG, "showLoadMoreProgress");
// if(snackbar.isShown()) snackbar.dismiss();
snackbar.show();
}

@Override
public void hideLoadMoreProgress() {
if(snackbar.isShown()) snackbar.dismiss();
}


//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
private void initAdapter(List<RedditChild> reddits) {
adapter = new RedditsAdapter(this, reddits, new RedditsAdapter.OnLoadMoreListener() {
@Override
public void onLoadMore() {
Log.d(TAG, "onLoadMore");
presenter.requestAddRedditsPage();
}
}, redditListView);
redditListView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
@@ -84,22 +157,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}

//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------

@Override
public void updateReddits(List<RedditChild> reddits) {

}

@Override
public void addReddits(List<RedditChild> reddits) {

}

@Override
public void showMessage(String message) {

}

}
@@ -1,41 +1,192 @@
package com.sergey.redditreader.ui;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.TextView;

import com.sergey.redditreader.R;
import com.sergey.redditreader.Util;
import com.sergey.redditreader.model.RedditChild;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
* Created by user on 23.09.2017.
*/

public class RedditsAdapter extends RecyclerView.Adapter<RedditsAdapter.ViewHolder> {
private final static String TAG = RedditsAdapter.class.getSimpleName();

private final static int VIEW_TYPE_ITEM = 0;
private final static int VIEW_TYPE_LOADING = 1;

private Context context;
private List<RedditChild> reddits = new ArrayList<>();

private OnLoadMoreListener loadMoreListener;
private RecyclerView recyclerView;

public RedditsAdapter(Context c) {
private boolean isLoading;
private int lastVisibleItem, totalItemCount;
private int visibleThreshold = 5;


public RedditsAdapter(Context c, List<RedditChild> items, OnLoadMoreListener listener, RecyclerView view) {
context = c;
reddits.addAll(items);
loadMoreListener = listener;
recyclerView = view;
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// Log.d(TAG, "onScrolled");
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if (loadMoreListener != null) {
Log.d(TAG, "onScrolled: loadmore");
loadMoreListener.onLoadMore();
}
isLoading = true;
}
}
});
}

public void addItems(List<RedditChild> items) {
// Log.i(TAG, "addItems:" + items.size() + " before:" + reddits.size());
int insertedPositionStart = reddits.size();
reddits.addAll(items);
// Log.i(TAG, "addItems:from=" + insertedPositionStart + " after:" + reddits.size());
notifyItemRangeInserted(insertedPositionStart, items.size());
}

public void updateItems(List<RedditChild> items) {
reddits.clear();
notifyDataSetChanged();
reddits.addAll(items);
notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.reddit_item, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
RedditChild item = reddits.get(position);

holder.title.setText(item.data.title);
holder.date.setText(Util.getTimeAgoString(item.data.date));
holder.author.setText(Util.getAuthorString(item.data.author));

holder.comments.setText(Util.getCommentsString(item.data.num_comments));

setImage(holder.image, item.data.thumbnail, item.data.thumbnailWidth, item.data.thumbnailHeight);

// if(position == getItemCount() - 1) {
// if(loadMoreListener != null) loadMoreListener.onLoadMore();
// }

}

private void setImage(ImageView imageView, String url, int w, int h) {
Log.i(TAG, "setImage url:" + url);
// imageView.setImageResource(R.drawable.default_reddit);
if(!TextUtils.isEmpty(url) && !url.equals("self") && !url.equals("default")) {
// imageView.setMinimumWidth(w);
// imageView.setMinimumHeight(h);
Log.i(TAG, "setImage picasso:" + url);
Picasso.with(context).load(url)
.fit().centerCrop().into(imageView, new Callback() {
@Override
public void onSuccess() {
Log.i(TAG, "onSuccess url");
}

@Override
public void onError() {
Log.i(TAG, "onError url");
}
});
} else {
Log.i(TAG, "setImage default");
Picasso.with(context).load(R.drawable.default_reddit).into(imageView, new Callback() {
@Override
public void onSuccess() {
Log.i(TAG, "onSuccess default");
}

@Override
public void onError() {
Log.i(TAG, "onError default");
}
});
// imageView.setImageResource(R.drawable.default_reddit);
// imageView.setImageDrawable(context.getResources().getDrawable(R.drawable.default_reddit));
// Drawable drawable = context.getResources().getDrawable(R.drawable.default_reddit);
// imageView.setBackgroundDrawable(drawable);
}
}

@Override
public int getItemCount() {
return 0;
return reddits.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
@Override public int getItemViewType(int position) {
return super.getItemViewType(position);
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView title;
public TextView date;
public TextView author;
public TextView comments;
public ImageView image;

public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
date = (TextView) itemView.findViewById(R.id.date);
author = (TextView) itemView.findViewById(R.id.author);
comments = (TextView) itemView.findViewById(R.id.comments);
image = (ImageView) itemView.findViewById(R.id.image);
itemView.setOnClickListener(this);
}

@Override
public void onClick(View view) {

}
}

public void setLoaded() {
isLoading = false;
}

public interface OnLoadMoreListener {
void onLoadMore();
}
}
@@ -12,5 +12,9 @@
public interface RedditsView extends BaseView {
void updateReddits(List<RedditChild> reddits);
void addReddits(List<RedditChild> reddits);
void showMessage(String message);
void showToastMessage(String message);
void showRefreshingProgress();
void hideRefreshingProgress();
void showLoadMoreProgress();
void hideLoadMoreProgress();
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -22,12 +22,13 @@

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

<!--<android.support.design.widget.FloatingActionButton-->
<!--android:id="@+id/fab"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom|end"-->
<!--android:layout_margin="@dimen/fab_margin"-->
<!--app:srcCompat="@android:drawable/ic_dialog_email" />-->

</android.support.design.widget.CoordinatorLayout>
@@ -7,7 +7,9 @@
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.sergey.redditreader.ui.MainActivity">
tools:context="com.sergey.redditreader.ui.MainActivity"
android:padding="5dp"
>

<!--
<TextView
@@ -23,10 +25,13 @@
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_container"
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
>

<android.support.v7.widget.RecyclerView
android:id="@+id/reddit_list"
@@ -37,4 +42,17 @@

</android.support.v4.widget.SwipeRefreshLayout>

<!--<android.support.v7.widget.RecyclerView-->
<!--android:id="@+id/reddit_list"-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="0dp"-->
<!--android:scrollbars="vertical"-->
<!--android:background="@android:color/holo_blue_bright"-->
<!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->
<!--app:layout_constraintBottom_toBottomOf="parent"-->
<!--app:layout_constraintLeft_toLeftOf="parent"-->
<!--app:layout_constraintRight_toRightOf="parent"-->
<!--app:layout_constraintTop_toTopOf="parent"-->
<!--/>-->

</android.support.constraint.ConstraintLayout>
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<LinearLayout
android:id="@+id/top_content"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RelativeLayout
android:id="@+id/image_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
>
<ImageView
android:id="@+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerInParent="true"
/>

</RelativeLayout>

<RelativeLayout
android:id="@+id/content_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/image_container"
android:layout_marginLeft="10dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Title"
android:layout_alignParentTop="true"
/>
<LinearLayout
android:id="@+id/date_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/title"
>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="date"
android:textColor="@android:color/darker_gray"
/>
<TextView
android:id="@+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_marginLeft="10dp"
android:text="author"
android:textColor="@android:color/darker_gray"
/>
</LinearLayout>

</RelativeLayout>

</RelativeLayout>

</LinearLayout>

<TextView
android:id="@+id/comments"
android:layout_below="@+id/top_content"
android:layout_alignLeft="@+id/top_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="comments"
android:layout_marginTop="10dp"
android:textColor="@android:color/darker_gray"
/>

<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_below="@+id/comments"
android:layout_marginTop="5dp"
android:background="#7B7B7B"/>

</RelativeLayout>