Skip to content

Commit

Permalink
fixed newsfeed lazy loading bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhbir-singh committed Oct 24, 2016
1 parent 4e8e28a commit d1cc2fd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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.widget.ProgressBar;
import android.widget.Toast;
Expand All @@ -30,12 +31,13 @@

public class NewsfeedActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener{

private static final String FEED_LIST ="list" ;
private RecyclerView recyclerView;
private CardAdapter adapter;
private ProgressBar progressBar;
private SwipeRefreshLayout swipeRefreshLayout;
private boolean loading = true;
private int pastVisiblesItems, visibleItemCount, totalItemCount, previousTotal = 0, visibleThreshold = 0,feedNo=0;
private int pastVisiblesItems, visibleItemCount, totalItemCount, previousTotal = 0, visibleThreshold = 0,feedNo=1;
private ArrayList<NewsfeedModel2> list=new ArrayList<>();
private SharedPref pref;

Expand Down Expand Up @@ -78,18 +80,31 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}
if (!loading && (totalItemCount - visibleItemCount)
<= (pastVisiblesItems + visibleThreshold)) {

list.add(null);
adapter.notifyItemInserted(list.size() + 1);
feedNo+=11;
feedNo+=10;
showData(feedNo);
loading = true;
}
}
}
});


if(savedInstanceState==null)
showData(1);
else {

list=savedInstanceState.getParcelableArrayList(FEED_LIST);
if(list!=null){
recyclerView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
adapter.refresh(list);
}
else {
showData(1);
}
}

// Button to upload the NewsFeed

FloatingActionButton upload= (FloatingActionButton) findViewById(R.id.upload_btn);
Expand All @@ -102,17 +117,9 @@ public void onClick(View view) {

}



/**
* Adding few albums for testing
*/

private void showData(int from){
if(from>1){
adapter.removeItem(null);
}
else if(from==1) {

if(from==1) {
list.clear();
adapter.refresh(list);
recyclerView.setVisibility(View.GONE);
Expand All @@ -125,27 +132,45 @@ public void onResponse(Call<NewsfeedModel> call, Response<NewsfeedModel> respons
if(swipeRefreshLayout.isRefreshing()){
swipeRefreshLayout.setRefreshing(false);
}


recyclerView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
if(response!=null&&response.isSuccess()){
if(response.body().getFeed()!=null){
if(response.body().getFeed().size()>0){

if(list.size()!=0){
list.remove(list.size()-1);
adapter.refresh(list);
}

list.addAll(response.body().getFeed());
list.add(null);

adapter.refresh(list);}
else {
adapter.removeItem(null);
adapter.refresh(list);
}

Log.d("rr","check");

}
else {
adapter.removeItem(null);
Log.d("sa","check");

if(list.size()!=0){
list.remove(list.size()-1);
adapter.refresh(list);
}

}

}
else {
adapter.removeItem(null);

if(list.size()!=0){
list.remove(list.size()-1);
adapter.refresh(list);
}


Toast.makeText(NewsfeedActivity.this,"Unable to fetch Data",Toast.LENGTH_SHORT).show();
}
}
Expand All @@ -155,7 +180,12 @@ public void onFailure(Call<NewsfeedModel> call, Throwable t) {
if(swipeRefreshLayout.isRefreshing()){
swipeRefreshLayout.setRefreshing(false);
}
adapter.removeItem(null);

if(list.size()!=0){
list.remove(list.size()-1);
adapter.refresh(list);
}

t.printStackTrace();
progressBar.setVisibility(View.GONE);
Toast.makeText(NewsfeedActivity.this, "Please check your network connection and internet permission", Toast.LENGTH_LONG).show();
Expand All @@ -168,4 +198,11 @@ public void onRefresh()
{
showData(1);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(list!=null)
outState.putParcelableArrayList(FEED_LIST,list);
}
}
22 changes: 2 additions & 20 deletions app/src/main/java/appteam/nith/hillffair/adapters/CardAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,7 @@ public void onFailure(Call<Likecount> call, Throwable t) {
@Override
public int getItemCount() {

if(list_card.size()==0)
return 0;
else if(list_card.size()<=9)
return list_card.size();
else
return list_card.size()+1;
return list_card.size();
}


Expand All @@ -227,19 +222,13 @@ public MyViewHolder(View view) {


}
// @Override
// public void onClick(View v) {
// NewsfeedModel2 card = list_card.get(position);
// APIINTERFACE mApiService = Utils.getRetrofitService();
// Call<Likecount> mService = mApiService.likecount();
//}
}



@Override
public int getItemViewType(int position) {
return position>=list_card.size()?FOOTER_VIEW:NORMAL_VIEW;
return list_card.get(position)==null?FOOTER_VIEW:NORMAL_VIEW;
}

public static class footerView extends RecyclerView.ViewHolder {
Expand All @@ -248,11 +237,4 @@ public footerView(View itemView) {
}
}

public void removeItem(NewsfeedModel2 item) {
int indexOfItem = list_card.indexOf(item);
if (indexOfItem != -1) {
list_card.remove(indexOfItem);
notifyItemRemoved(indexOfItem);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public void notificationReceived(OSNotification notification) {
DbHelper dbHelper = new DbHelper(getApplicationContext());
if(dbHelper.insert_2_homeposts(id,small_icon,title,description,image,large_icon,launch_url)){
Log.v("","inserted Successfully");
Intent intent = new Intent(getApplicationContext(), NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

}
if (data != null) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface APIINTERFACE {
Call<UserScoreResponse> getUserScore(@Field("id") String id);

@FormUrlEncoded
@POST("quiz/questions")
@POST("quiz/questions_new")
Call<QuizQuestionsModel> getQuiz(@Field("id") String id);

@FormUrlEncoded
Expand Down

0 comments on commit d1cc2fd

Please sign in to comment.