From 161e99f1db6f96571cfe45ca457a4a5a08709cec Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 22 Feb 2016 10:51:07 +0100 Subject: [PATCH] Save and restore selected tabs --- .../android/calculator/ActivityUi.java | 32 +------ .../android/calculator/AppComponent.java | 2 + .../android/calculator/AppModule.java | 8 ++ .../android/calculator/view/Tabs.java | 90 +++++++++++++------ 4 files changed, 76 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java b/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java index 44bf106c..20d9cc16 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java @@ -30,7 +30,6 @@ import android.content.res.Configuration; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; -import android.preference.PreferenceManager; import android.support.annotation.DrawableRes; import android.support.annotation.LayoutRes; import android.support.design.widget.FloatingActionButton; @@ -55,6 +54,7 @@ import org.solovyev.android.calculator.view.Tabs; import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; +import javax.inject.Named; import static org.solovyev.android.calculator.App.cast; @@ -91,7 +91,6 @@ public class ActivityUi { private Preferences.Gui.Layout layout = Preferences.Gui.Layout.main_calculator; @Nonnull private Language language = Languages.SYSTEM_LANGUAGE; - private int selectedNavigationIndex = 0; public ActivityUi(@Nonnull AppCompatActivity activity, @LayoutRes int layoutId) { this.activity = activity; @@ -127,11 +126,6 @@ public class ActivityUi { App.getGa().getAnalytics().reportActivityStart(activity); } - @Nonnull - private static String makeLastTabKey(@Nonnull Activity activity) { - return "tab_" + activity.getClass().getSimpleName(); - } - public static void setFont(@Nonnull TextView view, @Nonnull Typeface newTypeface) { final Typeface oldTypeface = view.getTypeface(); if (oldTypeface == newTypeface) { @@ -188,26 +182,8 @@ public class ActivityUi { actionBar.setDisplayHomeAsUpEnabled(true); } - public void restoreSavedTab() { - final ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - if (selectedNavigationIndex >= 0 && selectedNavigationIndex < actionBar.getTabCount()) { - actionBar.setSelectedNavigationItem(selectedNavigationIndex); - } - } - } - public void onPause() { - final ActionBar actionBar = activity.getSupportActionBar(); - if (actionBar != null) { - final int selectedNavigationIndex = actionBar.getSelectedNavigationIndex(); - if (selectedNavigationIndex >= 0) { - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); - final SharedPreferences.Editor editor = preferences.edit(); - editor.putInt(makeLastTabKey(activity), selectedNavigationIndex); - editor.apply(); - } - } + tabs.onPause(); } public void onDestroy() { @@ -248,10 +224,6 @@ public class ActivityUi { if (!restartIfThemeChanged(activity, theme)) { restartIfLanguageChanged(activity, language); } - - final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); - selectedNavigationIndex = preferences.getInt(makeLastTabKey(activity), -1); - restoreSavedTab(); } private void addHelpInfo(@Nonnull Activity activity, @Nonnull View root) { diff --git a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java index a300aa2e..470a18af 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -16,6 +16,7 @@ import org.solovyev.android.calculator.preferences.PreferencesActivity; import org.solovyev.android.calculator.preferences.PurchaseDialogActivity; import org.solovyev.android.calculator.variables.EditVariableFragment; import org.solovyev.android.calculator.variables.VariablesFragment; +import org.solovyev.android.calculator.view.Tabs; import org.solovyev.android.calculator.wizard.DragButtonWizardStep; import dagger.Component; @@ -51,4 +52,5 @@ public interface AppComponent { void inject(DragButtonWizardStep fragment); void inject(BaseFragment fragment); void inject(HistoryActivity activity); + void inject(Tabs tabs); } diff --git a/app/src/main/java/org/solovyev/android/calculator/AppModule.java b/app/src/main/java/org/solovyev/android/calculator/AppModule.java index 445fae69..6353334e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppModule.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppModule.java @@ -49,6 +49,7 @@ public class AppModule { public static final String THREAD_BACKGROUND = "thread-background"; public static final String DIR_FILES = "dir-files"; public static final String PREFS_FLOATING = "prefs-floating"; + public static final String PREFS_TABS = "prefs-tabs"; @NonNull private final Application application; @@ -88,6 +89,13 @@ public class AppModule { return application.getSharedPreferences("floating-calculator", Context.MODE_PRIVATE); } + @Provides + @Singleton + @Named(PREFS_TABS) + SharedPreferences provideTabsPreferences() { + return application.getSharedPreferences("tabs", Context.MODE_PRIVATE); + } + @Provides @Singleton @Named(THREAD_INIT) diff --git a/app/src/main/java/org/solovyev/android/calculator/view/Tabs.java b/app/src/main/java/org/solovyev/android/calculator/view/Tabs.java index 705c7da1..431e9950 100644 --- a/app/src/main/java/org/solovyev/android/calculator/view/Tabs.java +++ b/app/src/main/java/org/solovyev/android/calculator/view/Tabs.java @@ -1,34 +1,44 @@ package org.solovyev.android.calculator.view; +import static org.solovyev.android.calculator.App.cast; + +import android.app.Activity; +import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; -import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; -import android.view.ViewTreeObserver; -import butterknife.Bind; -import butterknife.ButterKnife; + +import org.solovyev.android.calculator.AppModule; import org.solovyev.android.calculator.FragmentTab; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.entities.BaseEntitiesFragment; import org.solovyev.android.calculator.entities.Category; -import org.solovyev.android.views.Adjuster; -import javax.annotation.Nonnull; +import butterknife.Bind; +import butterknife.ButterKnife; + import java.util.ArrayList; import java.util.List; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import javax.inject.Named; + public class Tabs { @Nonnull private final AppCompatActivity activity; @Nonnull private final TabFragments adapter; + @Inject + @Named(AppModule.PREFS_TABS) + SharedPreferences preferences; @Nullable @Bind(R.id.tabs) TabLayout tabLayout; @@ -42,6 +52,7 @@ public class Tabs { } public void onCreate() { + cast(activity.getApplicationContext()).getComponent().inject(this); ButterKnife.bind(this, activity); if (tabLayout == null || viewPager == null) { @@ -55,26 +66,7 @@ public class Tabs { viewPager.setAdapter(adapter); tabLayout.setTabMode(tabs > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED); tabLayout.setupWithViewPager(viewPager); - - - if (ViewCompat.isLaidOut(tabLayout)) { - tabLayout.setupWithViewPager(viewPager); - } else { - final ViewTreeObserver treeObserver = Adjuster.getTreeObserver(tabLayout); - if (treeObserver != null) { - treeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { - @Override - public void onGlobalLayout() { - final ViewTreeObserver anotherTreeObserver = Adjuster.getTreeObserver(tabLayout); - if(anotherTreeObserver != null) { - //noinspection deprecation - anotherTreeObserver.removeGlobalOnLayoutListener(this); - } - tabLayout.setupWithViewPager(viewPager); - } - }); - } - } + restoreSelectedTab(); } public void addTab(@Nonnull Category category, @Nonnull FragmentTab tab) { @@ -103,6 +95,13 @@ public class Tabs { return adapter.getItem(viewPager.getCurrentItem()); } + public int getCurrentTab() { + if (viewPager == null) { + return -1; + } + return viewPager.getCurrentItem(); + } + private final class TabFragments extends FragmentPagerAdapter { @Nonnull @@ -152,4 +151,43 @@ public class Tabs { return Fragment.instantiate(activity, fragmentClass.getName(), fragmentArgs); } } + + public int getTabCount() { + return adapter.getCount(); + } + + public void selectTab(int index) { + if(tabLayout == null) { + return; + } + final TabLayout.Tab tab = tabLayout.getTabAt(index); + if (tab != null) { + tab.select(); + } + } + + public void restoreSelectedTab() { + final int selectedTab = preferences.getInt(makeTabKey(activity), -1); + if (selectedTab >= 0 && selectedTab < getTabCount()) { + selectTab(selectedTab); + } + } + + public void onPause() { + saveSelectedTab(); + } + + private void saveSelectedTab() { + final int selectedTab = getCurrentTab(); + if (selectedTab >= 0) { + final SharedPreferences.Editor editor = preferences.edit(); + editor.putInt(makeTabKey(activity), selectedTab); + editor.apply(); + } + } + + @Nonnull + private static String makeTabKey(@Nonnull Activity activity) { + return activity.getClass().getSimpleName(); + } }