diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java index 94f6aa5..50b6c0b 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceListAdapter.java @@ -1,10 +1,13 @@ package dev.nuculabs.nucuhub.ui.settings.device; import android.content.Context; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; +import android.widget.ImageButton; +import android.widget.LinearLayout; import android.widget.TextView; import dev.nuculabs.nucuhub.R; import dev.nuculabs.nucuhub.domain.Device; @@ -12,18 +15,23 @@ import dev.nuculabs.nucuhub.domain.Device; import java.util.ArrayList; public class DeviceListAdapter extends BaseAdapter { + private final String TAG = DeviceListAdapter.class.getName(); private final ArrayList items = new ArrayList<>(); - private final Context context; private final LayoutInflater inflater; + private final DeleteDeviceAction deleteDeviceAction; + private final StarDeviceAction starDeviceAction; + private final DeviceManagementDialog deviceManagementDialog; - public DeviceListAdapter(Context context) { - this.context = context; + public DeviceListAdapter(Context context, DeviceManagementDialog dmd) { this.inflater = LayoutInflater.from(context); + this.deleteDeviceAction = new DeleteDeviceAction(); + this.starDeviceAction = new StarDeviceAction(); + this.deviceManagementDialog = dmd; } - public DeviceListAdapter(ArrayList items, Context context) { - this(context); + public DeviceListAdapter(ArrayList items, Context context, DeviceManagementDialog dmd) { + this(context, dmd); this.items.addAll(items); } @@ -48,16 +56,50 @@ public class DeviceListAdapter extends BaseAdapter { if (view == null) { view = inflater.inflate(R.layout.settings_device_list_item, null); } - final TextView text = view.findViewById(R.id.settingsDeviceListDeviceListItemText); - text.setText(items.get(position).toString()); + final TextView text = view.findViewById(R.id.settings_device_list_item_text); + final TextView devicePosition = view.findViewById(R.id.settings_device_list_item_position); + final ImageButton deleteButton = view.findViewById(R.id.settings_device_list_item_delete_button); + final ImageButton starButton = view.findViewById(R.id.settings_device_list_item_select_button); - // TODO: Add click listeners for Delete And Select Buttons. - // TODO: Mark selected item row? + text.setText(items.get(position).toString()); + devicePosition.setText(Integer.toString(position)); + + deleteButton.setOnClickListener(deleteDeviceAction); + starButton.setOnClickListener(starDeviceAction); return view; } + @Override + public void notifyDataSetChanged() { + super.notifyDataSetChanged(); + deviceManagementDialog.updatePreferenceEntryValues(); + } + public boolean add(Device device) { + Log.d(TAG, "Adding item " + device.toString()); return items.add(device); } + + private class DeleteDeviceAction implements View.OnClickListener { + @Override + public void onClick(View v) { + View parentContainer = (View) v.getParent().getParent(); + TextView positionText = parentContainer.findViewById(R.id.settings_device_list_item_position); + items.remove(Integer.parseInt(positionText.getText().toString())); + notifyDataSetChanged(); + } + } + + private class StarDeviceAction implements View.OnClickListener { + private final String TAG = StarDeviceAction.class.getName(); + + @Override + public void onClick(View v) { + // TODO: Highlight selected item row? + LinearLayout parent = (LinearLayout) v.getParent().getParent(); + CharSequence text = ((TextView) parent.getChildAt(0)).getText(); + Log.i(TAG, "StarDeviceAction " + text.toString()); + } + } } diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java index d17d2d7..414b32c 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/device/DeviceManagementDialog.java @@ -45,7 +45,7 @@ public class DeviceManagementDialog extends Dialog { final ListView deviceListView = requireViewById(R.id.settings_device_dialog_list); final Button addDeviceButton = requireViewById(R.id.settings_device_add_button); deviceTextInputLayout = requireViewById(R.id.settings_device_input_device); - adapter = new DeviceListAdapter(getContext()); + adapter = new DeviceListAdapter(getContext(), this); deviceListView.setAdapter(adapter); @@ -87,7 +87,7 @@ public class DeviceManagementDialog extends Dialog { dismiss(); } - private void updatePreferenceEntryValues() { + public void updatePreferenceEntryValues() { int itemsLength = adapter.getCount(); CharSequence[] entries = new CharSequence[itemsLength]; HashSet entriesSet = new HashSet<>(); diff --git a/NucuHub.Android/app/src/main/res/layout/settings_device_list_item.xml b/NucuHub.Android/app/src/main/res/layout/settings_device_list_item.xml index a1720d3..e99f8e0 100644 --- a/NucuHub.Android/app/src/main/res/layout/settings_device_list_item.xml +++ b/NucuHub.Android/app/src/main/res/layout/settings_device_list_item.xml @@ -7,19 +7,23 @@ +