Implement custom list adapter for list view.

This commit is contained in:
Denis-Cosmin Nutiu 2020-12-12 19:15:39 +02:00
parent 588973e0ad
commit 67ea7abde4
6 changed files with 98 additions and 59 deletions

View file

@ -2,15 +2,15 @@ package dev.nuculabs.nucuhub.domain;
import androidx.annotation.NonNull;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Device represents a NucuHub device.
*/
public class Device {
private String target;
private URL target;
public Device() {
}
@ -21,46 +21,37 @@ public class Device {
* @param url Url of the form http://localhost:port
*/
public Device(String url) {
validateUrlAgainstRegex(url);
target = url;
stringToURL(url);
}
public Device(String host, int port) {
String temp = String.format(Locale.ENGLISH, "%s:%d", host, port);
validateUrlAgainstRegex(temp);
target = temp;
stringToURL(temp);
}
public String getTarget() {
return target;
return target.toString();
}
public void setTarget(String target) {
validateUrlAgainstRegex(target);
this.target = target;
stringToURL(target);
}
public boolean testConnection() {
return true;
}
private void validateUrlAgainstRegex(String url) {
String urlValidationRegex = "^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?";
Pattern p = Pattern.compile(urlValidationRegex);
Matcher m = p.matcher(url);
if (m.matches()) {
if (url.contains("http://") || url.contains("https://")) {
throw new IllegalArgumentException("Don't include schema with URL");
}
} else {
throw new IllegalArgumentException(String.format(Locale.ENGLISH,
"Malformed URL provided for device: %s", url));
private void stringToURL(String url) {
try {
target = new URL(url);
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
@NonNull
@Override
public String toString() {
return this.target;
return this.target.toString();
}
}

View file

@ -0,0 +1,61 @@
package dev.nuculabs.nucuhub.ui.settings.device;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import dev.nuculabs.nucuhub.domain.Device;
import java.util.ArrayList;
public class DeviceListAdapter extends BaseAdapter {
private final ArrayList<Device> items = new ArrayList<>();
private final Context context;
private final LayoutInflater inflater;
public DeviceListAdapter(Context context) {
this.context = context;
this.inflater = LayoutInflater.from(context);
}
public DeviceListAdapter(ArrayList<Device> items, Context context) {
this(context);
this.items.addAll(items);
}
@Override
public int getCount() {
return items.size();
}
@Override
public Device getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return items.get(position).hashCode();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final TextView text;
if (view == null) {
view = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
text = (TextView) view;
text.setText(items.get(position).toString());
return view;
}
public boolean add(Device device) {
return items.add(device);
}
}

View file

@ -5,10 +5,10 @@ import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
@ -17,15 +17,17 @@ 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.Device;
import dev.nuculabs.nucuhub.domain.SettingValues;
import java.util.HashSet;
import java.util.Objects;
public class DeviceManagementDialog extends Dialog {
private final String TAG = DeviceManagementDialog.class.getName();
private SharedPreferences sharedPreferences;
private ListPreference preference = null;
private ArrayAdapter<String> adapter;
private DeviceListAdapter adapter;
private TextInputLayout deviceTextInputLayout;
public DeviceManagementDialog(@NonNull Context context) {
@ -43,14 +45,14 @@ 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 ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1);
adapter = new DeviceListAdapter(getContext());
deviceListView.setAdapter(adapter);
// 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());
adapter.add(new Device(item.toString()));
}
// on-click handlers.
@ -90,7 +92,7 @@ public class DeviceManagementDialog extends Dialog {
CharSequence[] entries = new CharSequence[itemsLength];
HashSet<String> entriesSet = new HashSet<>();
for (int i = 0; i < itemsLength; i++) {
entries[i] = adapter.getItem(i);
entries[i] = adapter.getItem(i).toString();
entriesSet.add(entries[i].toString());
}
preference.setEntries(entries);
@ -117,10 +119,15 @@ public class DeviceManagementDialog extends Dialog {
@Override
public void onClick(View v) {
// TODO: Test connection before adding. loading -> testing connection -> show dialog.
try {
EditText editText = Objects.requireNonNull(deviceTextInputLayout.getEditText());
String target = editText.getText().toString();
adapter.add(target);
editText.setText(null);
adapter.add(new Device(target));
editText.setText("http://");
} catch (IllegalArgumentException e) {
Log.e(TAG, e.getMessage());
}
}
}
}

View file

@ -4,12 +4,12 @@
<!-- Reply Preference -->
<string-array name="reply_entries">
<item>Reply</item>
<item>Reply to all</item>
<item>http://localhost:9090/</item>
<item>http://google.com/</item>
</string-array>
<string-array name="reply_values">
<item>reply</item>
<item>reply_all</item>
<item>http://localhost:9090/</item>
<item>http://google.com/</item>
</string-array>
</resources>

View file

@ -36,7 +36,7 @@
<string name="device_management_title">Device Management</string>
<string name="settings_connect_new_device">Connect new device</string>
<string name="settings_device_test_connection_btn">Add Device</string>
<string name="settings_device_add_new_device_input_hint">hostname:port</string>
<string name="settings_device_add_new_device_input_hint">http://hostname:port</string>
<string name="settings_device_saved_devices">Saved Devices</string>

View file

@ -11,7 +11,7 @@ import static org.junit.Assert.*;
public class DeviceTest {
@Test
public void test_constructionValidTargets() {
String[] testCases = {"localhost:8900/cool", "nuculabs.dev", "www.nuculabs.dev/", "user:pass@nuculabs.dev"};
String[] testCases = {"http://localhost:8900/cool", "http://nuculabs.dev", "http://www.nuculabs.dev/", "http://user:pass@nuculabs.dev"};
for (String target : testCases) {
Device device = new Device(target);
assertEquals(device.getTarget(), target);
@ -21,31 +21,11 @@ public class DeviceTest {
@Test
public void test_constructionValidHostsAndPorts() {
Random random = new Random();
String[] testCases = {"localhost", "nuculabs.dev", "www.nuculabs.dev", "user:pass@nuculabs.dev"};
String[] testCases = {"http://localhost", "http://nuculabs.dev", "http://www.nuculabs.dev", "http://user:pass@nuculabs.dev"};
for (String target : testCases) {
int port = random.nextInt(65535);
Device device = new Device(target, port);
assertEquals(device.getTarget(), String.format(Locale.ENGLISH, "%s:%d", target, port));
}
}
@Test(expected = IllegalArgumentException.class)
public void test_constructionInvalidHostAndPortHttps() {
new Device("https://google.com", 443);
}
@Test(expected = IllegalArgumentException.class)
public void test_constructionInvalidHostAndPortHttp() {
new Device("http://google.com", 443);
}
@Test(expected = IllegalArgumentException.class)
public void test_constructionInvalidTargetHttp() {
new Device("http://localhost:8900/cool");
}
@Test(expected = IllegalArgumentException.class)
public void test_constructionInvalidTargetHttps() {
new Device("https://nuculabs.dev");
}
}