Handle lifecycle better

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-10-23 19:10:35 +02:00
parent 029d47f99e
commit 38e48493c0
2 changed files with 51 additions and 42 deletions

View file

@ -249,7 +249,7 @@ public class CallsListController extends BaseController implements SearchView.On
}, throwable -> { }, throwable -> {
if (searchItem != null) { if (searchItem != null) {
searchItem.setVisible(false); searchItem.setVisible(false);
} }
dispose(roomsQueryDisposable); dispose(roomsQueryDisposable);
} }
, () -> { , () -> {
@ -265,19 +265,8 @@ public class CallsListController extends BaseController implements SearchView.On
roomItems.add(new RoomItem(room, userEntity)); roomItems.add(new RoomItem(room, userEntity));
} }
if (!TextUtils.isEmpty(adapter.getSearchText()) && searchItem != null && searchView != null) { adapter.updateDataSet(roomItems, true);
searchItem.expandActionView(); searchItem.setVisible(roomItems.size() > 0);
searchView.setQuery(adapter.getSearchText(), false);
recyclerView.setAdapter(null);
adapter.updateDataSet(roomItems, false);
onQueryTextSubmit(adapter.getSearchText());
recyclerView.setAdapter(adapter);
} else {
adapter.updateDataSet(roomItems, true);
if (searchItem != null) {
searchItem.setVisible(roomItems.size() > 0);
}
}
} }
}, throwable -> { }, throwable -> {
if (searchItem != null) { if (searchItem != null) {

View file

@ -80,6 +80,7 @@ public class ContactsController extends BaseController implements SearchView.OnQ
public static final String TAG = "ContactsController"; public static final String TAG = "ContactsController";
private static final String KEY_FROM_RESTORE_CONTROLLER = "ContactsController.fromRestoreController";
private static final String KEY_FROM_RESTORE_VIEW = "ContactsController.fromRestoreView"; private static final String KEY_FROM_RESTORE_VIEW = "ContactsController.fromRestoreView";
private static final String KEY_SEARCH_QUERY = "ContactsController.searchQuery"; private static final String KEY_SEARCH_QUERY = "ContactsController.searchQuery";
@ -103,11 +104,12 @@ public class ContactsController extends BaseController implements SearchView.OnQ
private FlexibleAdapter<UserItem> adapter; private FlexibleAdapter<UserItem> adapter;
private List<UserItem> contactItems = new ArrayList<>(); private List<UserItem> contactItems = new ArrayList<>();
private boolean isFromRestore; private boolean isFromRestoreController;
private String searchQuery; private boolean isFromRestoreView;
private MenuItem searchItem; private MenuItem searchItem;
private SearchView searchView; private SearchView searchView;
private String searchQuery;
public ContactsController() { public ContactsController() {
super(); super();
@ -123,23 +125,25 @@ public class ContactsController extends BaseController implements SearchView.OnQ
protected void onViewBound(@NonNull View view) { protected void onViewBound(@NonNull View view) {
super.onViewBound(view); super.onViewBound(view);
NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
adapter = new FlexibleAdapter<>(contactItems, getActivity(), false);
prepareRecyclerView();
swipeRefreshLayout.setOnRefreshListener(() -> fetchData(true));
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.colorPrimary));
} }
@Override @Override
protected void onAttach(@NonNull View view) { protected void onAttach(@NonNull View view) {
super.onAttach(view); super.onAttach(view);
if (adapter == null) {
adapter = new FlexibleAdapter<>(contactItems, getActivity(), false);
}
prepareViews();
if ((userEntity = userUtils.getCurrentUser()) != null) { if ((userEntity = userUtils.getCurrentUser()) != null) {
if (!cacheUtils.cacheExistsForContext(TAG) || !isFromRestore) { if (!adapter.hasSearchText()) {
fetchData(true); if (!cacheUtils.cacheExistsForContext(TAG) || !isFromRestoreView) {
} else { fetchData(true);
fetchData(false); } else if (cacheUtils.cacheExistsForContext(TAG) && isFromRestoreController) {
fetchData(false);
}
} }
} else { } else {
// Fallback to login if we have no users // Fallback to login if we have no users
@ -212,6 +216,10 @@ public class ContactsController extends BaseController implements SearchView.OnQ
public void onPrepareOptionsMenu(Menu menu) { public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu); super.onPrepareOptionsMenu(menu);
searchItem.setVisible(contactItems.size() > 0); searchItem.setVisible(contactItems.size() > 0);
if (adapter.hasSearchText()) {
searchItem.expandActionView();
searchView.setQuery(adapter.getSearchText(), false);
}
} }
private void fetchData(boolean forceNew) { private void fetchData(boolean forceNew) {
@ -302,17 +310,8 @@ public class ContactsController extends BaseController implements SearchView.OnQ
} }
if (!TextUtils.isEmpty(searchQuery) && searchItem != null && searchView != null) { adapter.updateDataSet(contactItems, true);
searchItem.expandActionView(); searchItem.setVisible(contactItems.size() > 0);
searchView.setQuery(searchQuery, false);
recyclerView.setAdapter(null);
adapter.updateDataSet(contactItems, false);
onQueryTextSubmit(searchQuery);
recyclerView.setAdapter(adapter);
} else {
adapter.updateDataSet(contactItems, true);
searchItem.setVisible(contactItems.size() > 0);
}
} }
}, throwable -> { }, throwable -> {
@ -327,13 +326,16 @@ public class ContactsController extends BaseController implements SearchView.OnQ
} }
} }
private void prepareRecyclerView() { private void prepareViews() {
recyclerView.setLayoutManager(new SmoothScrollLinearLayoutManager(getActivity())); recyclerView.setLayoutManager(new SmoothScrollLinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true); recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
recyclerView.addItemDecoration(new FlexibleItemDecoration(getActivity()) recyclerView.addItemDecoration(new FlexibleItemDecoration(getActivity())
.withDivider(R.drawable.divider)); .withDivider(R.drawable.divider));
swipeRefreshLayout.setOnRefreshListener(() -> fetchData(true));
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(getResources().getColor(R.color.colorPrimary));
} }
private void dispose(@Nullable Disposable disposable) { private void dispose(@Nullable Disposable disposable) {
@ -353,6 +355,18 @@ public class ContactsController extends BaseController implements SearchView.OnQ
} }
} }
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_FROM_RESTORE_CONTROLLER, true);
}
@Override
public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
isFromRestoreController = savedInstanceState.getBoolean(KEY_FROM_RESTORE_CONTROLLER, false);
}
@Override @Override
public void onSaveViewState(@NonNull View view, @NonNull Bundle outState) { public void onSaveViewState(@NonNull View view, @NonNull Bundle outState) {
super.onSaveViewState(view, outState); super.onSaveViewState(view, outState);
@ -365,16 +379,22 @@ public class ContactsController extends BaseController implements SearchView.OnQ
@Override @Override
public void onRestoreViewState(@NonNull View view, @NonNull Bundle savedViewState) { public void onRestoreViewState(@NonNull View view, @NonNull Bundle savedViewState) {
super.onRestoreViewState(view, savedViewState); super.onRestoreViewState(view, savedViewState);
isFromRestore = savedViewState.getBoolean(KEY_FROM_RESTORE_VIEW, false); isFromRestoreView = savedViewState.getBoolean(KEY_FROM_RESTORE_VIEW, false);
searchQuery = savedViewState.getString(KEY_SEARCH_QUERY, ""); searchQuery = savedViewState.getString(KEY_SEARCH_QUERY, "");
} }
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
if (adapter.hasNewSearchText(newText)) { if (adapter.hasNewSearchText(newText) || !TextUtils.isEmpty(searchQuery)) {
adapter.setSearchText(newText);
adapter.filterItems(300); if (!TextUtils.isEmpty(searchQuery)) {
adapter.setSearchText(searchQuery);
searchQuery = "";
adapter.filterItems();
} else {
adapter.setSearchText(newText);
adapter.filterItems(300);
}
} }
if (swipeRefreshLayout != null) { if (swipeRefreshLayout != null) {