Commit 55ae8355 authored by Varun Patil's avatar Varun Patil

Scroll to news/blog article on push notification open (fix #265)

parent c2e0bd37
......@@ -339,7 +339,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
openEventFragment(id);
return;
case DATA_TYPE_NEWS:
updateFragment(new NewsFragment());
updateFragment((new NewsFragment()).withId(id));
return;
}
Log.e("NOTIFICATIONS", "Server sent invalid notification?");
......@@ -355,9 +355,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
switch (type) {
case DATA_TYPE_PT:
if (extra.contains("/trainingblog")) {
openTrainingBlog();
openTrainingBlog(id);
} else {
openPlacementBlog();
openPlacementBlog(id);
}
return;
}
......@@ -586,8 +586,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
* Open placement blog fragment
*/
private void openPlacementBlog() {
openPlacementBlog(null);
}
private void openPlacementBlog(String id) {
if (session.isLoggedIn()) {
PlacementBlogFragment placementBlogFragment = new PlacementBlogFragment();
if (id != null) placementBlogFragment.withId(id);
updateFragment(placementBlogFragment);
} else {
Toast.makeText(this, Constants.LOGIN_MESSAGE, Toast.LENGTH_LONG).show();
......@@ -595,8 +600,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
}
private void openTrainingBlog() {
openTrainingBlog(null);
}
private void openTrainingBlog(String id) {
if (session.isLoggedIn()) {
TrainingBlogFragment trainingBlogFragment = new TrainingBlogFragment();
if (id != null) trainingBlogFragment.withId(id);
updateFragment(trainingBlogFragment);
} else {
Toast.makeText(this, Constants.LOGIN_MESSAGE, Toast.LENGTH_LONG).show();
......
......@@ -41,6 +41,8 @@ public class NewsArticle implements Clickable {
this.body = body;
}
public String getId() { return articleID; }
public String getArticleID() {
return articleID;
}
......
......@@ -37,6 +37,8 @@ public class PlacementBlogPost implements Clickable {
this.published = published;
}
public String getId() { return postID; }
public String getPostID() {
return postID;
}
......
......@@ -37,6 +37,8 @@ public class TrainingBlogPost implements Clickable {
this.published = published;
}
public String getId() { return postID; }
public String getPostID() {
return postID;
}
......
package app.insti.fragment;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSmoothScroller;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
......@@ -42,6 +41,12 @@ public abstract class RecyclerViewFragment<T extends Clickable, S extends Recycl
private S adapter = null;
boolean loading = false;
private boolean allLoaded = false;
private String initId = null;
public RecyclerViewFragment<T, S> withId(String id) {
initId = id;
return this;
}
/** Update the data clearing existing */
protected void updateData() {
......@@ -89,6 +94,11 @@ public abstract class RecyclerViewFragment<T extends Clickable, S extends Recycl
if (adapter == null || recyclerView.getAdapter() != adapter) {
initAdapter(result);
/* Scroll to current post */
if (initId != null) {
scrollToPosition(getPosition(result, initId));
}
} else {
adapter.setPosts(result);
adapter.notifyDataSetChanged();
......@@ -97,6 +107,29 @@ public abstract class RecyclerViewFragment<T extends Clickable, S extends Recycl
getActivity().findViewById(R.id.loadingPanel).setVisibility(GONE);
}
/** Set position of id in list of clickables */
int getPosition(List<T> result, String id) {
for (int i = 0; i < result.size(); i++) {
if (result.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}
/** Scroll the recyclerview to position */
void scrollToPosition(int i) {
if (i < 0) return;
RecyclerView.SmoothScroller smoothScroller = new LinearSmoothScroller(getContext()) {
@Override protected int getVerticalSnapPreference() {
return LinearSmoothScroller.SNAP_TO_START;
}
};
smoothScroller.setTargetPosition(i);
recyclerView.getLayoutManager().startSmoothScroll(smoothScroller);
}
/** Initialize the adapter */
private void initAdapter(final List<T> result) {
try {
......
......@@ -4,5 +4,7 @@ import android.content.Context;
import android.view.View.OnClickListener;
public interface Clickable {
String getId();
OnClickListener getOnClickListener(Context context);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment