Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resume scroll position #429

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.lucasr.twowayview.widget.SpannableGridLayoutManager;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
Expand All @@ -14,6 +15,7 @@
import android.support.v4.content.res.ResourcesCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -60,6 +62,9 @@ public class SessionsFragment extends BaseFragment {
@Inject
CompositeDisposable compositeDisposable;

@Inject
SharedPreferences sharedPreferences;

private SessionsAdapter adapter;

private FragmentSessionsBinding binding;
Expand Down Expand Up @@ -116,6 +121,8 @@ public void onResume() {
@Override
public void onStop() {
super.onStop();
int position = binding.recyclerView.getFirstVisiblePosition();
sharedPreferences.edit().putInt("last_position", position).apply();
Copy link
Contributor

@konifar konifar Mar 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this app uses kvs-schema.
So you can use auto generated method to access shared preferences.
Can you follow the steps below?

1. Add key to DefaultPrefsSchema

@Table(name = "io.github.droidkaigi.confsched_preferences")
public interface DefaultPrefsSchema {
    ...
    @Key(name = "last_sessions_view_position")
    int lastSessionsViewPosition;
}

2. Use generated method.

DefaultPrefs prefs = DefaultPrefs.get(context);
int lastPosition = prefs.getLastSessionsViewPosition();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! Thank you 😀

compositeDisposable.clear();
}

Expand Down Expand Up @@ -217,6 +224,23 @@ private void renderSessions(List<SessionViewModel> adjustedSessionViewModels) {
binding.txtDate.setText(adjustedSessionViewModels.get(0).getFormattedDate());
binding.txtDate.setVisibility(View.VISIBLE);
}

resumeLastPosition();
}

private void resumeLastPosition() {
int lastPosition = sharedPreferences.getInt("last_position", 0);
binding.recyclerView.post(() -> {
for (int i = lastPosition; i < adapter.getItems().size(); i++) {
if (!TextUtils.isEmpty(adapter.getItem(i).getFormattedDate())) {
String date = adapter.getItem(i).getFormattedDate();
binding.txtDate.setText(date);
break;
}
}
binding.recyclerView.getLayoutManager().scrollToPosition(lastPosition);
binding.txtDate.setVisibility(View.VISIBLE);
});
}

private void renderHeaderRow(List<Room> rooms) {
Expand Down Expand Up @@ -250,6 +274,10 @@ public void onBindViewHolder(BindingHolder<ViewSessionCellBinding> holder, int p
holder.binding.setViewModel(viewModel);
holder.binding.executePendingBindings();
}

public List<SessionViewModel> getItems() {
return list;
}
}

private static class ClickGestureCanceller {
Expand Down