code refactoring: extract pull-to-refresh

This commit is contained in:
houxg
2017-03-01 17:34:41 +08:00
parent 259167bf9a
commit 9affd84981
2 changed files with 47 additions and 19 deletions

View File

@@ -0,0 +1,33 @@
package org.houxg.leamonax.component;
import android.support.v4.widget.SwipeRefreshLayout;
import com.elvishew.xlog.XLog;
public class PullToRefresh {
private SwipeRefreshLayout refreshLayout;
private SwipeRefreshLayout.OnRefreshListener listener;
public PullToRefresh(SwipeRefreshLayout refreshLayout, SwipeRefreshLayout.OnRefreshListener listener) {
this.listener = listener;
this.refreshLayout = refreshLayout;
refreshLayout.setOnRefreshListener(listener);
}
public void forceRefresh() {
refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
XLog.i("fetching notes");
refreshLayout.setRefreshing(true);
listener.onRefresh();
}
}, 200);
}
public void stopRefreshing() {
refreshLayout.setRefreshing(false);
}
}

View File

@@ -20,6 +20,7 @@ import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode; import org.greenrobot.eventbus.ThreadMode;
import org.houxg.leamonax.R; import org.houxg.leamonax.R;
import org.houxg.leamonax.background.NoteSyncService; import org.houxg.leamonax.background.NoteSyncService;
import org.houxg.leamonax.component.PullToRefresh;
import org.houxg.leamonax.model.Account; import org.houxg.leamonax.model.Account;
import org.houxg.leamonax.model.Note; import org.houxg.leamonax.model.Note;
import org.houxg.leamonax.model.Notebook; import org.houxg.leamonax.model.Notebook;
@@ -44,9 +45,8 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
@BindView(R.id.drawer) @BindView(R.id.drawer)
View mNavigationView; View mNavigationView;
@BindView(R.id.swiperefresh)
SwipeRefreshLayout mSwipeRefresh;
PullToRefresh mPullToRefresh;
Navigation mNavigation; Navigation mNavigation;
public static Intent getOpenIntent(Context context, boolean shouldReload) { public static Intent getOpenIntent(Context context, boolean shouldReload) {
@@ -77,31 +77,26 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
mNoteFragment = (NoteFragment) getSupportFragmentManager().findFragmentByTag(TAG_NOTE_FRAGMENT); mNoteFragment = (NoteFragment) getSupportFragmentManager().findFragmentByTag(TAG_NOTE_FRAGMENT);
} }
mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { mPullToRefresh = new PullToRefresh(
@Override (SwipeRefreshLayout) findViewById(R.id.swiperefresh),
public void onRefresh() { new SwipeRefreshLayout.OnRefreshListener() {
syncNotes(); @Override
} public void onRefresh() {
}); syncNotes();
}
});
EventBus.getDefault().register(this); EventBus.getDefault().register(this);
if (shouldReload) { if (shouldReload) {
mSwipeRefresh.postDelayed(new Runnable() { mPullToRefresh.forceRefresh();
@Override
public void run() {
XLog.i("fetching notes");
mSwipeRefresh.setRefreshing(true);
syncNotes();
}
}, 200);
} }
} }
private void syncNotes() { private void syncNotes() {
if (!NetworkUtils.isNetworkAvailable()) { if (!NetworkUtils.isNetworkAvailable()) {
ToastUtils.showNetworkUnavailable(MainActivity.this); ToastUtils.showNetworkUnavailable(MainActivity.this);
mSwipeRefresh.setRefreshing(false); mPullToRefresh.stopRefreshing();
return; return;
} }
NoteSyncService.startServiceForNote(MainActivity.this); NoteSyncService.startServiceForNote(MainActivity.this);
@@ -176,7 +171,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
account.updateLastUseTime(); account.updateLastUseTime();
account.update(); account.update();
mNavigation.refresh(); mNavigation.refresh();
mSwipeRefresh.setRefreshing(true); mPullToRefresh.stopRefreshing();
syncNotes(); syncNotes();
return true; return true;
} }
@@ -216,7 +211,7 @@ public class MainActivity extends BaseActivity implements Navigation.Callback {
@Subscribe(threadMode = ThreadMode.MAIN) @Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(SyncEvent event) { public void onEvent(SyncEvent event) {
XLog.i("RequestNotes rcv: isSucceed=" + event.isSucceed()); XLog.i("RequestNotes rcv: isSucceed=" + event.isSucceed());
mSwipeRefresh.setRefreshing(false); mPullToRefresh.stopRefreshing();
if (event.isSucceed()) { if (event.isSucceed()) {
mNavigation.refresh(); mNavigation.refresh();
} else { } else {