diff --git a/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java b/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java index b59e533f..4feef378 100644 --- a/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java +++ b/src/main/java/com/nutomic/syncthingandroid/activities/FolderActivity.java @@ -22,7 +22,6 @@ import android.widget.TextView; import android.widget.Toast; import com.google.common.base.Objects; -import com.google.common.collect.ImmutableList; import com.google.gson.Gson; import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.fragments.dialog.KeepVersionsDialogFragment; @@ -96,14 +95,12 @@ public class FolderActivity extends SyncthingActivity mFolderNeedsToUpdate = true; break; case R.id.device_toggle: - List devicesList = mFolder.getDevices(); Device device = (Device) view.getTag(); if (isChecked) { - devicesList.add(device.deviceID); + mFolder.addDevice(device.deviceID); } else { - devicesList.remove(device.deviceID); + mFolder.removeDevice(device.deviceID); } - mFolder.setDevices(devicesList); mFolderNeedsToUpdate = true; break; } @@ -339,11 +336,7 @@ public class FolderActivity extends SyncthingActivity mFolder.versioning = new Folder.Versioning(); String deviceId = getIntent().getStringExtra(EXTRA_DEVICE_ID); if (deviceId != null) { - List devices = ImmutableList.builder() - .addAll(mFolder.getDevices()) - .add(deviceId) - .build(); - mFolder.setDevices(devices); + mFolder.addDevice(deviceId); } } @@ -370,7 +363,7 @@ public class FolderActivity extends SyncthingActivity private void addDeviceViewAndSetListener(Device device, LayoutInflater inflater) { inflater.inflate(R.layout.item_device_form, mDevicesContainer); SwitchCompat deviceView = (SwitchCompat) mDevicesContainer.getChildAt(mDevicesContainer.getChildCount()-1); - deviceView.setChecked(mFolder.getDevices().contains(device.deviceID)); + deviceView.setChecked(mFolder.getDevice(device.deviceID) != null); deviceView.setText(device.getDisplayName()); deviceView.setTag(device); deviceView.setOnCheckedChangeListener(mCheckedListener); diff --git a/src/main/java/com/nutomic/syncthingandroid/model/Folder.java b/src/main/java/com/nutomic/syncthingandroid/model/Folder.java index 56c1152e..30484f8b 100644 --- a/src/main/java/com/nutomic/syncthingandroid/model/Folder.java +++ b/src/main/java/com/nutomic/syncthingandroid/model/Folder.java @@ -2,7 +2,7 @@ package com.nutomic.syncthingandroid.model; import java.io.Serializable; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; @@ -12,7 +12,7 @@ public class Folder { public String label; public String path; public String type; - private transient List> devices = new ArrayList<>(); + public List devices = new ArrayList<>(); public int rescanIntervalS; public boolean ignorePerms; public boolean autoNormalize; @@ -36,23 +36,27 @@ public class Folder { public Map params; } - public List getDevices() { - if (devices == null) - return new ArrayList<>(); - - List devicesList = new ArrayList<>(); - for (Map map : devices) { - devicesList.addAll(map.values()); - } - return devicesList; + public void addDevice(String deviceId) { + Device d = new Device(); + d.deviceID = deviceId; + devices.add(d); } - public void setDevices(List newDevices) { - devices.clear(); - for (String d : newDevices) { - Map map = new HashMap<>(); - map.put("deviceID", d); - devices.add(map); + public Device getDevice(String deviceId) { + for (Device d : devices) { + if (d.deviceID.equals(deviceId)) { + return d; + } + } + return null; + } + + public void removeDevice(String deviceId) { + for (Iterator it = devices.iterator(); it.hasNext();) { + String currentId = it.next().deviceID; + if (currentId.equals(deviceId)) { + it.remove(); + } } } } diff --git a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java index 498c1b9d..ccc06583 100644 --- a/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/service/RestApi.java @@ -371,13 +371,10 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, int maxPercentage = 100; for (Map.Entry modelInfo : mCachedModelInfo.entrySet()) { boolean isShared = false; - outerloop: for (Folder r : getFolders()) { - for (String n : r.getDevices()) { - if (n.equals(deviceId)) { - isShared = true; - break outerloop; - } + if (r.getDevice(deviceId) != null) { + isShared = true; + break; } } if (isShared) {