diff --git a/component/common/src/main/java/one/nem/lacerta/component/common/LacertaSelectDirDialog.java b/component/common/src/main/java/one/nem/lacerta/component/common/LacertaSelectDirDialog.java new file mode 100644 index 00000000..77e1ab5a --- /dev/null +++ b/component/common/src/main/java/one/nem/lacerta/component/common/LacertaSelectDirDialog.java @@ -0,0 +1,70 @@ +package one.nem.lacerta.component.common; + +import android.app.Dialog; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Toast; + +import androidx.appcompat.widget.Toolbar; +import androidx.fragment.app.DialogFragment; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.dialog.MaterialAlertDialogBuilder; + +import javax.inject.Inject; + +import dagger.hilt.android.AndroidEntryPoint; +import one.nem.lacerta.data.LacertaLibrary; + +@AndroidEntryPoint +public class LacertaSelectDirDialog extends DialogFragment { + + @Inject + LacertaLibrary lacertaLibrary; + + private SelectDirDialogItemAdapter adapter; + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getActivity()); + LayoutInflater inflater = requireActivity().getLayoutInflater(); + View view = inflater.inflate(one.nem.lacerta.shared.ui.R.layout.lacerta_dialog_select_dir, null); + + RecyclerView recyclerView = view.findViewById(one.nem.lacerta.shared.ui.R.id.select_dir_recycler_view); + recyclerView.setHasFixedSize(true); + + showRecyclerViewItem(null); // get root folder list + + this.adapter = new SelectDirDialogItemAdapter((name, itemId) -> { + Toast.makeText(getContext(), "Called: name", Toast.LENGTH_SHORT).show(); + showRecyclerViewItem(itemId); + dismiss(); + }); + + recyclerView.setAdapter(this.adapter); + recyclerView.setLayoutManager(new androidx.recyclerview.widget.LinearLayoutManager(getContext())); + + Toolbar toolbar = view.findViewById(one.nem.lacerta.shared.ui.R.id.select_dir_toolbar); + toolbar.setNavigationOnClickListener(v -> { + Toast.makeText(getContext(), "Called", Toast.LENGTH_SHORT).show(); + dismiss(); + }); + + builder.setView(view); + + builder.setTitle("Select Directory"); + builder.setMessage("Please select a directory."); + builder.setPositiveButton("OK", null); + builder.setNegativeButton("Cancel", null); + return builder.create(); + } + + private void showRecyclerViewItem(String parent) { + lacertaLibrary.getFolderList(parent).thenAccept(listItems -> { + adapter.setListItems(listItems); + adapter.notifyDataSetChanged(); + }); + + } +} diff --git a/component/common/src/main/java/one/nem/lacerta/component/common/LacertaSelectDirDialogEventListener.java b/component/common/src/main/java/one/nem/lacerta/component/common/LacertaSelectDirDialogEventListener.java new file mode 100644 index 00000000..f1f4ae4a --- /dev/null +++ b/component/common/src/main/java/one/nem/lacerta/component/common/LacertaSelectDirDialogEventListener.java @@ -0,0 +1,5 @@ +package one.nem.lacerta.component.common; + +public interface LacertaSelectDirDialogEventListener { + void onDirSelected(String name, String itemId); +} diff --git a/component/common/src/main/java/one/nem/lacerta/component/common/SelectDirDialogItemAdapter.java b/component/common/src/main/java/one/nem/lacerta/component/common/SelectDirDialogItemAdapter.java new file mode 100644 index 00000000..79a95acb --- /dev/null +++ b/component/common/src/main/java/one/nem/lacerta/component/common/SelectDirDialogItemAdapter.java @@ -0,0 +1,63 @@ +package one.nem.lacerta.component.common; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; + +import one.nem.lacerta.model.ListItem; + +public class SelectDirDialogItemAdapter extends RecyclerView.Adapter { + + ArrayList listItems; + LacertaSelectDirDialogEventListener listener; + + public SelectDirDialogItemAdapter(LacertaSelectDirDialogEventListener listener) { + this.listener = listener; + } + + public void setListItems(ArrayList listItems) { + this.listItems = listItems; + } + + @Override + public ViewHolder 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 ViewHolder(view); + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + ListItem listItem = listItems.get(position); + holder.title.setText(listItem.getTitle()); + holder.description.setText(listItem.getDescription()); + holder.itemView.setOnClickListener(v -> { + listener.onDirSelected(listItem.getTitle(), listItem.getItemId()); + }); + } + + @Override + public int getItemCount() { + return listItems.size(); + } + + public class ViewHolder extends RecyclerView.ViewHolder { + + TextView title; + TextView description; + + + public ViewHolder(@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); + + } + } +}