Add PoC action support for Add Device button.

This commit is contained in:
Denis-Cosmin Nutiu 2020-12-11 20:58:47 +02:00
parent d87b748b64
commit 3cd90abdc9
6 changed files with 39 additions and 71 deletions

View file

@ -1,5 +1,7 @@
package dev.nuculabs.nucuhub.domain; package dev.nuculabs.nucuhub.domain;
import androidx.annotation.NonNull;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -55,4 +57,10 @@ public class Device {
"Malformed URL provided for device: %s", url)); "Malformed URL provided for device: %s", url));
} }
} }
@NonNull
@Override
public String toString() {
return this.target;
}
} }

View file

@ -24,9 +24,7 @@ public class SettingsFragment extends PreferenceFragmentCompat {
} }
private void initializeDeviceListItems() { private void initializeDeviceListItems() {
// TODO dummy code to update device list entries
SharedPreferences sp = Objects.requireNonNull(getContext()).getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE); SharedPreferences sp = Objects.requireNonNull(getContext()).getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor spe = sp.edit();
DeviceListPreference devicesList = findPreference(DEVICE_LIST_PREFERENCE); DeviceListPreference devicesList = findPreference(DEVICE_LIST_PREFERENCE);
Set<String> savedEntries = sp.getStringSet(SettingValues.CURRENT_DEVICE_LIST, null); Set<String> savedEntries = sp.getStringSet(SettingValues.CURRENT_DEVICE_LIST, null);
if (savedEntries != null) { if (savedEntries != null) {
@ -38,6 +36,6 @@ public class SettingsFragment extends PreferenceFragmentCompat {
} }
devicesList.setEntries(items); devicesList.setEntries(items);
} }
// TODO: Set default device.
} }
} }

View file

@ -10,19 +10,23 @@ import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView; import android.widget.ListView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar; import androidx.appcompat.widget.Toolbar;
import androidx.preference.ListPreference; import androidx.preference.ListPreference;
import com.google.android.material.textfield.TextInputLayout;
import dev.nuculabs.nucuhub.R; import dev.nuculabs.nucuhub.R;
import dev.nuculabs.nucuhub.domain.SettingValues; import dev.nuculabs.nucuhub.domain.SettingValues;
import java.util.HashSet; import java.util.HashSet;
import java.util.Objects;
public class DeviceManagementDialog extends Dialog { public class DeviceManagementDialog extends Dialog {
private SharedPreferences sharedPreferences; private SharedPreferences sharedPreferences;
private ListPreference preference = null; private ListPreference preference = null;
private ArrayAdapter<String> adapter; private ArrayAdapter<String> adapter;
private TextInputLayout deviceTextInputLayout;
public DeviceManagementDialog(@NonNull Context context) { public DeviceManagementDialog(@NonNull Context context) {
super(context, R.style.ThemeOverlay_AppCompat_ActionBar); super(context, R.style.ThemeOverlay_AppCompat_ActionBar);
@ -36,24 +40,22 @@ public class DeviceManagementDialog extends Dialog {
setupToolbar(); setupToolbar();
sharedPreferences = getContext().getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE); sharedPreferences = getContext().getSharedPreferences(SettingValues.NAME, Activity.MODE_PRIVATE);
final ListView deviceListView = requireViewById(R.id.settings_device_dialog_list);
final ListView deviceListView = findViewById(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); adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1);
deviceListView.setAdapter(adapter); 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(); final CharSequence [] preferenceCharSeq = preference.getEntries();
for (CharSequence item : preferenceCharSeq) { for (CharSequence item : preferenceCharSeq) {
adapter.add(item.toString()); adapter.add(item.toString());
} }
Button testConnectionButton = findViewById(R.id.settings_device_test_connection_button); // on-click handlers.
testConnectionButton.setOnClickListener(new View.OnClickListener() { addDeviceButton.setOnClickListener(new AddNewDeviceAction());
@Override // TODO add support for selecting device and deleting.
public void onClick(View v) {
adapter.add("new object.");
}
});
} }
@Override @Override
@ -84,7 +86,6 @@ public class DeviceManagementDialog extends Dialog {
} }
private void updatePreferenceEntryValues() { private void updatePreferenceEntryValues() {
// todo dummy code to update the entries.
int itemsLength = adapter.getCount(); int itemsLength = adapter.getCount();
CharSequence[] entries = new CharSequence[itemsLength]; CharSequence[] entries = new CharSequence[itemsLength];
HashSet<String> entriesSet = new HashSet<>(); HashSet<String> 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);
}
}
} }

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:orientation="vertical"
> >
@ -38,22 +38,12 @@
android:hint="@string/settings_device_add_new_device_input_hint"/> android:hint="@string/settings_device_add_new_device_input_hint"/>
</com.google.android.material.textfield.TextInputLayout> </com.google.android.material.textfield.TextInputLayout>
<TextView
android:text="$$Connection status$$"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/text_connection_status" android:gravity="center"/>
<Button <Button
android:text="@string/settings_device_test_connection_btn" android:text="@string/settings_device_test_connection_btn"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/settings_device_test_connection_button" android:layout_height="wrap_content" android:id="@+id/settings_device_add_button"
tools:text="Test Connection"/> tools:text="Test Connection"/>
<Button
android:text="@string/settings_device_add_device_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/settings_device_add_device_button"
android:enabled="false"/>
</LinearLayout> </LinearLayout>
<View android:layout_height="1dp" <View android:layout_height="1dp"

