From 0c27cf6b24d81e37931a10272a47f502ee7d2725 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Mon, 29 Jan 2024 11:45:35 +0900 Subject: [PATCH] WIP --- .../component/viewer/ViewerBodyFragment.java | 44 ++++++++++++++++++- .../viewer/ViewerContainerFragment.java | 12 +++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerBodyFragment.java b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerBodyFragment.java index f52576d4..334a7e97 100644 --- a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerBodyFragment.java +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerBodyFragment.java @@ -4,8 +4,10 @@ import android.content.Intent; import android.os.Bundle; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; +import androidx.viewpager2.widget.ViewPager2; import android.view.LayoutInflater; import android.view.View; @@ -49,6 +51,12 @@ public class ViewerBodyFragment extends Fragment { private String documentName; private String revisionId; + private SharedViewModel sharedViewModel; + + private RecyclerView recyclerView; + + private int maxPage = 0; + public ViewerBodyFragment() { // Required empty public constructor } @@ -94,9 +102,33 @@ public class ViewerBodyFragment extends Fragment { super.onViewCreated(view, savedInstanceState); logger.debug("ViewerBodyFragment", "ViewerBodyFragment.onViewCreated"); - RecyclerView recyclerView = view.findViewById(R.id.recycler_view); + this.recyclerView = view.findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); + + ViewPager2 viewPager2 = getActivity().findViewById(R.id.view_pager); + viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { + @Override + public void onPageSelected(int position) { + super.onPageSelected(position); + sharedViewModel.setCurrentFragmentPosition(position); + + if (maxPage > 0 && position >= maxPage) { + // out of range + } else { + try { + recyclerView.scrollToPosition(position); + } catch (Exception e) { + logger.error("ViewerBodyFragment", "recyclerView.scrollToPosition"); + logger.error("ViewerBodyFragment", e.getMessage()); + logger.e_code("8beec84b-1f7d-4e1e-9364-9fcf00f3509a"); + + } + } + } + }); + ViewerBodyAdapter viewerBodyAdapter = new ViewerBodyAdapter(new ItemClickListener() { @Override public void onItemClick(String fileName) { @@ -129,9 +161,19 @@ public class ViewerBodyFragment extends Fragment { loadDocument(viewerBodyAdapter, documentId, revisionId); } + @Override + public void onResume() { + super.onResume(); + logger.debug("ViewerBodyFragment", "ViewerBodyFragment.onResume"); + + this.sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class); + this.recyclerView.scrollToPosition(sharedViewModel.getCurrentFragmentPosition()); + } + private void loadDocument(ViewerBodyAdapter adapter, String documentId, String revisionId) { if (revisionId == null) { // load latest revision document.getDocument(documentId).thenAccept(document -> { + this.maxPage = document.getPages().size(); getActivity().runOnUiThread(() -> { adapter.setPages(document.getPages()); adapter.notifyDataSetChanged(); diff --git a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerContainerFragment.java b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerContainerFragment.java index f5fd50b0..3bfe11ab 100644 --- a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerContainerFragment.java +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerContainerFragment.java @@ -224,6 +224,18 @@ public class ViewerContainerFragment extends Fragment { tab.setCustomView(customView); }).attach(); + + // Sync position between pages + viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { + @Override + public void onPageSelected(int position) { + logger.debug("ViewerContainerFragment", "onPageSelected: " + position); + super.onPageSelected(position); + if (hasCombined) { + toolbar.setTitle(viewerViewPagerAdapter.getFragmentTitle(position)); + } + } + }); } private void renameCombinedDocument(String parentId, String childId, String current, int position) { // TODO-rca: 無理やりpositionを渡してるのでなんとかする