summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2021-03-26 15:45:44 +0100
committerJavier <dev.git@javispedro.com>2021-03-26 15:45:44 +0100
commit96577161813cacab5e47975942324feb92166e7c (patch)
treeb5f0455373fa836ad262fbf405fb9a9e7d2068af
parent4fafdb37fb23d12469cc1ac4b23efe9583f87407 (diff)
downloadrempe-96577161813cacab5e47975942324feb92166e7c.tar.gz
rempe-96577161813cacab5e47975942324feb92166e7c.zip
Support device removal by swiping left/right
-rw-r--r--app/src/main/java/com/javispedro/rempe/Device.java15
-rw-r--r--app/src/main/java/com/javispedro/rempe/MainActivity.java64
-rw-r--r--app/src/main/res/menu/menu_main.xml7
-rw-r--r--app/src/main/res/values/strings.xml6
4 files changed, 62 insertions, 30 deletions
diff --git a/app/src/main/java/com/javispedro/rempe/Device.java b/app/src/main/java/com/javispedro/rempe/Device.java
index d3a0d05..fed7291 100644
--- a/app/src/main/java/com/javispedro/rempe/Device.java
+++ b/app/src/main/java/com/javispedro/rempe/Device.java
@@ -59,13 +59,16 @@ public class Device {
public void close() {
Log.d(TAG, "close (" + mDeviceNumber + ")");
+ if (mEnvPcc != null) {
+ mEnvPcc.subscribeTemperatureDataEvent(null);
+ mEnvPcc.subscribeRssiEvent(null);
+ mEnvPcc.releaseAccess();
+ mEnvPcc = null;
+ }
if (mEnvPccHandle != null) {
mEnvPccHandle.close();
mEnvPccHandle = null;
}
- if (mEnvPcc != null) {
- mEnvPcc = null;
- }
mConnectResult = RequestAccessResult.SUCCESS;
mCurState = DeviceState.DEAD;
if (mObserver != null) {
@@ -88,6 +91,8 @@ public class Device {
private void setEnvPcc(AntPlusEnvironmentPcc envPcc) {
if (mEnvPcc != null) {
+ mEnvPcc.subscribeTemperatureDataEvent(null);
+ mEnvPcc.subscribeRssiEvent(null);
mEnvPcc.releaseAccess();
mEnvPcc = null;
}
@@ -130,7 +135,7 @@ public class Device {
return mConnectResult;
}
- private AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
+ private final AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
@Override
public void onResultReceived(AntPlusEnvironmentPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
Log.d(TAG, "onResultReceived resultCode=" + resultCode + " initialDeviceState=" + initialDeviceState);
@@ -149,7 +154,7 @@ public class Device {
}
};
- private AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
+ private final AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
@Override
public void onDeviceStateChange(DeviceState newDeviceState) {
Log.d(TAG, "onDeviceStateChange newDeviceState=" + newDeviceState);
diff --git a/app/src/main/java/com/javispedro/rempe/MainActivity.java b/app/src/main/java/com/javispedro/rempe/MainActivity.java
index 2c29bc4..e4e6d6f 100644
--- a/app/src/main/java/com/javispedro/rempe/MainActivity.java
+++ b/app/src/main/java/com/javispedro/rempe/MainActivity.java
@@ -6,11 +6,13 @@ import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
+import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.ListUpdateCallback;
import androidx.recyclerview.widget.RecyclerView;
@@ -31,16 +33,6 @@ public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
private SharedPreferences mPrefs = null;
- private SharedPreferences.OnSharedPreferenceChangeListener mPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
- @Override
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
- switch (key) {
- case Preferences.PREFS_DEVICES:
- refreshDevices();
- break;
- }
- }
- };
private final ArrayList<Integer> mDeviceNumbers = new ArrayList<Integer>();
private final ArrayList<Device> mDevices = new ArrayList<Device>();
@@ -72,6 +64,8 @@ public class MainActivity extends AppCompatActivity {
mDeviceListAdapter = new DeviceListRecyclerViewListAdapter();
mList.setAdapter(mDeviceListAdapter);
+ new ItemTouchHelper(mItemTouchHelperCallback).attachToRecyclerView(mList);
+
refreshDevices();
}
@@ -113,9 +107,6 @@ public class MainActivity extends AppCompatActivity {
int id = item.getItemId();
switch (id) {
- case R.id.action_remove_all:
- removeAllDevices();
- return true;
case R.id.action_settings:
return true;
}
@@ -132,6 +123,35 @@ public class MainActivity extends AppCompatActivity {
mSrlList.setRefreshing(false);
}
+ private final ItemTouchHelper.Callback mItemTouchHelperCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
+ @Override
+ public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
+ final int from = viewHolder.getAdapterPosition();
+ final int to = viewHolder.getAdapterPosition();
+
+ Log.d(TAG, "onMove from=" + from + " to=" + to);
+
+ return false;
+ }
+
+ @Override
+ public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
+ final int position = viewHolder.getAdapterPosition();
+ removeDeviceByPosition(position);
+ }
+ };
+
+ private final SharedPreferences.OnSharedPreferenceChangeListener mPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+ switch (key) {
+ case Preferences.PREFS_DEVICES:
+ refreshDevices();
+ break;
+ }
+ }
+ };
+
public void searchForNewDevice() {
Log.d(TAG, "searchForNewDevice");
if (mPccSearchHandle != null) {
@@ -141,7 +161,7 @@ public class MainActivity extends AppCompatActivity {
mPccSearchHandle = AntPlusEnvironmentPcc.requestAccess(this, this, mResultReceiver, mDeviceStateChangeReceiver);
}
- private AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
+ private final AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc> mResultReceiver = new AntPluginPcc.IPluginAccessResultReceiver<AntPlusEnvironmentPcc>() {
@Override
public void onResultReceived(AntPlusEnvironmentPcc result, RequestAccessResult resultCode, DeviceState initialDeviceState) {
Log.d(TAG, "onResultReceived resultCode=" + resultCode);
@@ -161,7 +181,7 @@ public class MainActivity extends AppCompatActivity {
}
};
- private AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
+ private final AntPluginPcc.IDeviceStateChangeReceiver mDeviceStateChangeReceiver = new AntPluginPcc.IDeviceStateChangeReceiver() {
@Override
public void onDeviceStateChange(DeviceState newDeviceState) {
Log.d(TAG, "onDeviceStateChange newDeviceState=" + newDeviceState);
@@ -180,8 +200,18 @@ public class MainActivity extends AppCompatActivity {
Preferences.saveDeviceNumbers(mPrefs, list);
}
- public void removeAllDevices() {
- List<Integer> list = new ArrayList<Integer>();
+ public void removeDeviceByPosition(int position) {
+ Log.d(TAG, "removeDeviceByPosition position=" + position);
+ List<Integer> list = Preferences.getDeviceNumbers(mPrefs);
+ try {
+ list.remove(position);
+ } catch (IndexOutOfBoundsException ex) {
+ Log.w(TAG, ex);
+ }
+
+ Snackbar.make(mList,
+ getString(R.string.remove_device_done), Snackbar.LENGTH_SHORT).show();
+
Preferences.saveDeviceNumbers(mPrefs, list);
}
diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml
index 5320159..b11af65 100644
--- a/app/src/main/res/menu/menu_main.xml
+++ b/app/src/main/res/menu/menu_main.xml
@@ -2,13 +2,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.javispedro.rempe.MainActivity">
- <item
- android:id="@+id/action_remove_all"
- android:orderInCategory="100"
- android:title="Remove all" />
+
<item
android:id="@+id/action_settings"
- android:orderInCategory="101"
+ android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu> \ No newline at end of file
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 7db9d07..c6832b0 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -1,16 +1,16 @@
<resources>
<string name="app_name">Rempe</string>
- <string name="action_remove_all">Remove all</string>
<string name="action_settings">Settings</string>
- <string name="fab_add_device">Add device...</string>
+ <string name="fab_add_device">Add device…</string>
<string name="add_device_failed">Cannot connect to device: %1$s</string>
<string name="add_device_already">Device already on list</string>
+ <string name="remove_device_done">Device removed</string>
<string name="device_placeholder_name">Device</string>
<string name="device_placeholder_status">Status</string>
- <string name="temperature_nothing">--</string>
+ <string name="temperature_nothing">—</string>
<string name="temperature_celsius">%1$s °C</string>
<string name="last_24h">Last 24h:</string>