From 058d91d0a9ca7dbf236e01dfd00c443af3f07da3 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 6 Dec 2020 19:15:43 +0200 Subject: [PATCH] Add PoC code for DeviceListManagement --- .../nuculabs/nucuhub/SettingsActivity.java | 2 +- .../ui/settings/DeviceListPreference.java | 6 +- .../ui/settings/DeviceManagementDialog.java | 111 ++++++++++++++++++ .../nucuhub/ui/settings/SettingsFragment.java | 30 ++++- .../drawable/ic_baseline_arrow_gray_24.xml | 5 + .../settings_device_management_dialog.xml | 78 ++++++++++++ .../app/src/main/res/values/strings.xml | 6 + .../app/src/main/res/xml/root_preferences.xml | 5 +- 8 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java create mode 100644 NucuHub.Android/app/src/main/res/drawable/ic_baseline_arrow_gray_24.xml create mode 100644 NucuHub.Android/app/src/main/res/layout/settings_device_management_dialog.xml diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/SettingsActivity.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/SettingsActivity.java index 02ead71..711887a 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/SettingsActivity.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/SettingsActivity.java @@ -39,7 +39,7 @@ public class SettingsActivity extends AppCompatActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { // back button click - if (item.getItemId()==android.R.id.home) { + if (item.getItemId() == android.R.id.home) { finish(); return true; } diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java index dbe82af..61368bc 100644 --- a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceListPreference.java @@ -20,5 +20,9 @@ public class DeviceListPreference extends ListPreference { public DeviceListPreference(Context context) { super(context); } - // TODO: Create custom dialog, override onClick. + + @Override + protected void onClick() { + new DeviceManagementDialog(getContext()).show(this); + } } diff --git a/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java new file mode 100644 index 0000000..40b5a17 --- /dev/null +++ b/NucuHub.Android/app/src/main/java/dev/nuculabs/nucuhub/ui/settings/DeviceManagementDialog.java @@ -0,0 +1,111 @@ +package dev.nuculabs.nucuhub.ui.settings; + +import android.app.Activity; +import android.app.Dialog; +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.MenuItem; +import android.view.View; +import android.view.WindowManager; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.preference.ListPreference; +import dev.nuculabs.nucuhub.R; + +import java.util.HashSet; + +public class DeviceManagementDialog extends Dialog { + private SharedPreferences sharedPreferences; + private ListPreference preference = null; + private ArrayAdapter adapter; + + public DeviceManagementDialog(@NonNull Context context) { + super(context, R.style.ThemeOverlay_AppCompat_ActionBar); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.settings_device_management_dialog); + getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT); + setupToolbar(); + + sharedPreferences = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE); + + final ListView deviceListView = findViewById(R.id.settings_device_dialog_list); + adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1); + deviceListView.setAdapter(adapter); + + // dummy code to add simple items. + 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."); + } + }); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // back button click + if (item.getItemId() == android.R.id.home) { + saveChangesAndDismiss(); + return true; + } + + return super.onOptionsItemSelected(item); + } + + @Override + public void onBackPressed() { + saveChangesAndDismiss(); + super.onBackPressed(); + } + + public void show(@NonNull ListPreference preference) { + this.preference = preference; + super.show(); + } + + public void saveChangesAndDismiss() { + updatePreferenceEntryValues(); + dismiss(); + } + + private void updatePreferenceEntryValues() { + // dummy code to update the entries. + int itemsLength = adapter.getCount(); + CharSequence[] entries = new CharSequence[itemsLength]; + HashSet entriesSet = new HashSet<>(); + for (int i = 0; i < itemsLength; i++) { + entries[i] = adapter.getItem(i); + entriesSet.add(entries[i].toString()); + } + preference.setEntries(entries); + + SharedPreferences.Editor spe = sharedPreferences.edit(); + spe.putStringSet("current_device_list", entriesSet); + } + + private void setupToolbar() { + Toolbar toolbar = findViewById(R.id.settings_device_dialog_toolbar); + toolbar.setTitle(R.string.device_management_title); + toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_gray_24); + toolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + saveChangesAndDismiss(); + } + }); + } +} 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 10ed022..4d89157 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 @@ -1,21 +1,41 @@ package dev.nuculabs.nucuhub.ui.settings; +import android.app.Activity; +import android.content.SharedPreferences; import android.os.Bundle; -import androidx.preference.ListPreference; import androidx.preference.PreferenceFragmentCompat; import dev.nuculabs.nucuhub.R; +import java.util.Set; + public class SettingsFragment extends PreferenceFragmentCompat { + private SharedPreferences sharedPreferences; + + @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { // To get preferences use this in main activity: // SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); setPreferencesFromResource(R.xml.root_preferences, rootKey); - ListPreference devicesList = findPreference("device_list"); + updateDeviceListItems(); + } + + private void updateDeviceListItems() { + // dummy code to update device list entries + SharedPreferences sp = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE); + SharedPreferences.Editor spe = sp.edit(); + DeviceListPreference devicesList = findPreference("device_list"); + Set savedEntries = sp.getStringSet("current_device_list", null); + if (savedEntries != null) { + CharSequence[] items = new CharSequence[savedEntries.size()]; + int index = 0; + for (String item : savedEntries) { + items[index] = item; + index += 1; + } + devicesList.setEntries(items); + } - // TODO: Override dialog in order to add a search devices button. - // - // set default value } } diff --git a/NucuHub.Android/app/src/main/res/drawable/ic_baseline_arrow_gray_24.xml b/NucuHub.Android/app/src/main/res/drawable/ic_baseline_arrow_gray_24.xml new file mode 100644 index 0000000..3502df5 --- /dev/null +++ b/NucuHub.Android/app/src/main/res/drawable/ic_baseline_arrow_gray_24.xml @@ -0,0 +1,5 @@ + + + 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 new file mode 100644 index 0000000..822aca4 --- /dev/null +++ b/NucuHub.Android/app/src/main/res/layout/settings_device_management_dialog.xml @@ -0,0 +1,78 @@ + + + + + + + + + + + + + + + + +