From 63a7342bfcb29d80657c93ff066c630218f17287 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 25 Feb 2016 12:47:16 +0100 Subject: [PATCH] Fixed loading of folder and device list items (fixes #556, ref #74). --- .../fragments/DeviceListFragment.java | 81 ++++++++++--------- .../fragments/FolderListFragment.java | 72 +++++++++-------- .../syncthingandroid/util/DevicesAdapter.java | 7 +- .../syncthingandroid/util/FoldersAdapter.java | 15 ++-- 4 files changed, 89 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java index a6f3be63..301eeb40 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/DeviceListFragment.java @@ -33,7 +33,25 @@ public class DeviceListFragment extends ListFragment implements SyncthingService @Override public void onResume() { super.onResume(); - setListShown(true); + mTimer = new Timer(); + mTimer.schedule(new TimerTask() { + @Override + public void run() { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + updateList(); + } + }); + } + + }, 0, SyncthingService.GUI_UPDATE_INTERVAL); + } + + @Override + public void onPause() { + super.onPause(); + mTimer.cancel(); } @Override @@ -41,54 +59,45 @@ public class DeviceListFragment extends ListFragment implements SyncthingService if (currentState != SyncthingService.State.ACTIVE) return; - initAdapter(); + updateList(); + } + + @Override + public void onViewCreated(View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + setEmptyText(getString(R.string.devices_list_empty)); + getListView().setOnItemClickListener(this); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - initAdapter(); + updateList(); setHasOptionsMenu(true); } - private void initAdapter() { - SyncthingActivity activity = (SyncthingActivity) getActivity(); - if (activity == null || activity.getApi() == null) - return; - - mAdapter = new DevicesAdapter(activity); - mAdapter.add(activity.getApi().getDevices(false)); - setListAdapter(mAdapter); - setEmptyText(getString(R.string.devices_list_empty)); - getListView().setOnItemClickListener(this); - } - + /** + * Refreshes ListView by updating devices and info. + * + * Also creates adapter if it doesn't exist yet. + */ private void updateList() { - if (mAdapter == null || getView() == null || getActivity().isFinishing()) + SyncthingActivity activity = (SyncthingActivity) getActivity(); + if (activity == null || activity.getApi() == null || getView() == null || + activity.isFinishing()) return; - MainActivity activity = (MainActivity) getActivity(); - mAdapter.updateConnections(activity.getApi(), getListView()); - } - - @Override - public void setUserVisibleHint(boolean isVisibleToUser) { - super.setUserVisibleHint(isVisibleToUser); - - if (isVisibleToUser) { - mTimer = new Timer(); - mTimer.schedule(new TimerTask() { - @Override - public void run() { - updateList(); - } - - }, 0, SyncthingService.GUI_UPDATE_INTERVAL); - } else if (mTimer != null) { - mTimer.cancel(); - mTimer = null; + if (mAdapter == null) { + mAdapter = new DevicesAdapter(activity); + setListAdapter(mAdapter); } + + mAdapter.clear(); + mAdapter.add(activity.getApi().getDevices(false)); + mAdapter.updateConnections(activity.getApi()); + setListShown(true); } @Override diff --git a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java index 79e41763..bff57127 100644 --- a/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java +++ b/src/main/java/com/nutomic/syncthingandroid/fragments/FolderListFragment.java @@ -11,7 +11,6 @@ import android.view.View; import android.widget.AdapterView; import com.nutomic.syncthingandroid.R; -import com.nutomic.syncthingandroid.activities.MainActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SyncthingActivity; import com.nutomic.syncthingandroid.syncthing.SyncthingService; @@ -33,7 +32,25 @@ public class FolderListFragment extends ListFragment implements SyncthingService @Override public void onResume() { super.onResume(); - setListShown(true); + mTimer = new Timer(); + mTimer.schedule(new TimerTask() { + @Override + public void run() { + getActivity().runOnUiThread(new Runnable() { + @Override + public void run() { + updateList(); + } + }); + } + + }, 0, SyncthingService.GUI_UPDATE_INTERVAL); + } + + @Override + public void onPause() { + super.onPause(); + mTimer.cancel(); } @Override @@ -41,7 +58,7 @@ public class FolderListFragment extends ListFragment implements SyncthingService if (currentState != SyncthingService.State.ACTIVE) return; - initAdapter(); + updateList(); } @Override @@ -57,45 +74,30 @@ public class FolderListFragment extends ListFragment implements SyncthingService public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - initAdapter(); + updateList(); setHasOptionsMenu(true); } - private void initAdapter() { - SyncthingActivity activity = (SyncthingActivity) getActivity(); - if (activity == null || activity.getApi() == null) - return; - - mAdapter = new FoldersAdapter(activity); - mAdapter.add(activity.getApi().getFolders()); - setListAdapter(mAdapter); - } - + /** + * Refreshes ListView by updating folders and info. + * + * Also creates adapter if it doesn't exist yet. + */ private void updateList() { - if (mAdapter == null || getView() == null || getActivity().isFinishing()) + SyncthingActivity activity = (SyncthingActivity) getActivity(); + if (activity == null || activity.getApi() == null || getView() == null || + activity.isFinishing()) return; - MainActivity activity = (MainActivity) getActivity(); - mAdapter.updateModel(activity.getApi(), getListView()); - } - - @Override - public void setUserVisibleHint(boolean isVisibleToUser) { - super.setUserVisibleHint(isVisibleToUser); - - if (isVisibleToUser) { - mTimer = new Timer(); - mTimer.schedule(new TimerTask() { - @Override - public void run() { - updateList(); - } - - }, 0, SyncthingService.GUI_UPDATE_INTERVAL); - } else if (mTimer != null) { - mTimer.cancel(); - mTimer = null; + if (mAdapter == null) { + mAdapter = new FoldersAdapter(activity); + setListAdapter(mAdapter); } + + mAdapter.clear(); + mAdapter.add(activity.getApi().getFolders()); + mAdapter.updateModel(activity.getApi()); + setListShown(true); } @Override diff --git a/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java b/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java index 5b65cf8b..68b4c1b6 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/DevicesAdapter.java @@ -98,12 +98,9 @@ public class DevicesAdapter extends ArrayAdapter /** * Requests new connection info for all devices visible in listView. */ - public void updateConnections(RestApi api, ListView listView) { + public void updateConnections(RestApi api) { for (int i = 0; i < getCount(); i++) { - if (i >= listView.getFirstVisiblePosition() && - i <= listView.getLastVisiblePosition()) { - api.getConnections(this); - } + api.getConnections(this); } } diff --git a/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java b/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java index c0ec3df1..9e736f38 100644 --- a/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java +++ b/src/main/java/com/nutomic/syncthingandroid/util/FoldersAdapter.java @@ -2,6 +2,7 @@ package com.nutomic.syncthingandroid.util; import android.content.Context; import android.text.TextUtils; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -71,9 +72,7 @@ public class FoldersAdapter extends ArrayAdapter size.setText(getContext().getString(R.string.folder_size_format, readableFileSize(getContext(), model.inSyncBytes), readableFileSize(getContext(), model.globalBytes))); - if (TextUtils.isEmpty(folder.invalid)) { - setTextOrHide(invalid, model.invalid); - } + setTextOrHide(invalid, model.invalid); } else { items.setVisibility(GONE); size.setVisibility(GONE); @@ -123,12 +122,9 @@ public class FoldersAdapter extends ArrayAdapter /** * Requests updated model info from the api for all visible items. */ - public void updateModel(RestApi api, ListView listView) { + public void updateModel(RestApi api) { for (int i = 0; i < getCount(); i++) { - if (i >= listView.getFirstVisiblePosition() && - i <= listView.getLastVisiblePosition()) { - api.getModel(getItem(i).id, this); - } + api.getModel(getItem(i).id, this); } } @@ -139,8 +135,7 @@ public class FoldersAdapter extends ArrayAdapter } private void setTextOrHide(TextView view, String text) { - boolean isEmpty = TextUtils.isEmpty(text); - if (isEmpty) { + if (TextUtils.isEmpty(text)) { view.setVisibility(GONE); } else { view.setText(text);