Closing TaskLists dialog now correctly updates the view

This commit is contained in:
BONNEVILLE Geoffroy
2017-12-22 11:22:21 +01:00
parent 0b3560a752
commit a8f0a80363
4 changed files with 109 additions and 77 deletions

View File

@@ -80,11 +80,13 @@ public class MainActivity extends AppCompatActivity {
/** Called when the user clicks the Edit Lists button */ /** Called when the user clicks the Edit Lists button */
public void openTaskLists(MenuItem menuItem) { public void openTaskLists(MenuItem menuItem) {
// Create the fragment
TaskListsDialogFragment taskListFragment = new TaskListsDialogFragment();
String title = getString(R.string.action_edit_task);
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentManager fragmentManager = getSupportFragmentManager();
// Create the fragment
TaskListsDialogFragment taskListFragment = TaskListsDialogFragment.newInstance(
(MainFragment)fragmentManager.findFragmentById(R.id.fragment_main));
String title = getString(R.string.action_edit_task);
// Set the arguments // Set the arguments
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putInt("button_count", 1); args.putInt("button_count", 1);

View File

@@ -33,7 +33,9 @@ import java.util.List;
/** /**
* Fragment that will handle the main display * Fragment that will handle the main display
*/ */
public class MainFragment extends Fragment implements TasksFragment.TaskChangedAdapter { public class MainFragment extends Fragment implements
TasksFragment.TaskChangedAdapter,
TaskListsDialogFragment.TaskListsListener {
private View mView; private View mView;
private ViewPager mViewPager; private ViewPager mViewPager;
@@ -53,84 +55,19 @@ public class MainFragment extends Fragment implements TasksFragment.TaskChangedA
Bundle savedInstanceState) { Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_main, container, false); mView = inflater.inflate(R.layout.fragment_main, container, false);
AppCompatActivity activity = (AppCompatActivity) getActivity();
Toolbar toolbar = mView.findViewById(R.id.toolbar); Toolbar toolbar = mView.findViewById(R.id.toolbar);
AppCompatActivity activity = (AppCompatActivity) getActivity();
assert activity != null; assert activity != null;
activity.setSupportActionBar(toolbar); activity.setSupportActionBar(toolbar);
// Load task lists
updateTaskLists(activity);
SharedPreferences sharedPref = SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences(activity); PreferenceManager.getDefaultSharedPreferences(activity);
int lastOpenedList = sharedPref.getInt("last_opened_tab", 0);
// Open last opened tab
mViewPager.setCurrentItem(lastOpenedList);
// Access database to retrieve Tabs
List<TaskList> taskLists;
try (TaskListDataAccess taskListDataAccess = new TaskListDataAccess(activity)) {
taskLists = taskListDataAccess.getAllTaskLists();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), taskLists);
mSectionsPagerAdapter.notifyDataSetChanged();
}
boolean isLargeLayout = getResources().getBoolean(R.bool.large_layout);
// TODO: determine whether this is the first startup, and if so, show a tutorial of sorts
// No tasks, show the edit task lists fragment
if (taskLists.size() == 0) {
TaskListsDialogFragment taskListFragment = new TaskListsDialogFragment();
String title = getString(R.string.task_list_no_lists);
FragmentManager fragmentManager = activity.getSupportFragmentManager();
// Set the arguments
Bundle args = new Bundle();
args.putInt("button_count", 1);
args.putString("button_negative", getString(R.string.task_list_ok));
taskListFragment.setArguments(args);
taskListFragment.showFragment(fragmentManager, title, getResources().getBoolean(R.bool.large_layout));
}
// Otherwise, show the normal view
else {
int lastOpenedList = sharedPref.getInt("last_opened_tab", 0);
// Set up the ViewPager with the sections adapter.
mViewPager = mView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
// Open last opened tab
mViewPager.setCurrentItem(lastOpenedList);
if (!isLargeLayout) {
tabLayout = mView.findViewById(R.id.tabs);
// Hide the tabs if there is only one task list
if (taskLists.size() == 1) tabLayout.setVisibility(View.GONE);
tabLayout.setupWithViewPager(mViewPager);
// Handles scroll detection (only available for SDK version >=23)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
toggleTabLayoutArrows(tabLayout.getScrollX());
//tabLayout.setScrollIndicators(TabLayout.SCROLL_INDICATOR_LEFT | TabLayout.SCROLL_INDICATOR_RIGHT);
tabLayout.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
toggleTabLayoutArrows(scrollX);
}
});
}
}
else {
ListView listView = mView.findViewById(R.id.list);
// Hide the list if there is only one task list
if (taskLists.size() == 1) listView.setVisibility(View.GONE);
//listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, taskLists));
listView.setAdapter(new ArrayAdapter<>(activity, R.layout.list_tasklist_item, taskLists));
//listView.setSelection(lastOpenedList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mViewPager.setCurrentItem(position);
}
});
}
}
return mView; return mView;
} }
@@ -154,6 +91,84 @@ public class MainFragment extends Fragment implements TasksFragment.TaskChangedA
if (destinationTaskAdapter != null) destinationTaskAdapter.add(task, destinationTaskAdapter.getItemCount()); if (destinationTaskAdapter != null) destinationTaskAdapter.add(task, destinationTaskAdapter.getItemCount());
} }
@Override
public void onTaskListsDialogNegativeClick() {
AppCompatActivity activity = (AppCompatActivity) getActivity();
assert activity != null;
updateTaskLists(activity);
}
private void updateTaskLists(AppCompatActivity activity)
{
// Access database to retrieve Tabs
List<TaskList> taskLists;
try (TaskListDataAccess taskListDataAccess = new TaskListDataAccess(activity)) {
taskLists = taskListDataAccess.getAllTaskLists();
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager(), taskLists);
mSectionsPagerAdapter.notifyDataSetChanged();
}
boolean isLargeLayout = getResources().getBoolean(R.bool.large_layout);
// TODO: determine whether this is the first startup, and if so, show a tutorial of sorts
// No tasks, show the edit task lists fragment
if (taskLists.size() == 0) {
TaskListsDialogFragment taskListFragment = TaskListsDialogFragment.newInstance(this);
String title = getString(R.string.task_list_no_lists);
FragmentManager fragmentManager = activity.getSupportFragmentManager();
// Set the arguments
Bundle args = new Bundle();
args.putInt("button_count", 1);
args.putString("button_negative", getString(R.string.task_list_ok));
taskListFragment.setArguments(args);
taskListFragment.showFragment(fragmentManager, title, getResources().getBoolean(R.bool.large_layout));
}
// Otherwise, show the normal view
else {
// Set up the ViewPager with the sections adapter.
mViewPager = mView.findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
if (!isLargeLayout) {
tabLayout = mView.findViewById(R.id.tabs);
// Hide the tabs if there is only one task list
tabLayout.setVisibility(taskLists.size() == 1 ? View.GONE : View.VISIBLE);
tabLayout.setupWithViewPager(mViewPager);
// Handles scroll detection (only available for SDK version >=23)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
toggleTabLayoutArrows(tabLayout.getScrollX());
//tabLayout.setScrollIndicators(TabLayout.SCROLL_INDICATOR_LEFT | TabLayout.SCROLL_INDICATOR_RIGHT);
tabLayout.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
toggleTabLayoutArrows(scrollX);
}
});
}
}
else {
ListView listView = mView.findViewById(R.id.list);
// Hide the list if there is only one task list
listView.setVisibility(taskLists.size() == 1 ? View.GONE : View.VISIBLE);
//listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, taskLists));
listView.setAdapter(new ArrayAdapter<>(activity, R.layout.list_tasklist_item, taskLists));
//listView.setSelection(lastOpenedList);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mViewPager.setCurrentItem(position);
}
});
}
}
}
private TaskRecyclerViewAdapter getSpecificTabAdapter(int position) { private TaskRecyclerViewAdapter getSpecificTabAdapter(int position) {
TasksFragment taskFragment = (TasksFragment) mSectionsPagerAdapter.getRegisteredFragment(position); TasksFragment taskFragment = (TasksFragment) mSectionsPagerAdapter.getRegisteredFragment(position);
if (taskFragment == null) return null; if (taskFragment == null) return null;

View File

@@ -39,7 +39,7 @@ public class TaskFormDialogFragment extends DynamicDialogFragment {
void onNewTaskDialogNeutralClick(DialogFragment dialog); void onNewTaskDialogNeutralClick(DialogFragment dialog);
} }
private TaskFormDialogFragment.NewTaskListener mListener; private NewTaskListener mListener;
private Task task; private Task task;
private List<TaskList> taskLists; private List<TaskList> taskLists;

View File

@@ -33,8 +33,21 @@ public class TaskListsDialogFragment extends DynamicDialogFragment implements
ConfirmDialogFragment.ConfirmDialogListener { ConfirmDialogFragment.ConfirmDialogListener {
private TaskListRecyclerViewAdapter taskListRecyclerViewAdapter; private TaskListRecyclerViewAdapter taskListRecyclerViewAdapter;
private TaskListDataAccess taskListDataAccess; private TaskListDataAccess taskListDataAccess;
//private View mView;
private ItemTouchHelper mItemTouchHelper; private ItemTouchHelper mItemTouchHelper;
private TaskListsListener mListener;
/** The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
interface TaskListsListener {
void onTaskListsDialogNegativeClick();
}
public static TaskListsDialogFragment newInstance(TaskListsListener taskListListener) {
TaskListsDialogFragment fragment = new TaskListsDialogFragment();
fragment.mListener = taskListListener;
return fragment;
}
/** /**
* Mandatory empty constructor for the fragment manager to instantiate the * Mandatory empty constructor for the fragment manager to instantiate the
@@ -94,6 +107,8 @@ public class TaskListsDialogFragment extends DynamicDialogFragment implements
@Override @Override
protected void onNegativeButtonClick() { protected void onNegativeButtonClick() {
dismiss(); dismiss();
// TODO: add an argument to refresh only if something changed
mListener.onTaskListsDialogNegativeClick();
} }
@Override @Override