aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java15
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java29
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java5
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java14
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java52
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java20
9 files changed, 112 insertions, 37 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java
index 39d26a5d..a6cbf578 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostPaginationScrollListener.java
@@ -27,7 +27,6 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
private PostRecyclerViewAdapter mAdapter;
private ArrayList<PostData> mPostData;
private PaginationSynchronizer mPaginationSynchronizer;
- private PaginationRetryNotifier mPaginationRetryNotifier;
private LastItemSynchronizer mLastItemSynchronizer;
private String mSubredditName;
@@ -54,17 +53,17 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
this.loadSuccess = loadSuccess;
this.locale = locale;
- mPaginationRetryNotifier = new PaginationRetryNotifier() {
+ PaginationRetryNotifier paginationRetryNotifier = new PaginationRetryNotifier() {
@Override
public void retry() {
- if(isBestPost) {
+ if (isBestPost) {
fetchBestPost(1);
} else {
fetchPost(subredditName, 1);
}
}
};
- mPaginationSynchronizer.setPaginationRetryNotifier(mPaginationRetryNotifier);
+ mPaginationSynchronizer.setPaginationRetryNotifier(paginationRetryNotifier);
mLastItemSynchronizer = mPaginationSynchronizer.getLastItemSynchronizer();
}
}
@@ -117,8 +116,8 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
clipboard.setPrimaryClip(clip);
ParsePost.parsePost(response.body(), mPostData, locale, new ParsePost.ParsePostListener() {
@Override
- public void onParsePostSuccess(ArrayList<PostData> bestPostData, String lastItem) {
- mAdapter.notifyDataSetChanged();
+ public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) {
+ mAdapter.notifyItemRangeInserted(mPostData.size(), postData.size());
mLastItem = lastItem;
mLastItemSynchronizer.lastItemChanged(mLastItem);
@@ -188,8 +187,8 @@ class PostPaginationScrollListener extends RecyclerView.OnScrollListener {
clipboard.setPrimaryClip(clip);
ParsePost.parsePost(response.body(), mPostData, locale, new ParsePost.ParsePostListener() {
@Override
- public void onParsePostSuccess(ArrayList<PostData> bestPostData, String lastItem) {
- mAdapter.notifyDataSetChanged();
+ public void onParsePostSuccess(ArrayList<PostData> postData, String lastItem) {
+ mAdapter.notifyItemRangeInserted(mPostData.size(), postData.size());
mLastItem = lastItem;
mLastItemSynchronizer.lastItemChanged(mLastItem);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
index f2e5d200..3292852b 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostRecyclerViewAdapter.java
@@ -136,13 +136,36 @@ class PostRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHold
((DataViewHolder) holder).subredditIconCircleImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
- Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
- intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
- mPostData.get(adapterPosition).getSubredditNamePrefixed().substring(2));
+ if(canStartActivity) {
+ canStartActivity = false;
+ Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
+ mPostData.get(adapterPosition).getSubredditNamePrefixed().substring(2));
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_VALUE_KEY,
+ mPostData.get(adapterPosition).getSubredditNamePrefixed());
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_QUERY_BY_ID_KEY, false);
+ mContext.startActivity(intent);
+ }
}
});
((DataViewHolder) holder).subredditNameTextView.setText(subredditName);
+
+ ((DataViewHolder) holder).subredditNameTextView.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ if(canStartActivity) {
+ canStartActivity = false;
+ Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY,
+ mPostData.get(adapterPosition).getSubredditNamePrefixed().substring(2));
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_VALUE_KEY,
+ mPostData.get(adapterPosition).getSubredditNamePrefixed());
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_QUERY_BY_ID_KEY, false);
+ mContext.startActivity(intent);
+ }
+ }
+ });
((DataViewHolder) holder).postTimeTextView.setText(postTime);
((DataViewHolder) holder).titleTextView.setText(title);
((DataViewHolder) holder).scoreTextView.setText(Integer.toString(mPostData.get(position).getScore()));
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
index 7b29693c..e150ab26 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java
@@ -38,4 +38,7 @@ public interface RedditAPI {
@GET("r/{subredditName}.json?raw_json=1")
Call<String> getPost(@Path("subredditName") String subredditName, @Query("after") String lastItem);
+
+ @GET("user/{username}/about.json/raw_json=1")
+ Call<String> getUserData(@Path("username") String username);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java
index 8fb1593c..2e1b0bb7 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditDao.java
@@ -15,7 +15,10 @@ public interface SubredditDao {
void deleteAllSubreddits();
@Query("SELECT * from subreddits WHERE id = :id")
- LiveData<SubredditData> getSubredditLiveData(String id);
+ LiveData<SubredditData> getSubredditLiveDataById(String id);
+
+ @Query("SELECT * from subreddits WHERE name = :namePrefixed")
+ LiveData<SubredditData> getSubredditLiveDataByNamePrefixed(String namePrefixed);
@Query("SELECT * from subreddits WHERE name = :namePrefixed")
SubredditData getSubredditData(String namePrefixed);
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java
index a104fed7..deb68806 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditRepository.java
@@ -8,10 +8,14 @@ public class SubredditRepository {
private SubredditDao mSubredditDao;
private LiveData<SubredditData> mSubredditLiveData;
- SubredditRepository(Application application, String id) {
+ SubredditRepository(Application application, String value, boolean isId) {
SubredditRoomDatabase db = SubredditRoomDatabase.getDatabase(application);
mSubredditDao = db.subredditDao();
- mSubredditLiveData = mSubredditDao.getSubredditLiveData(id);
+ if(isId) {
+ mSubredditLiveData = mSubredditDao.getSubredditLiveDataById(value);
+ } else {
+ mSubredditLiveData = mSubredditDao.getSubredditLiveDataByNamePrefixed(value);
+ }
}
LiveData<SubredditData> getSubredditLiveData() {
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java
index 6d9e20fc..ee636b43 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditViewModel.java
@@ -11,9 +11,9 @@ public class SubredditViewModel extends AndroidViewModel {
private SubredditRepository mSubredditRepository;
private LiveData<SubredditData> mSubredditLiveData;
- SubredditViewModel(Application application, String id) {
+ SubredditViewModel(Application application, String id, boolean isId) {
super(application);
- mSubredditRepository = new SubredditRepository(application, id);
+ mSubredditRepository = new SubredditRepository(application, id, isId);
mSubredditLiveData = mSubredditRepository.getSubredditLiveData();
}
@@ -30,17 +30,19 @@ public class SubredditViewModel extends AndroidViewModel {
@NonNull
private final Application mApplication;
- private final String id;
+ private final String value;
+ private final boolean isId;
- public Factory(@NonNull Application application, String id) {
+ public Factory(@NonNull Application application, String value, boolean isId) {
mApplication = application;
- this.id = id;
+ this.value = value;
+ this.isId = isId;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
//noinspection unchecked
- return (T) new SubredditViewModel(mApplication, id);
+ return (T) new SubredditViewModel(mApplication, value, isId);
}
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java
index ebc316e5..72e860be 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedSubredditRecyclerViewAdapter.java
@@ -39,7 +39,8 @@ class SubscribedSubredditRecyclerViewAdapter extends RecyclerView.Adapter<Recycl
public void onClick(View view) {
Intent intent = new Intent(mContext, ViewSubredditDetailActivity.class);
intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_NAME_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getName());
- intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_ID_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId());
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_SUBREDDIT_VALUE_KEY, mSubscribedSubredditData.get(viewHolder.getAdapterPosition()).getId());
+ intent.putExtra(ViewSubredditDetailActivity.EXTRA_QUERY_BY_ID_KEY, true);
mContext.startActivity(intent);
}
});
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
index d754b5af..af367793 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java
@@ -13,6 +13,7 @@ import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
+import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@@ -25,7 +26,8 @@ import de.hdodenhof.circleimageview.CircleImageView;
public class ViewSubredditDetailActivity extends AppCompatActivity {
static final String EXTRA_SUBREDDIT_NAME_KEY = "ESN";
- static final String EXTRA_SUBREDDIT_ID_KEY = "ESI";
+ static final String EXTRA_SUBREDDIT_VALUE_KEY = "ESV";
+ static final String EXTRA_QUERY_BY_ID_KEY = "EQBI";
private static final String FRAGMENT_OUT_STATE_KEY = "FOSK";
@@ -37,31 +39,48 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_subreddit_detail);
- Toolbar toolbar = findViewById(R.id.toolbar);
+ final Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
+ //Get status bar height
+ int statusBarHeight = 0;
+ int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
+ if (resourceId > 0) {
+ statusBarHeight = getResources().getDimensionPixelSize(resourceId);
+ }
+
+ ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) toolbar.getLayoutParams();
+ params.topMargin = statusBarHeight;
+
final String subredditName = getIntent().getExtras().getString(EXTRA_SUBREDDIT_NAME_KEY);
final String title = "r/" + subredditName;
- setTitle(title);
-
final CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar_layout_view_subreddit_detail_activity);
- AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity);
+ final AppBarLayout appBarLayout = findViewById(R.id.app_bar_layout_view_subreddit_detail_activity);
+
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
- boolean isShow = true;
+ int previousVerticalOffset = 0;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
- if (scrollRange == -1) {
+ /*collapsingToolbarLayout.setExpandedTitleColor(Color.BLACK);
+ collapsingToolbarLayout.setCollapsedTitleTextColor(Color.BLACK);*/
+ if(scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
- }
- if (scrollRange + verticalOffset == 0) {
- collapsingToolbarLayout.setTitle(title);
- isShow = true;
- } else if(isShow) {
- collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work
- isShow = false;
+ } else {
+ if(verticalOffset < previousVerticalOffset) {
+ //Scroll down
+ if(scrollRange - Math.abs(verticalOffset) <= toolbar.getHeight()) {
+ collapsingToolbarLayout.setTitle(title);
+ }
+ } else {
+ //Scroll up
+ if(scrollRange - Math.abs(verticalOffset) > toolbar.getHeight()) {
+ collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work
+ }
+ }
+ previousVerticalOffset = verticalOffset;
}
}
});
@@ -74,8 +93,9 @@ public class ViewSubredditDetailActivity extends AppCompatActivity {
final TextView descriptionTextView = findViewById(R.id.description_text_view_view_subreddit_detail_activity);
final RequestManager glide = Glide.with(ViewSubredditDetailActivity.this);
- String id = getIntent().getExtras().getString(EXTRA_SUBREDDIT_ID_KEY);
- SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), id);
+ String value = getIntent().getExtras().getString(EXTRA_SUBREDDIT_VALUE_KEY);
+ boolean queryById = getIntent().getExtras().getBoolean(EXTRA_QUERY_BY_ID_KEY);
+ SubredditViewModel.Factory factory = new SubredditViewModel.Factory(getApplication(), value, queryById);
mSubredditViewModel = ViewModelProviders.of(this, factory).get(SubredditViewModel.class);
mSubredditViewModel.getSubredditLiveData().observe(this, new Observer<SubredditData>() {
@Override
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
new file mode 100644
index 00000000..0ec59030
--- /dev/null
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java
@@ -0,0 +1,20 @@
+package ml.docilealligator.infinityforreddit;
+
+import android.os.Bundle;
+import android.support.v7.app.AppCompatActivity;
+import android.support.v7.widget.Toolbar;
+
+public class ViewUserDetailActivity extends AppCompatActivity {
+
+ static final String EXTRA_USER_NAME_KEY = "EUNK";
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_view_user_detail);
+ Toolbar toolbar = findViewById(R.id.toolbar);
+ setSupportActionBar(toolbar);
+
+
+ }
+}