diff --git a/component/viewer/build.gradle b/component/viewer/build.gradle index 2dd27941..e1d552bb 100644 --- a/component/viewer/build.gradle +++ b/component/viewer/build.gradle @@ -61,4 +61,6 @@ dependencies { // TODO-rca: バージョンカタログに切り出す implementation "com.hendraanggrian.material:collapsingtoolbarlayout-subtitle:1.5.0" + // ViewPager2 + implementation "androidx.viewpager2:viewpager2:1.0.0" } \ No newline at end of file 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 new file mode 100644 index 00000000..3fe30549 --- /dev/null +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerBodyFragment.java @@ -0,0 +1,123 @@ +package one.nem.lacerta.component.viewer; + +import android.os.Bundle; + +import androidx.fragment.app.Fragment; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import javax.inject.Inject; + +import dagger.hilt.android.AndroidEntryPoint; +import one.nem.lacerta.data.Document; +import one.nem.lacerta.data.LacertaLibrary; +import one.nem.lacerta.utils.LacertaLogger; +import one.nem.lacerta.vcs.LacertaVcs; +import one.nem.lacerta.vcs.factory.LacertaVcsFactory; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link ViewerBodyFragment#newInstance} factory method to + * create an instance of this fragment. + */ +@AndroidEntryPoint +public class ViewerBodyFragment extends Fragment { + + @Inject + Document document; + + @Inject + LacertaLibrary lacertaLibrary; + + @Inject + LacertaLogger logger; + + @Inject + LacertaVcsFactory lacertaVcsFactory; + + // Variables + private String documentId; + private String documentName; + private String revisionId; + + public ViewerBodyFragment() { + // Required empty public constructor + } + + public static ViewerBodyFragment newInstance(String documentId, String documentName) { + ViewerBodyFragment fragment = new ViewerBodyFragment(); + Bundle args = new Bundle(); + args.putString("documentId", documentId); + args.putString("documentName", documentName); + fragment.setArguments(args); + return fragment; + } + + public static ViewerBodyFragment newInstance(String documentId, String documentName, String revisionId) { + ViewerBodyFragment fragment = new ViewerBodyFragment(); + Bundle args = new Bundle(); + args.putString("documentId", documentId); + args.putString("documentName", documentName); + args.putString("revisionId", revisionId); + fragment.setArguments(args); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null) { + documentId = getArguments().getString("documentId"); + documentName = getArguments().getString("documentName"); + revisionId = getArguments().getString("revisionId"); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_viewer_body, container, false); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + logger.debug("ViewerBodyFragment", "ViewerBodyFragment.onViewCreated"); + + RecyclerView recyclerView = view.findViewById(R.id.recycler_view); + recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + + ViewerBodyAdapter viewerBodyAdapter = new ViewerBodyAdapter(fileName -> { + Toast.makeText(getContext(), fileName, Toast.LENGTH_SHORT).show(); + // TODO-rca: なにか処理をもたせる + }); + recyclerView.setAdapter(viewerBodyAdapter); + + loadDocument(viewerBodyAdapter, documentId, revisionId); + } + + private void loadDocument(ViewerBodyAdapter adapter, String documentId, String revisionId) { + if (revisionId == null) { // load latest revision + document.getDocument(documentId).thenAccept(document -> { + getActivity().runOnUiThread(() -> { + adapter.setPages(document.getPages()); + adapter.notifyDataSetChanged(); + }); + }); + } else { // load specified revision + LacertaVcs vcs = lacertaVcsFactory.create(documentId); + document.getDocumentPageListByFileNameList(documentId, vcs.getDocumentPagePathListRev(revisionId).join()).thenAccept(documentPageList -> { + getActivity().runOnUiThread(() -> { + adapter.setPages(documentPageList); + adapter.notifyDataSetChanged(); + }); + }); + } + } +} \ No newline at end of file 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 new file mode 100644 index 00000000..7636eb97 --- /dev/null +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerContainerFragment.java @@ -0,0 +1,243 @@ +package one.nem.lacerta.component.viewer; + +import android.os.Bundle; + +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.Fragment; +import androidx.viewpager2.widget.ViewPager2; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.TextView; +import android.widget.Toast; + +import com.google.android.material.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.tabs.TabLayout; +import com.google.android.material.tabs.TabLayoutMediator; +import com.google.android.material.textfield.TextInputEditText; +import com.google.android.material.textfield.TextInputLayout; + +import javax.inject.Inject; + +import dagger.hilt.android.AndroidEntryPoint; +import one.nem.lacerta.component.common.picker.LacertaFilePickerDialog; +import one.nem.lacerta.data.Document; +import one.nem.lacerta.data.LacertaLibrary; +import one.nem.lacerta.model.document.page.Page; +import one.nem.lacerta.model.pref.ToxiDocumentModel; +import one.nem.lacerta.utils.LacertaLogger; + +/** + * A simple {@link Fragment} subclass. + * Use the {@link ViewerContainerFragment#newInstance} factory method to + * create an instance of this fragment. + */ +@AndroidEntryPoint +public class ViewerContainerFragment extends Fragment { + + // Inject + + @Inject + LacertaLogger logger; + + @Inject + LacertaLibrary lacertaLibrary; + + @Inject + Document document; + + // Variables + private String documentId; + private String documentName; + private boolean hasCombined = false; + + public ViewerContainerFragment() { + // Required empty public constructor + } + + public static ViewerContainerFragment newInstance(String documentId, String documentName, boolean hasCombined) { + ViewerContainerFragment fragment = new ViewerContainerFragment(); + Bundle args = new Bundle(); + fragment.setArguments(args); + args.putString("documentId", documentId); + args.putString("documentName", documentName); + args.putBoolean("hasCombined", hasCombined); + return fragment; + } + + public static ViewerContainerFragment newInstance(String documentId, String documentName) { + ViewerContainerFragment fragment = new ViewerContainerFragment(); + Bundle args = new Bundle(); + fragment.setArguments(args); + args.putString("documentId", documentId); + args.putString("documentName", documentName); + args.putBoolean("hasCombined", false); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + if (getArguments() != null){ + documentId = getArguments().getString("documentId"); + documentName = getArguments().getString("documentName"); + hasCombined = getArguments().getBoolean("hasCombined"); + } + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_viewer_container, container, false); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + // Init view pager + ViewPager2 viewPager = view.findViewById(R.id.view_pager); + + // Init view pager adapter + ViewerViewPagerAdapter viewerViewPagerAdapter = new ViewerViewPagerAdapter(requireActivity()); + viewPager.setAdapter(viewerViewPagerAdapter); + + // Init tab layout + TabLayout tabLayout = view.findViewById(R.id.tab_layout); + + // Init toolbar + Toolbar toolbar = view.findViewById(R.id.toolbar); + initToolbar(toolbar, true, documentName); + + if (this.hasCombined) { + logger.debug("ViewerContainerFragment", "hasCombined: " + hasCombined); + lacertaLibrary.getCombinedDocumentToxiList(documentId).thenAccept(combinedDocumentToxiList -> { + logger.debug("ViewerContainerFragment", "combinedDocumentToxiList: " + combinedDocumentToxiList.size()); + for (ToxiDocumentModel toxiDocumentModel : combinedDocumentToxiList) { + logger.debug("ViewerContainerFragment", "titleCache: " + toxiDocumentModel.getTitleCache()); + viewerViewPagerAdapter + .addFragment(ViewerBodyFragment.newInstance(toxiDocumentModel.getChildDocumentId(), toxiDocumentModel.getTitleCache()), + toxiDocumentModel.getTitleCache()); + } + viewerViewPagerAdapter.notifyItemRangeChanged(0, combinedDocumentToxiList.size()); + }); + } else { + logger.debug("ViewerContainerFragment", "hasCombined: " + hasCombined); + tabLayout.setVisibility(View.GONE); + viewerViewPagerAdapter.addFragment(ViewerBodyFragment.newInstance(documentId, documentName), documentName); + viewerViewPagerAdapter.notifyItemRangeChanged(0, 1); + } + + // Attach tab layout to view pager + new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> { + View customView = LayoutInflater.from(getContext()).inflate(R.layout.viewer_custom_tab, null); + + TextView textView = customView.findViewById(R.id.tab_title); + textView.setText(viewerViewPagerAdapter.getTabTitle(position)); + + ImageButton imageButton = customView.findViewById(R.id.tab_modify); + imageButton.setOnClickListener(v -> { + Toast.makeText(getContext(), "Working!, Index:" + position, Toast.LENGTH_SHORT).show(); + }); + + tab.setCustomView(customView); + }).attach(); + } + + /** + * Toolbarをinitする + * + * @param toolbar Toolbar + * @param showCloseButton Closeボタンを表示するかどうか + * @param title タイトル + */ + private void initToolbar(Toolbar toolbar, boolean showCloseButton, String title) { + getActivity().runOnUiThread(() -> { + // Close button + if (showCloseButton) { + toolbar.setNavigationIcon(one.nem.lacerta.shared.ui.R.drawable.close_24px); + toolbar.setNavigationOnClickListener(v -> { + getActivity().finish(); + }); + } else { + toolbar.setNavigationIcon(null); + } + + // Title + toolbar.setTitle(title); + + // Menu + toolbar.inflateMenu(R.menu.viewer_menu); + toolbar.setOnMenuItemClickListener(item -> { + if (item.getItemId() == R.id.action_open_vcs_rev_list) { + Toast.makeText(getContext(), "Work in progress", Toast.LENGTH_SHORT).show(); + return true; + } else if (item.getItemId() == R.id.action_rename) { + renameDocument(); + return true; + } else if (item.getItemId() == R.id.action_delete) { + Toast.makeText(getContext(), "Work in progress", Toast.LENGTH_SHORT).show(); + return true; + } else if (item.getItemId() == R.id.action_move) { + Toast.makeText(getContext(), "Work in progress", Toast.LENGTH_SHORT).show(); + return true; + } else if (item.getItemId() == R.id.action_combine) { + combineDocument(); + return true; + } else { + return false; + } + }); + }); + } + + private void combineDocument() { + LacertaFilePickerDialog lacertaFilePickerDialog = new LacertaFilePickerDialog(); + lacertaFilePickerDialog.setListener((fileName, selectedId) -> { + lacertaLibrary.combineDocument(documentId, selectedId).thenAccept(aVoid -> { + getActivity().runOnUiThread(() -> { + Toast.makeText(getContext(), "結合しました", Toast.LENGTH_SHORT).show(); + getActivity().finish(); // TODO-rca: 終了させずにUIを更新したい + }); + }); + }); + lacertaFilePickerDialog + .setTitle("ファイルの結合") + .setMessage("結合するファイルを選択してください") + .setNegativeButtonText("キャンセル") + .show(getChildFragmentManager(), "LacertaFilePickerDialog"); + } + + /** + * ドキュメント名を変更する + */ + private void renameDocument() { + MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext()); + builder.setTitle("ファイル名の変更"); + builder.setMessage("ファイル名を入力してください"); + + View view = LayoutInflater.from(getContext()).inflate(one.nem.lacerta.shared.ui.R.layout.lacerta_dialog_edit_text_layout, null); + TextInputEditText textInputEditText = view.findViewById(one.nem.lacerta.shared.ui.R.id.custom_edit_text); + TextInputLayout textInputLayout = view.findViewById(one.nem.lacerta.shared.ui.R.id.custom_text_input_layout); + textInputEditText.setText(documentName); + textInputLayout.setHint("ファイル名"); + builder.setView(view); + + builder.setPositiveButton("変更", (dialog, which) -> { + document.renameDocument(documentId, textInputEditText.getText().toString()).thenAccept(aVoid -> { + getActivity().runOnUiThread(() -> { + this.documentName = textInputEditText.getText().toString(); + // TODO-rca: Toolbarのタイトルも変更する + }); + }); + }); + builder.setNegativeButton("キャンセル", (dialog, which) -> { + dialog.cancel(); + }); + + builder.show(); + } +} \ No newline at end of file diff --git a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerListFragment.java b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerListFragment.java index 65518a10..c4ba8a1c 100644 --- a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerListFragment.java +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerListFragment.java @@ -24,6 +24,8 @@ import one.nem.lacerta.component.common.LacertaSelectDirDialogListener; import one.nem.lacerta.component.common.LacertaSelectRevDialog; import one.nem.lacerta.component.common.LacertaSelectRevDialogListener; import one.nem.lacerta.component.common.picker.LacertaDirPickerDialog; +import one.nem.lacerta.component.common.picker.LacertaFilePickerAdapter; +import one.nem.lacerta.component.common.picker.LacertaFilePickerDialog; import one.nem.lacerta.data.Document; import one.nem.lacerta.data.LacertaLibrary; import one.nem.lacerta.model.ListItemType; @@ -256,6 +258,21 @@ public class ViewerListFragment extends Fragment { .setNegativeButtonText("キャンセル"); lacertaDirPickerDialog.show(getParentFragmentManager(), "select_dir_dialog"); return true; + } else if(item.getItemId() == R.id.action_combine) { + LacertaFilePickerDialog lacertaFilePickerDialog = new LacertaFilePickerDialog(); + lacertaFilePickerDialog.setListener((name, fileId) -> { + lacertaLibrary.combineDocument(documentId, fileId).thenAccept(aVoid -> { + getActivity().runOnUiThread(() -> { + // Stop Activity + getActivity().finish(); // TODO-rca: 終了せずにUIを更新したい + }); + }); + }); + lacertaFilePickerDialog.setTitle("ファイルの結合") + .setMessage("結合するファイルを選択してください。") + .setNegativeButtonText("キャンセル"); + lacertaFilePickerDialog.show(getParentFragmentManager(), "select_file_dialog"); + return true; } else { return false; } diff --git a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerMainActivity.java b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerMainActivity.java index 0b9a3e59..0c73af39 100644 --- a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerMainActivity.java +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerMainActivity.java @@ -30,6 +30,7 @@ public class ViewerMainActivity extends AppCompatActivity { private static final String TAG = "ViewerMainActivity"; String documentId; String documentName; + boolean hasCombined = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -49,6 +50,7 @@ public class ViewerMainActivity extends AppCompatActivity { try { documentId = intent.getStringExtra("documentId"); documentName = intent.getStringExtra("documentName"); + hasCombined = intent.getBooleanExtra("hasCombined", false); } catch (Exception e) { logger.error(TAG, "Failed to get documentId from intent"); @@ -58,9 +60,18 @@ public class ViewerMainActivity extends AppCompatActivity { } // Navigation - getSupportFragmentManager().beginTransaction() - .replace(R.id.nav_host_fragment, ViewerListFragment.newInstance(documentId, documentName)) - .commit(); +// getSupportFragmentManager().beginTransaction() +// .replace(R.id.nav_host_fragment, ViewerListFragment.newInstance(documentId, documentName)) +// .commit(); + if (hasCombined) { + getSupportFragmentManager().beginTransaction() + .replace(R.id.nav_host_fragment, ViewerContainerFragment.newInstance(documentId, documentName, hasCombined)) + .commit(); + } else { + getSupportFragmentManager().beginTransaction() + .replace(R.id.nav_host_fragment, ViewerContainerFragment.newInstance(documentId, documentName)) + .commit(); + } } } \ No newline at end of file diff --git a/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerViewPagerAdapter.java b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerViewPagerAdapter.java new file mode 100644 index 00000000..e3f998fe --- /dev/null +++ b/component/viewer/src/main/java/one/nem/lacerta/component/viewer/ViewerViewPagerAdapter.java @@ -0,0 +1,44 @@ +package one.nem.lacerta.component.viewer; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; +import androidx.fragment.app.FragmentPagerAdapter; +import androidx.viewpager2.adapter.FragmentStateAdapter; + +import java.util.ArrayList; + +public class ViewerViewPagerAdapter extends FragmentStateAdapter { + + // Variables + private ArrayList fragmentArrayList = new ArrayList<>(); + private ArrayList fragmentTitleList = new ArrayList<>(); + + // Setter + public void addFragment(Fragment fragment, String title){ + fragmentArrayList.add(fragment); + fragmentTitleList.add(title); + } + + public ViewerViewPagerAdapter(@NonNull FragmentActivity fragmentActivity) { + super(fragmentActivity); + } + + @NonNull + @Override + public Fragment createFragment(int position) { + return fragmentArrayList.get(position); + } + + @Override + public int getItemCount() { + return fragmentArrayList == null ? 0 : fragmentArrayList.size(); + } + + @Nullable + public CharSequence getTabTitle(int position) { + return fragmentTitleList.get(position); + } +} diff --git a/component/viewer/src/main/res/layout/fragment_viewer_body.xml b/component/viewer/src/main/res/layout/fragment_viewer_body.xml new file mode 100644 index 00000000..0309ef0a --- /dev/null +++ b/component/viewer/src/main/res/layout/fragment_viewer_body.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/component/viewer/src/main/res/layout/fragment_viewer_container.xml b/component/viewer/src/main/res/layout/fragment_viewer_container.xml new file mode 100644 index 00000000..12ed3204 --- /dev/null +++ b/component/viewer/src/main/res/layout/fragment_viewer_container.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/component/viewer/src/main/res/layout/viewer_custom_tab.xml b/component/viewer/src/main/res/layout/viewer_custom_tab.xml new file mode 100644 index 00000000..afa650cc --- /dev/null +++ b/component/viewer/src/main/res/layout/viewer_custom_tab.xml @@ -0,0 +1,25 @@ + + + + + + + \ No newline at end of file diff --git a/component/viewer/src/main/res/menu/viewer_menu.xml b/component/viewer/src/main/res/menu/viewer_menu.xml index 5d301a09..231419b6 100644 --- a/component/viewer/src/main/res/menu/viewer_menu.xml +++ b/component/viewer/src/main/res/menu/viewer_menu.xml @@ -16,4 +16,8 @@ android:id="@+id/action_move" android:title="移動" /> + + \ No newline at end of file diff --git a/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java b/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java index 19cd9b34..7e8ba899 100644 --- a/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java +++ b/data/src/main/java/one/nem/lacerta/data/LacertaLibrary.java @@ -10,6 +10,7 @@ import one.nem.lacerta.model.LibraryItemPage; import one.nem.lacerta.model.PublicPath; import one.nem.lacerta.model.document.DocumentDetail; import one.nem.lacerta.model.document.tag.DocumentTag; +import one.nem.lacerta.model.pref.ToxiDocumentModel; public interface LacertaLibrary { @@ -43,4 +44,16 @@ public interface LacertaLibrary { CompletableFuture addTagToDocument(String documentId, String tagId); CompletableFuture removeTagFromDocument(String documentId, String tagId); + + // Combined Document + + CompletableFuture combineDocument(String parentId, String childId); + + CompletableFuture uncombineDocument(String parentId, String childId); + +// CompletableFuture combineDocument(String parentId, ArrayList childIdList); +// +// CompletableFuture uncombineDocument(String parentId, ArrayList childIdList); + + CompletableFuture> getCombinedDocumentToxiList(String parentId); } diff --git a/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java index bcd3318e..40dc234e 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/DocumentImpl.java @@ -81,6 +81,8 @@ public class DocumentImpl implements Document { documentEntity.updatedAt = meta.getUpdatedAt(); documentEntity.createdAt = meta.getCreatedAt(); documentEntity.parentId = meta.getParentId(); + documentEntity.isCombineChild = meta.getIsCombineChild(); + documentEntity.isCombineParent = meta.getIsCombineParent(); database.documentDao().insert(documentEntity); @@ -104,6 +106,8 @@ public class DocumentImpl implements Document { meta.setUpdatedAt(new Date()); meta.setCreatedAt(new Date()); meta.setParentId(null); + meta.setIsCombineChild(false); + meta.setIsCombineParent(false); return createDocument(meta); } diff --git a/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java b/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java index 816bf706..e6c19bd9 100644 --- a/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java +++ b/data/src/main/java/one/nem/lacerta/data/impl/LacertaLibraryImpl.java @@ -8,6 +8,7 @@ import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Date; import java.util.List; import java.util.Objects; import java.util.UUID; @@ -22,11 +23,13 @@ import one.nem.lacerta.model.ListItemType; import one.nem.lacerta.model.PublicPath; import one.nem.lacerta.model.document.DocumentDetail; import one.nem.lacerta.model.document.tag.DocumentTag; +import one.nem.lacerta.model.pref.ToxiDocumentModel; import one.nem.lacerta.source.database.LacertaDatabase; import one.nem.lacerta.source.database.common.DateTypeConverter; import one.nem.lacerta.source.database.entity.DocumentEntity; import one.nem.lacerta.source.database.entity.FolderEntity; import one.nem.lacerta.source.database.entity.TagEntity; +import one.nem.lacerta.source.database.entity.ToxiDocumentEntity; import one.nem.lacerta.source.database.entity.ToxiDocumentTagEntity; import one.nem.lacerta.utils.FeatureSwitch; import one.nem.lacerta.utils.LacertaLogger; @@ -54,6 +57,7 @@ public class LacertaLibraryImpl implements LacertaLibrary { listItem.setTitle(documentEntity.title); listItem.setDescription(DateFormat.getDateInstance().format(documentEntity.updatedAt)); listItem.setItemId(documentEntity.id); + listItem.setHasCombined(documentEntity.isCombineParent); listItems.add(listItem); } @@ -103,6 +107,7 @@ public class LacertaLibraryImpl implements LacertaLibrary { listItem.setTitle(childFolderEntity.name); listItem.setDescription("フォルダ"); // TODO-rca: ハードコーディングやめる listItem.setItemId(childFolderEntity.id); + listItem.setHasCombined(false); listItems.add(listItem); } @@ -115,6 +120,7 @@ public class LacertaLibraryImpl implements LacertaLibrary { listItem.setTitle(documentEntity.title); listItem.setDescription(simpleDateFormat.format(documentEntity.updatedAt)); listItem.setItemId(documentEntity.id); + listItem.setHasCombined(documentEntity.isCombineParent); listItems.add(listItem); } @@ -164,6 +170,7 @@ public class LacertaLibraryImpl implements LacertaLibrary { listItem.setTitle(childFolderEntity.name); listItem.setDescription("フォルダ"); // TODO-rca: ハードコーディングやめる listItem.setItemId(childFolderEntity.id); + listItem.setHasCombined(false); listItems.add(listItem); } @@ -294,6 +301,90 @@ public class LacertaLibraryImpl implements LacertaLibrary { }); } + @Override + public CompletableFuture combineDocument(String targetId1, String tagetId2) { + return CompletableFuture.supplyAsync(() -> { + DocumentEntity target1DocumentEntity = database.documentDao().findById(targetId1); + DocumentEntity target2DocumentEntity = database.documentDao().findById(tagetId2); + if (target1DocumentEntity == null || target2DocumentEntity == null) { + logger.warn("LacertaLibraryImpl", "DocumentEntity is not found."); + return null; + } + target1DocumentEntity.isCombineChild = true; + target2DocumentEntity.isCombineChild = true; + database.documentDao().update(target1DocumentEntity); + database.documentDao().update(target2DocumentEntity); + logger.debug("LacertaLibraryImpl", "Database Query: Updated DocumentEntity"); + + DocumentEntity parentDocumentEntity = new DocumentEntity(); + parentDocumentEntity.id = UUID.randomUUID().toString(); + parentDocumentEntity.title = target1DocumentEntity.title + "(Combined)"; + parentDocumentEntity.parentId = target1DocumentEntity.parentId; + parentDocumentEntity.author = target1DocumentEntity.author; // TODO-rca: Target1とTarget2の作者が異なる場合の処理 + // CombinedなDocumentはいま作成された と考える + parentDocumentEntity.createdAt = new Date(); + parentDocumentEntity.updatedAt = new Date(); + parentDocumentEntity.isCombineParent = true; + parentDocumentEntity.isCombineChild = false; + database.documentDao().insert(parentDocumentEntity); + logger.debug("LacertaLibraryImpl", "Database Query: Inserted DocumentEntity"); + + insertCombineDocumentToxi(parentDocumentEntity.id, target1DocumentEntity.id, target1DocumentEntity.title); + insertCombineDocumentToxi(parentDocumentEntity.id, target2DocumentEntity.id, target2DocumentEntity.title); + return null; + }); + } + + private void insertCombineDocumentToxi(String parentId, String childId, String titleCache) { + ToxiDocumentEntity toxiDocumentEntity = new ToxiDocumentEntity(); + toxiDocumentEntity.parentDocumentId = parentId; + toxiDocumentEntity.childDocumentId = childId; + toxiDocumentEntity.order = 0; // TODO-rca: 並び順の概念をもたせる + toxiDocumentEntity.isActive = true; // TODO-rca: タブから非表示にできるようにする + toxiDocumentEntity.titleCache = titleCache; + database.toxiDocumentDao().insert(toxiDocumentEntity); + logger.debug("LacertaLibraryImpl", "Database Query: Inserted ToxiDocumentEntity"); + } + + @Override + public CompletableFuture uncombineDocument(String parentId, String childId) { + return CompletableFuture.supplyAsync(() -> { + DocumentEntity parentDocumentEntity = database.documentDao().findById(parentId); + DocumentEntity childDocumentEntity = database.documentDao().findById(childId); + if (parentDocumentEntity == null || childDocumentEntity == null) { + logger.warn("LacertaLibraryImpl", "DocumentEntity is not found."); + return null; + } + parentDocumentEntity.isCombineParent = false; + childDocumentEntity.isCombineChild = false; + database.documentDao().update(parentDocumentEntity); + database.documentDao().update(childDocumentEntity); + logger.debug("LacertaLibraryImpl", "Database Query: Updated DocumentEntity"); + + database.toxiDocumentDao().deleteByParentIdAndChildId(parentId, childId); + logger.debug("LacertaLibraryImpl", "Database Query: Deleted ToxiDocumentEntity"); + return null; + }); + } + + @Override + public CompletableFuture> getCombinedDocumentToxiList(String parentId) { + return CompletableFuture.supplyAsync(() -> { + List toxiDocumentEntities = database.toxiDocumentDao().findByParentId(parentId); + ArrayList toxiDocumentModels = new ArrayList<>(); + for (ToxiDocumentEntity toxiDocumentEntity : toxiDocumentEntities) { + ToxiDocumentModel toxiDocumentModel = new ToxiDocumentModel(); + toxiDocumentModel.setParentDocumentId(toxiDocumentEntity.parentDocumentId); + toxiDocumentModel.setChildDocumentId(toxiDocumentEntity.childDocumentId); + toxiDocumentModel.setOrder(toxiDocumentEntity.order); + toxiDocumentModel.setActive(toxiDocumentEntity.isActive); + toxiDocumentModel.setTitleCache(toxiDocumentEntity.titleCache); + toxiDocumentModels.add(toxiDocumentModel); + } + return toxiDocumentModels; + }); + } + /** * 再帰的にパスを解決する * diff --git a/feature/home/src/main/java/one/nem/lacerta/feature/home/DocumentSelectListener.java b/feature/home/src/main/java/one/nem/lacerta/feature/home/DocumentSelectListener.java index 3d33367f..61c4f9d1 100644 --- a/feature/home/src/main/java/one/nem/lacerta/feature/home/DocumentSelectListener.java +++ b/feature/home/src/main/java/one/nem/lacerta/feature/home/DocumentSelectListener.java @@ -1,5 +1,5 @@ package one.nem.lacerta.feature.home; public interface DocumentSelectListener { - void onDocumentSelect(String documentId, String documentName); + void onDocumentSelect(String documentId, String documentName, boolean hasCombined); } diff --git a/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java b/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java index 4ac6a93d..622e2dfe 100644 --- a/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java +++ b/feature/home/src/main/java/one/nem/lacerta/feature/home/HomeTopFragment.java @@ -85,11 +85,12 @@ public class HomeTopFragment extends Fragment { this.listItemAdapter = new ListItemAdapter(new DocumentSelectListener() { @Override - public void onDocumentSelect(String documentId, String documentName) { + public void onDocumentSelect(String documentId, String documentName, boolean hasCombined) { Intent intent = new Intent(getContext(), ViewerMainActivity.class); Log.d("HomeTopFragment", "onDocumentSelect: " + documentId + " " + documentName); intent.putExtra("documentId", documentId); intent.putExtra("documentName", documentName); + intent.putExtra("hasCombined", hasCombined); startActivity(intent, ActivityOptions.makeCustomAnimation(getContext(), one.nem.lacerta.shared.ui.R.anim.nav_up_enter_anim, one.nem.lacerta.shared.ui.R.anim.nav_up_exit_anim).toBundle()); } }); diff --git a/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java b/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java index 80656ecb..9d45e261 100644 --- a/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java +++ b/feature/home/src/main/java/one/nem/lacerta/feature/home/ListItemAdapter.java @@ -46,7 +46,7 @@ public class ListItemAdapter extends RecyclerView.Adapter { - listener.onDocumentSelect(listItem.getItemId(), listItem.getTitle()); + listener.onDocumentSelect(listItem.getItemId(), listItem.getTitle(), listItem.getHasCombined()); }); } diff --git a/feature/library/src/main/java/one/nem/lacerta/feature/library/DocumentSelectListener.java b/feature/library/src/main/java/one/nem/lacerta/feature/library/DocumentSelectListener.java index 5aa5294a..67dbc46f 100644 --- a/feature/library/src/main/java/one/nem/lacerta/feature/library/DocumentSelectListener.java +++ b/feature/library/src/main/java/one/nem/lacerta/feature/library/DocumentSelectListener.java @@ -2,5 +2,5 @@ package one.nem.lacerta.feature.library; public interface DocumentSelectListener { void onFolderSelected(String folderId, String folderName); - void onDocumentSelected(String documentId, String documentName); + void onDocumentSelected(String documentId, String documentName, boolean hasCombined); } diff --git a/feature/library/src/main/java/one/nem/lacerta/feature/library/LibraryPageFragment.java b/feature/library/src/main/java/one/nem/lacerta/feature/library/LibraryPageFragment.java index e23d2089..7a37e9f1 100644 --- a/feature/library/src/main/java/one/nem/lacerta/feature/library/LibraryPageFragment.java +++ b/feature/library/src/main/java/one/nem/lacerta/feature/library/LibraryPageFragment.java @@ -164,11 +164,12 @@ public class LibraryPageFragment extends Fragment { } @Override - public void onDocumentSelected(String documentId, String documentName) { + public void onDocumentSelected(String documentId, String documentName, boolean hasCombined) { Intent intent = new Intent(getContext(), ViewerMainActivity.class); logger.debug("LibraryTopFragment", "Document selected! documentId: " + documentId + ", documentName: " + documentName); intent.putExtra("documentId", documentId); intent.putExtra("documentName", documentName); + intent.putExtra("hasCombined", hasCombined); startActivity(intent); } }); diff --git a/feature/library/src/main/java/one/nem/lacerta/feature/library/ListItemAdapter.java b/feature/library/src/main/java/one/nem/lacerta/feature/library/ListItemAdapter.java index c28e61ec..57edf131 100644 --- a/feature/library/src/main/java/one/nem/lacerta/feature/library/ListItemAdapter.java +++ b/feature/library/src/main/java/one/nem/lacerta/feature/library/ListItemAdapter.java @@ -45,7 +45,7 @@ public class ListItemAdapter extends RecyclerView.Adapter { if (listItem.getItemType() == ListItemType.ITEM_TYPE_DOCUMENT) { - listener.onDocumentSelected(listItem.getItemId(), listItem.getTitle()); + listener.onDocumentSelected(listItem.getItemId(), listItem.getTitle(), listItem.getHasCombined()); } else if (listItem.getItemType() == ListItemType.ITEM_TYPE_FOLDER) { listener.onFolderSelected(listItem.getItemId(), listItem.getTitle()); diff --git a/model/src/main/java/one/nem/lacerta/model/ListItem.java b/model/src/main/java/one/nem/lacerta/model/ListItem.java index 323b6bdd..b987b616 100644 --- a/model/src/main/java/one/nem/lacerta/model/ListItem.java +++ b/model/src/main/java/one/nem/lacerta/model/ListItem.java @@ -10,6 +10,7 @@ public class ListItem { String description; ListItemType itemType; String itemId; + boolean hasCombined; // Constructor @@ -20,6 +21,14 @@ public class ListItem { this.itemId = itemId; } + public ListItem(String title, String description, ListItemType itemType, String itemId, boolean hasCombined) { + this.title = title; + this.description = description; + this.itemType = itemType; + this.itemId = itemId; + this.hasCombined = hasCombined; + } + public ListItem() { // Empty constructor } @@ -42,6 +51,10 @@ public class ListItem { return itemId; } + public boolean getHasCombined() { + return hasCombined; + } + // Setter public void setTitle(String title) { @@ -60,4 +73,8 @@ public class ListItem { this.itemId = itemId; } + public void setHasCombined(boolean hasCombined) { + this.hasCombined = hasCombined; + } + } diff --git a/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java b/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java index ed686d0c..ffe54b35 100644 --- a/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java +++ b/model/src/main/java/one/nem/lacerta/model/document/DocumentMeta.java @@ -36,6 +36,10 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 String author; + boolean isCombineChild; + + boolean isCombineParent; + // Constructor public DocumentMeta() { @@ -48,12 +52,16 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 this.parentId = null; this.updatedAt = new Date(); this.createdAt = new Date(); + this.isCombineChild = false; + this.isCombineParent = false; } public DocumentMeta(String title, String author) { this.id = UUID.randomUUID().toString(); this.title = title; this.author = author; + this.isCombineChild = false; + this.isCombineParent = false; } public DocumentMeta(String id, String title, Date updatedAt, Date createdAt, String author) { @@ -62,6 +70,8 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 this.updatedAt = updatedAt; this.createdAt = createdAt; this.author = author; + this.isCombineChild = false; + this.isCombineParent = false; } public DocumentMeta(String id, String title, Date updatedAt, Date createdAt, String parentId, String author) { @@ -71,6 +81,19 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 this.createdAt = createdAt; this.parentId = parentId; this.author = author; + this.isCombineChild = false; + this.isCombineParent = false; + } + + public DocumentMeta(String id, String title, Date updatedAt, Date createdAt, String parentId, String author, boolean isCombineChild, boolean isCombineParent) { + this.id = id; + this.title = title; + this.updatedAt = updatedAt; + this.createdAt = createdAt; + this.parentId = parentId; + this.author = author; + this.isCombineChild = isCombineChild; + this.isCombineParent = isCombineParent; } // Getter @@ -117,6 +140,20 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 return author; } + /** + * ドキュメントの結合子フラグ(boolean)を取得する + */ + public boolean getIsCombineChild() { + return isCombineChild; + } + + /** + * ドキュメントの結合親フラグ(boolean)を取得する + */ + public boolean getIsCombineParent() { + return isCombineParent; + } + // Setter /** @@ -167,6 +204,22 @@ public class DocumentMeta { // TODO-rca: JavaDoc対応 this.author = author; } + /** + * ドキュメントの結合子フラグ(boolean)を設定する + * @param isCombineChild ドキュメントの結合子フラグ + */ + public void setIsCombineChild(boolean isCombineChild) { + this.isCombineChild = isCombineChild; + } + + /** + * ドキュメントの結合親フラグ(boolean)を設定する + * @param isCombineParent ドキュメントの結合親フラグ + */ + public void setIsCombineParent(boolean isCombineParent) { + this.isCombineParent = isCombineParent; + } + /** * updatedAtを現在時刻に設定する */ diff --git a/model/src/main/java/one/nem/lacerta/model/pref/ToxiDocumentModel.java b/model/src/main/java/one/nem/lacerta/model/pref/ToxiDocumentModel.java new file mode 100644 index 00000000..0afcce79 --- /dev/null +++ b/model/src/main/java/one/nem/lacerta/model/pref/ToxiDocumentModel.java @@ -0,0 +1,68 @@ +package one.nem.lacerta.model.pref; + +public class ToxiDocumentModel { + + // Field + public String parentDocumentId; + + public String childDocumentId; + + public int order; + + public boolean isActive; + + public String titleCache; + + // Constructor + public ToxiDocumentModel() { + } + + public ToxiDocumentModel(String parentDocumentId, String childDocumentId, int order, boolean isActive, String titleCache) { + this.parentDocumentId = parentDocumentId; + this.childDocumentId = childDocumentId; + this.order = order; + this.isActive = isActive; + this.titleCache = titleCache; + } + + // Getter / Setter + public String getParentDocumentId() { + return parentDocumentId; + } + + public void setParentDocumentId(String parentDocumentId) { + this.parentDocumentId = parentDocumentId; + } + + public String getChildDocumentId() { + return childDocumentId; + } + + public void setChildDocumentId(String childDocumentId) { + this.childDocumentId = childDocumentId; + } + + public int getOrder() { + return order; + } + + public void setOrder(int order) { + this.order = order; + } + + public boolean isActive() { + return isActive; + } + + public void setActive(boolean active) { + isActive = active; + } + + public String getTitleCache() { + return titleCache; + } + + public void setTitleCache(String titleCache) { + this.titleCache = titleCache; + } +} diff --git a/shared/ui/src/main/res/drawable/more_vert_24px.xml b/shared/ui/src/main/res/drawable/more_vert_24px.xml new file mode 100644 index 00000000..fe1efeba --- /dev/null +++ b/shared/ui/src/main/res/drawable/more_vert_24px.xml @@ -0,0 +1,10 @@ + + + diff --git a/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java b/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java index bb9a5dd9..df677293 100644 --- a/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java +++ b/source/src/main/java/one/nem/lacerta/source/database/LacertaDatabase.java @@ -5,11 +5,13 @@ import androidx.room.RoomDatabase; // Entities import one.nem.lacerta.source.database.dao.FolderDao; +import one.nem.lacerta.source.database.dao.ToxiDocumentDao; import one.nem.lacerta.source.database.dao.ToxiDocumentTagDao; import one.nem.lacerta.source.database.entity.FolderEntity; import one.nem.lacerta.source.database.entity.TagEntity; import one.nem.lacerta.source.database.entity.DocumentEntity; import one.nem.lacerta.source.database.entity.LibraryEntity; +import one.nem.lacerta.source.database.entity.ToxiDocumentEntity; import one.nem.lacerta.source.database.entity.ToxiDocumentTagEntity; import one.nem.lacerta.source.database.entity.VcsRevEntity; import one.nem.lacerta.source.database.entity.VcsLogEntity; @@ -21,11 +23,12 @@ import one.nem.lacerta.source.database.dao.LibraryDao; import one.nem.lacerta.source.database.dao.VcsRevDao; import one.nem.lacerta.source.database.dao.VcsLogDao; -@Database(entities = {TagEntity.class, DocumentEntity.class, LibraryEntity.class, VcsRevEntity.class, VcsLogEntity.class, FolderEntity.class, ToxiDocumentTagEntity.class}, version = 6) +@Database(entities = {TagEntity.class, DocumentEntity.class, LibraryEntity.class, VcsRevEntity.class, VcsLogEntity.class, FolderEntity.class, ToxiDocumentTagEntity.class, ToxiDocumentEntity.class}, version = 8) public abstract class LacertaDatabase extends RoomDatabase { public abstract TagDao tagDao(); public abstract DocumentDao documentDao(); public abstract ToxiDocumentTagDao toxiDocumentTagDao(); + public abstract ToxiDocumentDao toxiDocumentDao(); public abstract LibraryDao libraryDao(); public abstract VcsRevDao vcsRevDao(); public abstract VcsLogDao vcsLogDao(); diff --git a/source/src/main/java/one/nem/lacerta/source/database/dao/DocumentDao.java b/source/src/main/java/one/nem/lacerta/source/database/dao/DocumentDao.java index 21e1cc2e..7b410ba4 100644 --- a/source/src/main/java/one/nem/lacerta/source/database/dao/DocumentDao.java +++ b/source/src/main/java/one/nem/lacerta/source/database/dao/DocumentDao.java @@ -29,13 +29,13 @@ public interface DocumentDao { @Query("SELECT * FROM Document WHERE id IN (:ids)") List findByIds(List ids); - @Query("SELECT * FROM Document WHERE parent_id = :parentId") + @Query("SELECT * FROM Document WHERE parent_id = :parentId AND is_combine_child = 0 ORDER BY created_at DESC") List findByParentId(String parentId); - @Query("SELECT * FROM Document WHERE parent_id IS NULL") + @Query("SELECT * FROM Document WHERE parent_id IS NULL AND is_combine_child = 0 ORDER BY created_at DESC") List findRootDocuments(); - @Query("SELECT * FROM Document ORDER BY created_at DESC LIMIT :limit") + @Query("SELECT * FROM Document WHERE is_combine_child = 0 ORDER BY created_at DESC LIMIT :limit") List getRecentDocument(int limit); // Insert diff --git a/source/src/main/java/one/nem/lacerta/source/database/dao/ToxiDocumentDao.java b/source/src/main/java/one/nem/lacerta/source/database/dao/ToxiDocumentDao.java new file mode 100644 index 00000000..201a5d37 --- /dev/null +++ b/source/src/main/java/one/nem/lacerta/source/database/dao/ToxiDocumentDao.java @@ -0,0 +1,35 @@ +package one.nem.lacerta.source.database.dao; + +import androidx.room.Dao; +import androidx.room.Insert; +import androidx.room.Query; +import androidx.room.Update; + +import java.util.List; + +import one.nem.lacerta.source.database.entity.ToxiDocumentEntity; + +@Dao +public interface ToxiDocumentDao { + + @Query("SELECT * FROM toxi_document WHERE parent_document_id = :parentId AND is_active = 1 ORDER BY `order` ASC") + List findByParentId(String parentId); + + @Insert + void insert(ToxiDocumentEntity toxiDocument); + + @Insert + void insertAll(ToxiDocumentEntity... toxiDocuments); + + @Update + void update(ToxiDocumentEntity toxiDocument); + + @Update + void updateAll(ToxiDocumentEntity... toxiDocuments); + + @Query("DELETE FROM toxi_document WHERE parent_document_id = :parentId") + void deleteByParentId(String parentId); + + @Query("DELETE FROM toxi_document WHERE parent_document_id = :parentId AND child_document_id = :childId") + void deleteByParentIdAndChildId(String parentId, String childId); +} diff --git a/source/src/main/java/one/nem/lacerta/source/database/entity/DocumentEntity.java b/source/src/main/java/one/nem/lacerta/source/database/entity/DocumentEntity.java index 1e7891e9..88c77281 100644 --- a/source/src/main/java/one/nem/lacerta/source/database/entity/DocumentEntity.java +++ b/source/src/main/java/one/nem/lacerta/source/database/entity/DocumentEntity.java @@ -36,4 +36,10 @@ public class DocumentEntity { @ColumnInfo(name = "parent_id") public String parentId; // 親フォルダID + + @ColumnInfo(name = "is_combine_child") + public boolean isCombineChild; // 結合されたドキュメントの子かどうか + + @ColumnInfo(name = "is_combine_parent") + public boolean isCombineParent; // 結合されたドキュメントの親かどうか } diff --git a/source/src/main/java/one/nem/lacerta/source/database/entity/ToxiDocumentEntity.java b/source/src/main/java/one/nem/lacerta/source/database/entity/ToxiDocumentEntity.java new file mode 100644 index 00000000..3261f91d --- /dev/null +++ b/source/src/main/java/one/nem/lacerta/source/database/entity/ToxiDocumentEntity.java @@ -0,0 +1,26 @@ +package one.nem.lacerta.source.database.entity; + +import androidx.annotation.NonNull; +import androidx.room.ColumnInfo; +import androidx.room.Entity; + +@Entity(primaryKeys = {"parent_document_id", "child_document_id"}, tableName = "toxi_document") +public class ToxiDocumentEntity { + + @ColumnInfo(name = "parent_document_id") + @NonNull + public String parentDocumentId; + + @ColumnInfo(name = "child_document_id") + @NonNull + public String childDocumentId; + + @ColumnInfo(name = "order") + public int order; + + @ColumnInfo(name = "is_active") + public boolean isActive; + + @ColumnInfo(name = "title_cache") + public String titleCache; +}