FilePickerDialogBase実装 WIP

This commit is contained in:
r-ca 2024-01-28 09:56:37 +09:00
parent 97b1806407
commit 7a106b1ed3
No known key found for this signature in database
GPG Key ID: 6A72911AC73464A9
2 changed files with 161 additions and 0 deletions

View File

@ -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<LacertaFilePickerAdapterBase.LacertaFilePickerViewHolder> {
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); // 暫定
}
}
}

View File

@ -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());
}
}
}