From a809c0ac35dadbedd78047ffa98cc7699be4cc85 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 22 Jul 2019 21:41:42 +0200 Subject: [PATCH] ActionListAdapter to kotlin --- .../automation/dialogs/ActionListAdapter.java | 78 ------------------- .../automation/dialogs/ActionListAdapter.kt | 54 +++++++++++++ .../automation/dialogs/EditEventDialog.kt | 2 +- .../res/layout/automation_action_item.xml | 17 +--- 4 files changed, 59 insertions(+), 92 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java deleted file mode 100644 index 2a1136fff0..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java +++ /dev/null @@ -1,78 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.dialogs; - -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.fragment.app.FragmentManager; -import androidx.recyclerview.widget.RecyclerView; - -import java.util.List; - -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.general.automation.actions.Action; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui; - -public class ActionListAdapter extends RecyclerView.Adapter { - private final List actionList; - private final FragmentManager fragmentManager; - - public ActionListAdapter(FragmentManager manager, List actions) { - this.actionList = actions; - this.fragmentManager = manager; - } - - @NonNull - @Override - public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.automation_action_item, parent, false); - return new ViewHolder(v); - } - - @Override - public void onBindViewHolder(@NonNull ViewHolder holder, int position) { - final Action action = actionList.get(position); - holder.actionTitle.setText(action.shortDescription()); - holder.layoutText.setOnClickListener(v -> { - if (action.hasDialog()) { - Bundle args = new Bundle(); - args.putInt("actionPosition", position); - args.putString("action", action.toJSON()); - EditActionDialog dialog = new EditActionDialog(); - dialog.setArguments(args); - dialog.show(fragmentManager, "EditActionDialog"); - } - }); - holder.iconTrash.setOnClickListener(v -> { - actionList.remove(action); - notifyDataSetChanged(); - RxBus.INSTANCE.send(new EventAutomationUpdateGui()); - }); - } - - @Override - public int getItemCount() { - return actionList.size(); - } - - static class ViewHolder extends RecyclerView.ViewHolder { - TextView actionTitle; - TextView actionDescription; - LinearLayout layoutText; - ImageView iconTrash; - - ViewHolder(View view) { - super(view); - layoutText = view.findViewById(R.id.layoutText); - actionTitle = view.findViewById(R.id.viewActionTitle); - actionDescription = view.findViewById(R.id.viewActionDescription); - iconTrash = view.findViewById(R.id.iconTrash); - } - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt new file mode 100644 index 0000000000..8c9efedd20 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.kt @@ -0,0 +1,54 @@ +package info.nightscout.androidaps.plugins.general.automation.dialogs + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout +import android.widget.TextView +import androidx.fragment.app.FragmentManager +import androidx.recyclerview.widget.RecyclerView +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.automation.actions.Action +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui + +class ActionListAdapter(private val fragmentManager: FragmentManager, private val actionList: MutableList) : RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val v = LayoutInflater.from(parent.context).inflate(R.layout.automation_action_item, parent, false) + return ViewHolder(v) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val action = actionList[position] + holder.bind(action, fragmentManager, this, position, actionList) + } + + override fun getItemCount(): Int { + return actionList.size + } + + class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) { + + fun bind(action: Action, fragmentManager: FragmentManager, recyclerView: RecyclerView.Adapter, position : Int, actionList: MutableList) { + view.findViewById(R.id.automation_layoutText).setOnClickListener { + if (action.hasDialog()) { + val args = Bundle() + args.putInt("actionPosition", position) + args.putString("action", action.toJSON()) + val dialog = EditActionDialog() + dialog.arguments = args + dialog.show(fragmentManager, "EditActionDialog") + } + } + view.findViewById(R.id.automation_iconTrash).setOnClickListener { + actionList.remove(action) + recyclerView.notifyDataSetChanged() + RxBus.send(EventAutomationUpdateGui()) + } + view.findViewById(R.id.automation_viewActionTitle).text = action.shortDescription() + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt index 676b6af40e..a16f6ed78b 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditEventDialog.kt @@ -53,7 +53,7 @@ class EditEventDialog : DialogFragment() { } // setup action list view - actionListAdapter = ActionListAdapter(fragmentManager, event.actions) + fragmentManager?.let { actionListAdapter = ActionListAdapter(it, event.actions)} automation_actionListView.layoutManager = LinearLayoutManager(context) automation_actionListView.adapter = actionListAdapter diff --git a/app/src/main/res/layout/automation_action_item.xml b/app/src/main/res/layout/automation_action_item.xml index 40ca024d56..2741f1d5ff 100644 --- a/app/src/main/res/layout/automation_action_item.xml +++ b/app/src/main/res/layout/automation_action_item.xml @@ -12,7 +12,7 @@ android:layout_marginTop="8dp"> - - \ No newline at end of file