View file

@ -35,9 +35,8 @@
<string name="device_selection_title">Your current device</string> <string name="device_selection_title">Your current device</string>
<string name="device_management_title">Device Management</string> <string name="device_management_title">Device Management</string>
<string name="settings_connect_new_device">Connect new device</string> <string name="settings_connect_new_device">Connect new device</string>
<string name="settings_device_test_connection_btn">Test Connection</string> <string name="settings_device_test_connection_btn">Add Device</string>
<string name="settings_device_add_new_device_input_hint">http://hostname:port</string> <string name="settings_device_add_new_device_input_hint">hostname:port</string>
<string name="settings_device_add_device_btn">Add Device</string>
<string name="settings_device_saved_devices">Saved Devices</string> <string name="settings_device_saved_devices">Saved Devices</string>

View file

@ -1,43 +1,5 @@
<PreferenceScreen <PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
xmlns:app="http://schemas.android.com/apk/res-auto"> <PreferenceCategory app:title="@string/devices_header">
<!-- <PreferenceCategory-->
<!-- app:title="@string/messages_header">-->
<!-- <EditTextPreference-->
<!-- app:key="signature"-->
<!-- app:title="@string/signature_title"-->
<!-- app:useSimpleSummaryProvider="true"/>-->
<!-- <ListPreference-->
<!-- app:key="reply"-->
<!-- app:title="@string/reply_title"-->
<!-- app:entries="@array/reply_entries"-->
<!-- app:entryValues="@array/reply_values"-->
<!-- app:defaultValue="reply"-->
<!-- app:useSimpleSummaryProvider="true"/>-->
<!-- </PreferenceCategory>-->
<!-- <PreferenceCategory-->
<!-- app:title="@string/sync_header">-->
<!-- <SwitchPreferenceCompat-->
<!-- app:key="sync"-->
<!-- app:title="@string/sync_title"/>-->
<!-- <SwitchPreferenceCompat-->
<!-- app:key="attachment"-->
<!-- app:title="@string/attachment_title"-->
<!-- app:summaryOn="@string/attachment_summary_on"-->
<!-- app:summaryOff="@string/attachment_summary_off"-->
<!-- app:dependency="sync"/>-->
<!-- </PreferenceCategory>-->
<PreferenceCategory
app:title="@string/devices_header">
<dev.nuculabs.nucuhub.ui.settings.device.DeviceListPreference <dev.nuculabs.nucuhub.ui.settings.device.DeviceListPreference
app:key="device_list" app:key="device_list"
app:title="@string/device_selection_title" app:title="@string/device_selection_title"
@ -45,7 +7,5 @@
app:entryValues="@array/reply_values" app:entryValues="@array/reply_values"
app:defaultValue="reply" app:defaultValue="reply"
app:useSimpleSummaryProvider="true"/> app:useSimpleSummaryProvider="true"/>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>