ActionListAdapter to kotlin

This commit is contained in:
Milos Kozak 2019-07-22 21:41:42 +02:00
parent e539dbc112
commit a809c0ac35
4 changed files with 59 additions and 92 deletions

View file

@ -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<ActionListAdapter.ViewHolder> {
private final List<Action> actionList;
private final FragmentManager fragmentManager;
public ActionListAdapter(FragmentManager manager, List<Action> 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);
}
}
}

View file

@ -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<Action>) : RecyclerView.Adapter<ActionListAdapter.ViewHolder>() {
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<ViewHolder>, position : Int, actionList: MutableList<Action>) {
view.findViewById<LinearLayout>(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<ImageView>(R.id.automation_iconTrash).setOnClickListener {
actionList.remove(action)
recyclerView.notifyDataSetChanged()
RxBus.send(EventAutomationUpdateGui())
}
view.findViewById<TextView>(R.id.automation_viewActionTitle).text = action.shortDescription()
}
}
}

View file

@ -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

View file

@ -12,7 +12,7 @@
android:layout_marginTop="8dp">
<ImageView
android:id="@+id/iconTrash"
android:id="@+id/automation_iconTrash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_trash_outline"
@ -22,32 +22,23 @@
android:contentDescription="@string/overview_quickwizard_item_remove_button" />
<LinearLayout
android:id="@+id/layoutText"
android:id="@+id/automation_layoutText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@id/iconTrash"
android:layout_toStartOf="@id/automation_iconTrash"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/viewActionTitle"
android:id="@+id/automation_viewActionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/viewActionDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:visibility="gone"/>
</LinearLayout>
</RelativeLayout>