Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Revert "Reusing methods onSaveInstanceState() and restoreState() thro…
Browse files Browse the repository at this point in the history
…ugh AppListFragment"

This reverts commit 1ce0ab5.
  • Loading branch information
iadeelzafar committed Apr 21, 2019
1 parent 4b91098 commit 6c8aeaf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

package org.odk.share.fragments;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.MenuItemCompat;
Expand All @@ -28,11 +26,8 @@
import android.view.MenuItem;
import android.view.View;

import android.widget.Button;
import butterknife.BindView;
import org.odk.share.R;
import org.odk.share.adapters.SortDialogAdapter;
import org.odk.share.utilities.ArrayUtils;

import java.util.LinkedHashSet;

Expand All @@ -46,19 +41,6 @@ abstract class AppListFragment extends InjectableFragment {
private BottomSheetDialog bottomSheetDialog;
private String filterText;

private static final String SELECTED_INSTANCES_KEY = "ROTATION_SELECTED_INSTANCES";

@BindView(R.id.send_button)
Button sendButton;

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
selectedInstances.toArray(new Long[selectedInstances.size()])));

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
Expand Down Expand Up @@ -131,16 +113,6 @@ public void onResume() {
}
}

public void restoreState(Bundle savedInstanceState) {
if (savedInstanceState != null) {
long[] previousSelectedInstances = savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
for (long previousSelectedInstance : previousSelectedInstances) {
selectedInstances.add(previousSelectedInstance);
}
sendButton.setEnabled(selectedInstances.size() > 0);
}
}

