1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2025-01-11 04:25: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
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

View file

@ -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

View file

@ -98,12 +98,9 @@ public class DevicesAdapter extends ArrayAdapter<RestApi.Device>
/**
* 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);
}
}

View file

@ -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<RestApi.Folder>
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<RestApi.Folder>
/**
* 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<RestApi.Folder>
}
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);