From 3cd90abdc91f5fb4bca03f1c920096342d47faa3 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Fri, 11 Dec 2020 20:58:47 +0200 Subject: [PATCH] Add PoC action support for Add Device button. --- .../dev/nuculabs/nucuhub/domain/Device.java | 8 ++++ .../nucuhub/ui/settings/SettingsFragment.java | 4 +- .../device/DeviceManagementDialog.java | 35 ++++++++++----- .../settings_device_management_dialog.xml | 14 +----- .../app/src/main/res/values/strings.xml | 5 +-- .../app/src/main/res/xml/root_preferences.xml | 44 +------------------ 6 files changed, 39 insertions(+), 71 deletions(-) diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java index 5804d84..5497e5f 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/domain/Device.java @@ -1,5 +1,7 @@ package dev.nuculabs.nucuhub.domain; +import androidx.annotation.NonNull; + import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -55,4 +57,10 @@ public class Device { "Malformed URL provided for device: %s", url)); } } + + @NonNull + @Override + public String toString() { + return this.target; + } } diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java index 409b58d..416e060 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/SettingsFragment.java @@ -24,9 +24,7 @@ public class SettingsFragment extends PreferenceFragmentCompat { } private void initializeDeviceListItems() { - // TODO dummy code to update device list entries SharedPreferences sp = Objects.requireNonNull(getContext()).getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE); - SharedPreferences.Editor spe = sp.edit(); DeviceListPreference devicesList = findPreference(DEVICE_LIST_PREFERENCE); Set savedEntries = sp.getStringSet(SettingValues.CURRENT_DEVICE_LIST, null); if (savedEntries != null) { @@ -38,6 +36,6 @@ public class SettingsFragment extends PreferenceFragmentCompat { } devicesList.setEntries(items); } - + // TODO: Set default device. } } 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 f711046..799b099 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 @@ -10,19 +10,23 @@ import android.view.View; import android.view.WindowManager; import android.widget.ArrayAdapter; import android.widget.Button; +import android.widget.EditText; import android.widget.ListView; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; import androidx.preference.ListPreference; +import com.google.android.material.textfield.TextInputLayout; import dev.nuculabs.nucuhub.R; import dev.nuculabs.nucuhub.domain.SettingValues; import java.util.HashSet; +import java.util.Objects; public class DeviceManagementDialog extends Dialog { private SharedPreferences sharedPreferences; private ListPreference preference = null; private ArrayAdapter adapter; + private TextInputLayout deviceTextInputLayout; public DeviceManagementDialog(@NonNull Context context) { super(context, R.style.ThemeOverlay_AppCompat_ActionBar); @@ -36,24 +40,22 @@ public class DeviceManagementDialog extends Dialog { setupToolbar(); sharedPreferences = getContext().getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE); - - final ListView deviceListView = findViewById(R.id.settings_device_dialog_list); + 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 ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1); + deviceListView.setAdapter(adapter); - // todo: dummy code to add simple items. + // Add the existing items in the list adapter so they will be displayed. final CharSequence [] preferenceCharSeq = preference.getEntries(); for (CharSequence item : preferenceCharSeq) { adapter.add(item.toString()); } - Button testConnectionButton = findViewById(R.id.settings_device_test_connection_button); - testConnectionButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - adapter.add("new object."); - } - }); + // on-click handlers. + addDeviceButton.setOnClickListener(new AddNewDeviceAction()); + // TODO add support for selecting device and deleting. } @Override @@ -84,7 +86,6 @@ public class DeviceManagementDialog extends Dialog { } private void updatePreferenceEntryValues() { - // todo dummy code to update the entries. int itemsLength = adapter.getCount(); CharSequence[] entries = new CharSequence[itemsLength]; HashSet entriesSet = new HashSet<>(); @@ -109,4 +110,16 @@ public class DeviceManagementDialog extends Dialog { } }); } + + private class AddNewDeviceAction implements View.OnClickListener { + + @Override + public void onClick(View v) { + // TODO: Test connection before adding. loading -> testing connection -> show dialog. + EditText editText = Objects.requireNonNull(deviceTextInputLayout.getEditText()); + String target = editText.getText().toString(); + adapter.add(target); + editText.setText(null); + } + } } diff --git a/NucuHub.Android/app/src/main/res/layout/settings_device_management_dialog.xml b/NucuHub.Android/app/src/main/res/layout/settings_device_management_dialog.xml index 822aca4..6a5dbfa 100644 --- a/NucuHub.Android/app/src/main/res/layout/settings_device_management_dialog.xml +++ b/NucuHub.Android/app/src/main/res/layout/settings_device_management_dialog.xml @@ -1,6 +1,6 @@ @@ -38,22 +38,12 @@ android:hint="@string/settings_device_add_new_device_input_hint"/> -