private void setupBottomSheet() {
bottomSheetDialog = new BottomSheetDialog(getActivity());
View sheetView = getActivity().getLayoutInflater().inflate(R.layout.bottom_sheet, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.odk.share.utilities.ApplicationConstants;
import org.odk.share.utilities.ArrayUtils;

import java.util.LinkedHashSet;

import javax.inject.Inject;

import butterknife.BindView;
Expand All @@ -46,6 +48,7 @@ public class BlankFormsFragment extends FormListFragment implements LoaderManage

public static final String FORM_IDS = "form_ids";
private static final String FORM_CHOOSER_LIST_SORTING_ORDER = "formChooserListSortingOrder";
private static final String SELECTED_INSTANCES_KEY = "ROTATION_SELECTED_INSTANCES";
private static final int FORM_LOADER = 2;

@BindView(R.id.recyclerview)
Expand All @@ -69,6 +72,8 @@ public class BlankFormsFragment extends FormListFragment implements LoaderManage
TransferDao transferDao;

private FormsAdapter formAdapter;
private LinkedHashSet<Long> selectedForms;


public BlankFormsFragment() {
}
Expand All @@ -79,11 +84,19 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
View view = inflater.inflate(R.layout.fragment_forms, container, false);
ButterKnife.bind(this, view);

selectedForms = new LinkedHashSet<>();

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);

restoreState(savedInstanceState);
if (savedInstanceState != null) {
long[] previousSelectedInstances = savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
for (long previousSelectedInstance : previousSelectedInstances) {
selectedForms.add(previousSelectedInstance);
}
sendButton.setEnabled(selectedForms.size() > 0);
}

return view;
}
Expand All @@ -100,11 +113,19 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return formsDao.getFormsCursorLoader(getFilterText(), getSortingOrder());
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
selectedForms.toArray(new Long[selectedForms.size()])));

}

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
if (cursor != null) {
cursor.moveToFirst();
formAdapter = new FormsAdapter(getActivity(), cursor, this, selectedInstances, instancesDao, transferDao);
formAdapter = new FormsAdapter(getActivity(), cursor, this, selectedForms, instancesDao, transferDao);
recyclerView.setAdapter(formAdapter);
setEmptyViewVisibility(cursor.getCount());
if (formAdapter.getItemCount() > 0) {
Expand All @@ -129,13 +150,13 @@ public void onItemClick(BaseCursorViewHolder holder, int position) {
((FormsAdapter.FormHolder) holder).toggleCheckbox();

long id = ((FormsAdapter.FormHolder) holder).getForm().getId();
if (selectedInstances.contains(id)) {
selectedInstances.remove(id);
if (selectedForms.contains(id)) {
selectedForms.remove(id);
} else {
selectedInstances.add(id);
selectedForms.add(id);
}

sendButton.setEnabled(selectedInstances.size() > 0);
sendButton.setEnabled(selectedForms.size() > 0);

toggleButtonLabel();
}
Expand All @@ -154,7 +175,7 @@ private void setEmptyViewVisibility(int len) {
@OnClick(R.id.send_button)
public void send() {
Intent intent = new Intent(getActivity(), SendActivity.class);
Long[] arr = selectedInstances.toArray(new Long[selectedInstances.size()]);
Long[] arr = selectedForms.toArray(new Long[selectedForms.size()]);
long[] a = ArrayUtils.toPrimitive(arr);
intent.putExtra(FORM_IDS, a);
intent.putExtra(MODE, ApplicationConstants.SEND_BLANK_FORM_MODE);
Expand All @@ -164,26 +185,26 @@ public void send() {

@OnClick(R.id.toggle_button)
public void toggle() {
boolean newState = formAdapter.getItemCount() > selectedInstances.size();
boolean newState = formAdapter.getItemCount() > selectedForms.size();
sendButton.setEnabled(newState);

if (newState) {
Cursor cursor = formAdapter.getCursor();
if (cursor.moveToFirst()) {
do {
selectedInstances.add(cursor.getLong(cursor.getColumnIndex(FormsProviderAPI.FormsColumns._ID)));
selectedForms.add(cursor.getLong(cursor.getColumnIndex(FormsProviderAPI.FormsColumns._ID)));
} while (cursor.moveToNext());
}
} else {
selectedInstances.clear();
selectedForms.clear();
}

formAdapter.notifyDataSetChanged();
toggleButtonLabel();
}

private void toggleButtonLabel() {
if (selectedInstances.size() == formAdapter.getItemCount()) {
if (selectedForms.size() == formAdapter.getItemCount()) {
toggleButton.setText(getString(R.string.clear_all));
} else {
toggleButton.setText(getString(R.string.select_all));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.odk.share.utilities.ApplicationConstants;
import org.odk.share.utilities.ArrayUtils;

import java.util.LinkedHashSet;

import javax.inject.Inject;

import butterknife.BindView;
Expand Down Expand Up @@ -58,6 +60,8 @@ public class FilledFormsFragment extends InstanceListFragment implements LoaderM
InstancesDao instancesDao;

private InstanceAdapter instanceAdapter;
private LinkedHashSet<Long> selectedInstances;
private static final String SELECTED_INSTANCES_KEY = "ROTATION_SELECTED_INSTANCES";

public FilledFormsFragment() {
}
Expand All @@ -68,11 +72,18 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
setHasOptionsMenu(true);
View view = inflater.inflate(R.layout.fragment_forms, container, false);
ButterKnife.bind(this, view);
selectedInstances = new LinkedHashSet<>();
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);

restoreState(savedInstanceState);
if (savedInstanceState != null) {
long[] previousSelectedInstances = savedInstanceState.getLongArray(SELECTED_INSTANCES_KEY);
for (long previousSelectedInstance : previousSelectedInstances) {
selectedInstances.add(previousSelectedInstance);
}
sendButton.setEnabled(selectedInstances.size() > 0);
}

return view;
}
Expand All @@ -89,6 +100,14 @@ public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return instancesDao.getSavedInstancesCursorLoader(getFilterText(), getSortingOrder());
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLongArray(SELECTED_INSTANCES_KEY, ArrayUtils.toPrimitive(
selectedInstances.toArray(new Long[selectedInstances.size()])));

}

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor cursor) {
if (cursor != null) {
Expand Down

0 comments on commit 6c8aeaf

Please sign in to comment.