From f60e591a905fca91d89413720a788ac106317ebc Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 30 Oct 2016 16:34:12 +0900 Subject: [PATCH] Fixed adding/editing folders and devices --- .../fragments/DeviceListFragment.java | 6 +++-- .../fragments/FolderFragment.java | 7 +++-- .../fragments/FolderListFragment.java | 6 +++-- .../syncthingandroid/model/Folder.java | 6 ++--- .../syncthingandroid/syncthing/RestApi.java | 26 +++++++++++++++---- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java index 329fc327..c3c953fc 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java @@ -50,6 +50,9 @@ public class DeviceListFragment extends ListFragment implements SyncthingService mTimer.schedule(new TimerTask() { @Override public void run() { + if (getActivity() == null) + return; + getActivity().runOnUiThread(DeviceListFragment.this::updateList); } @@ -72,8 +75,7 @@ public class DeviceListFragment extends ListFragment implements SyncthingService */ private void updateList() { SyncthingActivity activity = (SyncthingActivity) getActivity(); - if (activity == null || activity.getApi() == null || getView() == null || - activity.isFinishing()) + if (activity.getApi() == null || getView() == null || activity.isFinishing()) return; if (mAdapter == null) { diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java index 97eb3129..b67adc45 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderFragment.java @@ -24,6 +24,7 @@ import android.widget.LinearLayout; 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; @@ -272,7 +273,7 @@ public class FolderFragment extends Fragment mLabelView.setText(mFolder.label); mIdView.setText(mFolder.id); mPathView.setText(mFolder.path); - mFolderMasterView.setChecked(mFolder.type.equals("readonly")); + mFolderMasterView.setChecked(Objects.equal(mFolder.type, "readonly")); List devicesList = mSyncthingService.getApi().getDevices(false); mDevicesContainer.removeAllViews(); @@ -284,7 +285,7 @@ public class FolderFragment extends Fragment } } - boolean versioningEnabled = mFolder.versioning.type.equals("simple"); + boolean versioningEnabled = Objects.equal(mFolder.versioning.type, "simple"); int versions = 0; if (versioningEnabled) { versions = Integer.valueOf(mFolder.versioning.params.get("keep")); @@ -360,9 +361,7 @@ public class FolderFragment extends Fragment mFolder = new Folder(); mFolder.id = getActivity().getIntent().getStringExtra(EXTRA_FOLDER_ID); mFolder.label = getActivity().getIntent().getStringExtra(EXTRA_FOLDER_LABEL); - mFolder.path = ""; mFolder.rescanIntervalS = 259200; // Scan every 3 days (in case inotify dropped some changes) - mFolder.setDevices(new ArrayList<>()); mFolder.versioning = new Folder.Versioning(); String deviceId = getActivity().getIntent().getStringExtra(EXTRA_DEVICE_ID); if (deviceId != null) { diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java index a2f98675..044e92fb 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java @@ -58,6 +58,9 @@ public class FolderListFragment extends ListFragment implements SyncthingService mTimer.schedule(new TimerTask() { @Override public void run() { + if (getActivity() == null) + return; + getActivity().runOnUiThread(FolderListFragment.this::updateList); } @@ -81,8 +84,7 @@ public class FolderListFragment extends ListFragment implements SyncthingService */ private void updateList() { SyncthingActivity activity = (SyncthingActivity) getActivity(); - if (activity == null || activity.getApi() == null || getView() == null || - activity.isFinishing()) + if (activity.getApi() == null || getView() == null || activity.isFinishing()) return; if (mAdapter == null) { diff --git a/src/main/java/com/nutomic/syncthingandroid/model/Folder.java b/src/main/java/com/nutomic/syncthingandroid/model/Folder.java index 39247b32..56c1152e 100644 --- a/src/main/java/com/nutomic/syncthingandroid/model/Folder.java +++ b/src/main/java/com/nutomic/syncthingandroid/model/Folder.java @@ -12,7 +12,7 @@ public class Folder { public String label; public String path; public String type; - private transient List> devices; + private transient List> devices = new ArrayList<>(); public int rescanIntervalS; public boolean ignorePerms; public boolean autoNormalize; @@ -47,9 +47,9 @@ public class Folder { return devicesList; } - public void setDevices(List newDvices) { + public void setDevices(List newDevices) { devices.clear(); - for (String d : newDvices) { + for (String d : newDevices) { Map map = new HashMap<>(); map.put("deviceID", d); devices.add(map); diff --git a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java index 6e02491e..ea7dcaf1 100644 --- a/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java +++ b/src/main/java/com/nutomic/syncthingandroid/syncthing/RestApi.java @@ -174,14 +174,18 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, */ public void requireRestart(Activity activity) { if (mRestartPostponed) { - new PostTask(mUrl, PostTask.URI_CONFIG, mHttpsCertPath, mApiKey, null) - .execute(mConfig.toString()); + sendConfig(); } else { activity.startActivity(new Intent(mContext, RestartActivity.class)); } mOnConfigChangedListener.onConfigChanged(); } + private void sendConfig() { + new PostTask(mUrl, PostTask.URI_CONFIG, mHttpsCertPath, mApiKey, null) + .execute(new Gson().toJson(mConfig)); + } + /** * Immediately restarts Syncthing, without confirmation. */ @@ -190,7 +194,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, Intent intent = new Intent(mContext, SyncthingService.class) .setAction(SyncthingService.ACTION_RESTART); mContext.startService(intent); - }).execute(mConfig.toString()); + }).execute(new Gson().toJson(mConfig)); } /** @@ -230,6 +234,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } public void removeDevice(String deviceId) { + removeDeviceInternal(deviceId); + sendConfig(); + } + + private void removeDeviceInternal(String deviceId) { Iterator it = mConfig.devices.iterator(); while (it.hasNext()) { Device d = it.next(); @@ -240,6 +249,11 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, } public void removeFolder(String id) { + removeFolderInternal(id); + sendConfig(); + } + + private void removeFolderInternal(String id) { Iterator it = mConfig.folders.iterator(); while (it.hasNext()) { Folder f = it.next(); @@ -253,6 +267,7 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, normalizeDeviceId(device.deviceID, ((normalizedId, error) -> { if (error == null) { mConfig.devices.add(device); + sendConfig(); } else { listener.onDeviceIdNormalized(normalizedId, error); @@ -262,15 +277,16 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener, public void addFolder(Folder folder) { mConfig.folders.add(folder); + sendConfig(); } public void editDevice(Device newDevice) { - removeDevice(newDevice.deviceID); + removeDeviceInternal(newDevice.deviceID); addDevice(newDevice, null); } public void editFolder(Folder newFolder) { - removeFolder(newFolder.id); + removeFolderInternal(newFolder.id); addFolder(newFolder); }