From dd67ff44211d48d6a0ec44b5d6b801fc19e62659 Mon Sep 17 00:00:00 2001 From: Geoffroy Bonneville Date: Fri, 15 Jan 2016 13:38:24 -0500 Subject: [PATCH] Changes are now persisted when application is closed while the Snackbar is opened. Edit list menu button is now outside the menu, with an icon New menu button to allow faster layout changing (does the same as going in the Settings) Current opened tab is now saved when re-opening app --- DoNExt/app/build.gradle | 4 +- .../donext/activities/MainActivity.java | 60 +++++++++++++++++-- .../SmartFragmentStatePagerAdapter.java | 5 ++ .../donext/fragments/TasksFragment.java | 51 ++++++++-------- .../app/src/main/res/layout/activity_main.xml | 3 +- DoNExt/app/src/main/res/menu/menu_main.xml | 12 +++- DoNExt/app/src/main/res/values/strings.xml | 1 + 7 files changed, 101 insertions(+), 35 deletions(-) diff --git a/DoNExt/app/build.gradle b/DoNExt/app/build.gradle index e5ce9eb..894dda8 100644 --- a/DoNExt/app/build.gradle +++ b/DoNExt/app/build.gradle @@ -8,8 +8,8 @@ android { applicationId "com.wismna.geoffroy.donext" minSdkVersion 15 targetSdkVersion 23 - versionCode 6 - versionName "0.6" + versionCode 7 + versionName "0.7" } buildTypes { release { diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java index 29e9e45..aee6a12 100644 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/activities/MainActivity.java @@ -1,7 +1,9 @@ package com.wismna.geoffroy.donext.activities; import android.content.Intent; +import android.content.SharedPreferences; import android.os.Bundle; +import android.preference.PreferenceManager; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; @@ -72,6 +74,10 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.container); mViewPager.setAdapter(mSectionsPagerAdapter); + // Open last opened tab + SharedPreferences sharedPref = + PreferenceManager.getDefaultSharedPreferences(MainActivity.this); + mViewPager.setCurrentItem(sharedPref.getInt("last_opened_tab", 0)); TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(mViewPager); @@ -80,8 +86,36 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.show(); } - } + + @Override + protected void onPause() { + super.onPause(); + // Save currently opened tab + SharedPreferences sharedPref = + PreferenceManager.getDefaultSharedPreferences(MainActivity.this); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putInt("last_opened_tab", mViewPager.getCurrentItem()); + editor.apply(); + } + + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + MenuItem item = menu.findItem(R.id.action_changeLayout); + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + String layoutType = sharedPref.getString("pref_conf_task_layout", "1"); + switch (layoutType) { + case "1" : + item.setIcon(R.drawable.ic_list_white_24dp); + break; + case "2" : + item.setIcon(R.drawable.ic_view_list_white_24dp); + break; + } + + return super.onPrepareOptionsMenu(menu); + } + @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. @@ -122,17 +156,33 @@ public class MainActivity extends AppCompatActivity implements TasksFragment.Tas taskDialogFragment.show(manager, "Create new task"); } - /** Called when the user clicks the Settings button */ - public void openSettings(MenuItem menuItem) { - Intent intent = new Intent(this, SettingsActivity.class); - startActivity(intent); + /** Called when the user clicks on the Change Layout button */ + public void changeLayout(MenuItem item) { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + SharedPreferences.Editor editor = sharedPref.edit(); + String layoutTypeString = sharedPref.getString("pref_conf_task_layout", "1"); + int layoutType = Integer.valueOf(layoutTypeString); + editor.putString("pref_conf_task_layout", String.valueOf(layoutType % 2 + 1)); + editor.apply(); + + // Update the ViewPagerAdapter to refresh all tabs + mSectionsPagerAdapter.notifyDataSetChanged(); + // Invalidate the menu to redraw the icon + invalidateOptionsMenu(); } + /** Called when the user clicks the Edit Lists button */ public void openTaskLists(MenuItem menuItem) { Intent intent = new Intent(this, TaskListActivity.class); startActivity(intent); } + /** Called when the user clicks the Settings button */ + public void openSettings(MenuItem menuItem) { + Intent intent = new Intent(this, SettingsActivity.class); + startActivity(intent); + } + private TaskRecyclerViewAdapter getSpecificTabAdapter(int position) { TasksFragment taskFragment = (TasksFragment) mSectionsPagerAdapter.getRegisteredFragment(position); if (taskFragment == null) return null; diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/SmartFragmentStatePagerAdapter.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/SmartFragmentStatePagerAdapter.java index c7156e5..a2e8b34 100644 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/SmartFragmentStatePagerAdapter.java +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/adapters/SmartFragmentStatePagerAdapter.java @@ -21,6 +21,11 @@ public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerA super(fragmentManager); } + @Override + public int getItemPosition(Object object) { + return POSITION_NONE; + } + // Register the fragment when the item is instantiated @Override public Object instantiateItem(ViewGroup container, int position) { diff --git a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TasksFragment.java b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TasksFragment.java index c69c22e..3a644f2 100644 --- a/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TasksFragment.java +++ b/DoNExt/app/src/main/java/com/wismna/geoffroy/donext/fragments/TasksFragment.java @@ -52,6 +52,7 @@ public class TasksFragment extends Fragment implements private View view; private RecyclerView recyclerView; private TaskChangedAdapter mAdapter; + private Snackbar snackbar; /** * Mandatory empty constructor for the fragment manager to instantiate the @@ -164,6 +165,7 @@ public class TasksFragment extends Fragment implements @Override public void onDestroy() { super.onDestroy(); + if (snackbar != null) snackbar.dismiss(); taskDataAccess.close(); } @@ -198,7 +200,7 @@ public class TasksFragment extends Fragment implements } // Setup the snack bar - Snackbar.make(view, "Task " + action, Snackbar.LENGTH_LONG) + snackbar = Snackbar.make(view, "Task " + action, Snackbar.LENGTH_LONG) .setAction("Undo", new View.OnClickListener() { @Override public void onClick(View v) { @@ -220,33 +222,32 @@ public class TasksFragment extends Fragment implements taskRecyclerViewAdapter.add(task, itemPosition); recyclerView.scrollToPosition(0); } - }).setCallback(new Snackbar.Callback() { - @Override - public void onDismissed(Snackbar snackbar, int event) { - super.onDismissed(snackbar, event); + }); + snackbar.setCallback(new Snackbar.Callback() { + @Override + public void onDismissed(Snackbar snackbar, int event) { + super.onDismissed(snackbar, event); - // When clicked on undo, do not write to DB - if (event == DISMISS_EVENT_ACTION) return; + // When clicked on undo, do not write to DB + if (event == DISMISS_EVENT_ACTION) return; - // Commit the changes to DB - switch (direction) - { - // Mark item as Done - case ItemTouchHelper.LEFT: - taskDataAccess.setDone(itemId); - break; - // Increase task cycle count - case ItemTouchHelper.RIGHT: - taskDataAccess.increaseCycle(task.getCycle(), itemId); - break; - case -1: - // Commit the changes to DB - taskDataAccess.deleteTask(itemId); - } - - //UpdateCycleCount(); + // Commit the changes to DB + switch (direction) + { + // Mark item as Done + case ItemTouchHelper.LEFT: + taskDataAccess.setDone(itemId); + break; + // Increase task cycle count + case ItemTouchHelper.RIGHT: + taskDataAccess.increaseCycle(task.getCycle(), itemId); + break; + case -1: + // Commit the changes to DB + taskDataAccess.deleteTask(itemId); } - }).show(); + } + }).show(); } @Override diff --git a/DoNExt/app/src/main/res/layout/activity_main.xml b/DoNExt/app/src/main/res/layout/activity_main.xml index c9abe80..ba2ab0d 100644 --- a/DoNExt/app/src/main/res/layout/activity_main.xml +++ b/DoNExt/app/src/main/res/layout/activity_main.xml @@ -26,7 +26,8 @@ + android:layout_height="wrap_content" + app:tabMode="scrollable" /> diff --git a/DoNExt/app/src/main/res/menu/menu_main.xml b/DoNExt/app/src/main/res/menu/menu_main.xml index f2d90f8..d93b89e 100644 --- a/DoNExt/app/src/main/res/menu/menu_main.xml +++ b/DoNExt/app/src/main/res/menu/menu_main.xml @@ -9,11 +9,19 @@ android:onClick="openNewTaskDialog" app:showAsAction="never" />--> + + android:icon="@drawable/ic_create_new_folder_white_24dp" + app:showAsAction="ifRoom" /> Edit lists About New task + Change layout Settings