Skip to content

Commit

Permalink
Scroll to the day of the current day.
Browse files Browse the repository at this point in the history
  • Loading branch information
sys1yagi committed Mar 10, 2017
1 parent f5ed3a7 commit 7887630
Showing 1 changed file with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.github.droidkaigi.confsched2017.view.fragment;

import com.annimon.stream.IntPair;
import com.annimon.stream.Stream;

import org.lucasr.twowayview.TwoWayLayoutManager;
import org.lucasr.twowayview.widget.DividerItemDecoration;
import org.lucasr.twowayview.widget.SpannableGridLayoutManager;
Expand All @@ -23,6 +26,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

Expand All @@ -36,6 +40,7 @@
import io.github.droidkaigi.confsched2017.databinding.FragmentSessionsBinding;
import io.github.droidkaigi.confsched2017.databinding.ViewSessionCellBinding;
import io.github.droidkaigi.confsched2017.model.Room;
import io.github.droidkaigi.confsched2017.util.DateUtil;
import io.github.droidkaigi.confsched2017.util.ViewUtil;
import io.github.droidkaigi.confsched2017.view.activity.MySessionsActivity;
import io.github.droidkaigi.confsched2017.view.activity.SearchActivity;
Expand Down Expand Up @@ -217,6 +222,28 @@ private void renderSessions(List<SessionViewModel> adjustedSessionViewModels) {
binding.txtDate.setText(adjustedSessionViewModels.get(0).getFormattedDate());
binding.txtDate.setVisibility(View.VISIBLE);
}
scrollTo(extractTodayPos(getContext(), new Date(), adjustedSessionViewModels));
}

private int extractTodayPos(Context context, Date today, List<SessionViewModel> adjustedSessionViewModels) {
String todayString = DateUtil.getMonthDate(today, context);
return Stream.of(adjustedSessionViewModels)
.map(SessionViewModel::getFormattedDate)
.indexed()
.filter(pair -> todayString.equals(pair.getSecond()))
.findFirst()
.orElse(new IntPair<>(0, ""))
.getFirst();
}

private void scrollTo(int position) {
binding.recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
binding.recyclerView.getLayoutManager().scrollToPosition(position);
binding.recyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}

private void renderHeaderRow(List<Room> rooms) {
Expand Down

0 comments on commit 7887630

Please sign in to comment.