From 7a106b1ed321eb8c0ad0c943ae9f2226ed06fd6c Mon Sep 17 00:00:00 2001 From: r-ca Date: Sun, 28 Jan 2024 09:56:37 +0900 Subject: [PATCH] =?UTF-8?q?FilePickerDialogBase=E5=AE=9F=E8=A3=85=20WIP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../base/LacertaFilePickerAdapterBase.java | 101 ++++++++++++++++++ .../base/LacertaFilePickerDialogBase.java | 60 +++++++++++ 2 files changed, 161 insertions(+) create mode 100644 component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerAdapterBase.java create mode 100644 component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerDialogBase.java diff --git a/component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerAdapterBase.java b/component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerAdapterBase.java new file mode 100644 index 00000000..551a2b69 --- /dev/null +++ b/component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerAdapterBase.java @@ -0,0 +1,101 @@ +package one.nem.lacerta.component.common.picker.base; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import one.nem.lacerta.component.common.LacertaSelectDirDialogInternalEventListener; +import one.nem.lacerta.model.LibraryItemPage; +import one.nem.lacerta.model.ListItem; +import one.nem.lacerta.model.ListItemType; + +public class LacertaFilePickerAdapterBase extends RecyclerView.Adapter { + + + private LibraryItemPage libraryItemPage; + LacertaSelectDirDialogInternalEventListener listener; + + public LacertaFilePickerAdapterBase(LacertaSelectDirDialogInternalEventListener listener) { + this.listener = listener; + } + + public void setListItems(LibraryItemPage libraryItemPage) { + this.libraryItemPage = libraryItemPage; + if (this.libraryItemPage.getPageId() != null) { // ルートディレクトリの場合は戻るボタンを表示しない + this.libraryItemPage.getListItems().add(0, new ListItem("戻る", " ", ListItemType.ITEM_TYPE_ACTION_BACK, null)); + } + } + + @NonNull + @Override + public LacertaFilePickerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()).inflate(one.nem.lacerta.shared.ui.R.layout.common_list_item, parent, false); + return new LacertaFilePickerViewHolder(view); + } + + @Override + public void onBindViewHolder(LacertaFilePickerViewHolder holder, int position) { + ListItem listItem = libraryItemPage.getListItems().get(position); + holder.title.setText(listItem.getTitle()); + holder.description.setText(listItem.getDescription()); + holder.icon.setImageResource(listItem.getItemType().getIconId()); + if(listItem.getItemType() == ListItemType.ITEM_TYPE_ACTION_BACK) { + holder.itemView.setOnClickListener(v -> listener.onBackSelected(this.libraryItemPage.getParentId())); + } else { + holder.itemView.setOnClickListener(v -> listener.onDirSelected(listItem.getTitle(), listItem.getItemId())); + } + } + + @Override + public int getItemCount() { + return this.libraryItemPage == null ? 0 : this.libraryItemPage.getListItems().size(); + } + + public String getCurrentId() { + if (this.libraryItemPage == null) { + return null; + } else { + if (this.libraryItemPage.getPageId() == null) { + return null; + } else { + return this.libraryItemPage.getPageId(); + } + } + } + + public String getCurrentPageTitle() { + if (this.libraryItemPage == null) { + return null; + } else { + if (this.libraryItemPage.getPageId() == null) { + return null; + } else { + return this.libraryItemPage.getPageTitle(); + } + } + } + + public static class LacertaFilePickerViewHolder extends RecyclerView.ViewHolder { + + TextView title; + TextView description; + + ImageView icon; + + + public LacertaFilePickerViewHolder(@NonNull View itemView) { + super(itemView); + + title = itemView.findViewById(one.nem.lacerta.shared.ui.R.id.item_title); + description = itemView.findViewById(one.nem.lacerta.shared.ui.R.id.item_description); + icon = itemView.findViewById(one.nem.lacerta.shared.ui.R.id.item_icon); + description.setVisibility(View.GONE); // 暫定 + + } + } +} diff --git a/component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerDialogBase.java b/component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerDialogBase.java new file mode 100644 index 00000000..79f54e29 --- /dev/null +++ b/component/common/src/main/java/one/nem/lacerta/component/common/picker/base/LacertaFilePickerDialogBase.java @@ -0,0 +1,60 @@ +package one.nem.lacerta.component.common.picker.base; + +import android.app.Dialog; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.TextView; + +import androidx.fragment.app.DialogFragment; + +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import javax.inject.Inject; + +import one.nem.lacerta.component.common.LacertaSelectDirDialog; +import one.nem.lacerta.component.common.R; +import one.nem.lacerta.component.common.SelectDirDialogItemAdapter; +import one.nem.lacerta.data.LacertaLibrary; +import one.nem.lacerta.model.LibraryItemPage; +import one.nem.lacerta.model.ListItemType; +import one.nem.lacerta.utils.FeatureSwitch; +import one.nem.lacerta.utils.LacertaLogger; + +public class LacertaFilePickerDialogBase extends DialogFragment { + + @Inject + LacertaLibrary lacertaLibrary; + + @Inject + LacertaLogger logger; + + + // Methods + public void updateList(LacertaFilePickerAdapterBase adapter, LibraryItemPage libraryItemPage) { + int currentCount = adapter.getItemCount(); + String currentDirId = adapter.getCurrentId(); + if (currentDirId == null) { + // Rootが関わる推移 (Rootからの推移) + adapter.setListItems(libraryItemPage); + adapter.notifyItemRangeRemoved(0, currentCount); + adapter.notifyItemRangeInserted(0, libraryItemPage.getListItems().size()); + } else if (libraryItemPage.getPageId() == null) { + // Rootが関わる推移 (Rootへの推移) + adapter.setListItems(libraryItemPage); + adapter.notifyItemRangeRemoved(0, currentCount); + adapter.notifyItemRangeInserted(0, libraryItemPage.getListItems().size()); + } else if (libraryItemPage.getPageId() != null) { + // Rootが関わらない推移 + adapter.setListItems(libraryItemPage); + adapter.notifyItemRangeRemoved(1, currentCount); + adapter.notifyItemRangeInserted(1, libraryItemPage.getListItems().size()); + } else { + // その他の遷移(安全側に倒すため全アイテム更新) + logger.warn("FilePickerDialogBase", "Unknown transition."); + logger.warn("FilePickerDialogBase", "currentDirId: " + currentDirId + ", libraryItemPage.getPageId(): " + libraryItemPage.getPageId()); + adapter.setListItems(libraryItemPage); + adapter.notifyItemRangeRemoved(0, currentCount); + adapter.notifyItemRangeInserted(0, libraryItemPage.getListItems().size()); + } + } +}