1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-11 12:35:53 +00:00

Fixed loading of folder and device list items (fixes #556, ref #74).

This commit is contained in:
Felix Ableitner 2016-02-25 12:47:16 +01:00
parent a15d08cfb3
commit 63a7342bfc
4 changed files with 89 additions and 86 deletions

View file

@ -33,7 +33,25 @@ public class DeviceListFragment extends ListFragment implements SyncthingService
@Override @Override
public void onResume() { public void onResume() {
super.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 @Override
@ -41,54 +59,45 @@ public class DeviceListFragment extends ListFragment implements SyncthingService
if (currentState != SyncthingService.State.ACTIVE) if (currentState != SyncthingService.State.ACTIVE)
return; 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 @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
initAdapter(); updateList();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
private void initAdapter() { /**
SyncthingActivity activity = (SyncthingActivity) getActivity(); * Refreshes ListView by updating devices and info.
if (activity == null || activity.getApi() == null) *
return; * Also creates adapter if it doesn't exist yet.
*/
mAdapter = new DevicesAdapter(activity);
mAdapter.add(activity.getApi().getDevices(false));
setListAdapter(mAdapter);
setEmptyText(getString(R.string.devices_list_empty));
getListView().setOnItemClickListener(this);
}
private void updateList() { 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; return;
MainActivity activity = (MainActivity) getActivity(); if (mAdapter == null) {
mAdapter.updateConnections(activity.getApi(), getListView()); mAdapter = new DevicesAdapter(activity);
setListAdapter(mAdapter);
} }
@Override mAdapter.clear();
public void setUserVisibleHint(boolean isVisibleToUser) { mAdapter.add(activity.getApi().getDevices(false));
super.setUserVisibleHint(isVisibleToUser); mAdapter.updateConnections(activity.getApi());
setListShown(true);
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;
}
} }
@Override @Override

View file

@ -11,7 +11,6 @@ import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import com.nutomic.syncthingandroid.R; import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.activities.MainActivity;
import com.nutomic.syncthingandroid.activities.SettingsActivity; import com.nutomic.syncthingandroid.activities.SettingsActivity;
import com.nutomic.syncthingandroid.activities.SyncthingActivity; import com.nutomic.syncthingandroid.activities.SyncthingActivity;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
@ -33,7 +32,25 @@ public class FolderListFragment extends ListFragment implements SyncthingService
@Override @Override
public void onResume() { public void onResume() {
super.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 @Override
@ -41,7 +58,7 @@ public class FolderListFragment extends ListFragment implements SyncthingService
if (currentState != SyncthingService.State.ACTIVE) if (currentState != SyncthingService.State.ACTIVE)
return; return;
initAdapter(); updateList();
} }
@Override @Override
@ -57,45 +74,30 @@ public class FolderListFragment extends ListFragment implements SyncthingService
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
initAdapter(); updateList();
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
private void initAdapter() { /**
* Refreshes ListView by updating folders and info.
*
* Also creates adapter if it doesn't exist yet.
*/
private void updateList() {
SyncthingActivity activity = (SyncthingActivity) getActivity(); SyncthingActivity activity = (SyncthingActivity) getActivity();
if (activity == null || activity.getApi() == null) if (activity == null || activity.getApi() == null || getView() == null ||
activity.isFinishing())
return; return;
if (mAdapter == null) {
mAdapter = new FoldersAdapter(activity); mAdapter = new FoldersAdapter(activity);
mAdapter.add(activity.getApi().getFolders());
setListAdapter(mAdapter); setListAdapter(mAdapter);
} }
private void updateList() { mAdapter.clear();
if (mAdapter == null || getView() == null || getActivity().isFinishing()) mAdapter.add(activity.getApi().getFolders());
return; mAdapter.updateModel(activity.getApi());
setListShown(true);
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;
}
} }
@Override @Override

View file

@ -98,14 +98,11 @@ public class DevicesAdapter extends ArrayAdapter<RestApi.Device>
/** /**
* Requests new connection info for all devices visible in listView. * 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++) { for (int i = 0; i < getCount(); i++) {
if (i >= listView.getFirstVisiblePosition() &&
i <= listView.getLastVisiblePosition()) {
api.getConnections(this); api.getConnections(this);
} }
} }
}
@Override @Override
public void onReceiveConnections(Map<String, RestApi.Connection> connections) { public void onReceiveConnections(Map<String, RestApi.Connection> connections) {

View file

@ -2,6 +2,7 @@ package com.nutomic.syncthingandroid.util;
import android.content.Context; import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -71,9 +72,7 @@ public class FoldersAdapter extends ArrayAdapter<RestApi.Folder>
size.setText(getContext().getString(R.string.folder_size_format, size.setText(getContext().getString(R.string.folder_size_format,
readableFileSize(getContext(), model.inSyncBytes), readableFileSize(getContext(), model.inSyncBytes),
readableFileSize(getContext(), model.globalBytes))); readableFileSize(getContext(), model.globalBytes)));
if (TextUtils.isEmpty(folder.invalid)) {
setTextOrHide(invalid, model.invalid); setTextOrHide(invalid, model.invalid);
}
} else { } else {
items.setVisibility(GONE); items.setVisibility(GONE);
size.setVisibility(GONE); size.setVisibility(GONE);
@ -123,14 +122,11 @@ public class FoldersAdapter extends ArrayAdapter<RestApi.Folder>
/** /**
* Requests updated model info from the api for all visible items. * 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++) { 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);
} }
} }
}
@Override @Override
public void onReceiveModel(String folderId, RestApi.Model model) { public void onReceiveModel(String folderId, RestApi.Model model) {
@ -139,8 +135,7 @@ public class FoldersAdapter extends ArrayAdapter<RestApi.Folder>
} }
private void setTextOrHide(TextView view, String text) { private void setTextOrHide(TextView view, String text) {
boolean isEmpty = TextUtils.isEmpty(text); if (TextUtils.isEmpty(text)) {
if (isEmpty) {
view.setVisibility(GONE); view.setVisibility(GONE);
} else { } else {
view.setText(text); view.setText(text);