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

If today matches the opening date, scroll to that day. #430

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
@@ -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, ""))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is no match, position 0 is returned.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it better to use Stream? In this case, just for loop seems easy. What do you think?

for (int i = 0; i<= adjustedSessionViewModels.size(); i++) {
  SessionViewModel viewModel = adjustedSessionViewModels.get(i);
  if (todayString.equals(viewModel.getFormattedDate()) {
    return i;
  }
  return 0;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreeee~

.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);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a better way? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I don't know why OnGlobalLayoutListener is needed 🙇
Is it enough just to call binding.recyclerView.getLayoutManager().scrollToPosition(position); ?

}
});
}

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