Save and restore selected tabs

This commit is contained in:
serso 2016-02-22 10:51:07 +01:00
parent 49ea547003
commit 161e99f1db
4 changed files with 76 additions and 56 deletions

View File

@ -30,7 +30,6 @@ import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.support.annotation.LayoutRes; import android.support.annotation.LayoutRes;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
@ -55,6 +54,7 @@ import org.solovyev.android.calculator.view.Tabs;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import static org.solovyev.android.calculator.App.cast; 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; private Preferences.Gui.Layout layout = Preferences.Gui.Layout.main_calculator;
@Nonnull @Nonnull
private Language language = Languages.SYSTEM_LANGUAGE; private Language language = Languages.SYSTEM_LANGUAGE;
private int selectedNavigationIndex = 0;
public ActivityUi(@Nonnull AppCompatActivity activity, @LayoutRes int layoutId) { public ActivityUi(@Nonnull AppCompatActivity activity, @LayoutRes int layoutId) {
this.activity = activity; this.activity = activity;
@ -127,11 +126,6 @@ public class ActivityUi {
App.getGa().getAnalytics().reportActivityStart(activity); 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) { public static void setFont(@Nonnull TextView view, @Nonnull Typeface newTypeface) {
final Typeface oldTypeface = view.getTypeface(); final Typeface oldTypeface = view.getTypeface();
if (oldTypeface == newTypeface) { if (oldTypeface == newTypeface) {
@ -188,26 +182,8 @@ public class ActivityUi {
actionBar.setDisplayHomeAsUpEnabled(true); 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() { public void onPause() {
final ActionBar actionBar = activity.getSupportActionBar(); tabs.onPause();
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();
}
}
} }
public void onDestroy() { public void onDestroy() {
@ -248,10 +224,6 @@ public class ActivityUi {
if (!restartIfThemeChanged(activity, theme)) { if (!restartIfThemeChanged(activity, theme)) {
restartIfLanguageChanged(activity, language); 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) { private void addHelpInfo(@Nonnull Activity activity, @Nonnull View root) {

View File

@ -16,6 +16,7 @@ import org.solovyev.android.calculator.preferences.PreferencesActivity;
import org.solovyev.android.calculator.preferences.PurchaseDialogActivity; import org.solovyev.android.calculator.preferences.PurchaseDialogActivity;
import org.solovyev.android.calculator.variables.EditVariableFragment; import org.solovyev.android.calculator.variables.EditVariableFragment;
import org.solovyev.android.calculator.variables.VariablesFragment; import org.solovyev.android.calculator.variables.VariablesFragment;
import org.solovyev.android.calculator.view.Tabs;
import org.solovyev.android.calculator.wizard.DragButtonWizardStep; import org.solovyev.android.calculator.wizard.DragButtonWizardStep;
import dagger.Component; import dagger.Component;
@ -51,4 +52,5 @@ public interface AppComponent {
void inject(DragButtonWizardStep fragment); void inject(DragButtonWizardStep fragment);
void inject(BaseFragment fragment); void inject(BaseFragment fragment);
void inject(HistoryActivity activity); void inject(HistoryActivity activity);
void inject(Tabs tabs);
} }

View File

@ -49,6 +49,7 @@ public class AppModule {
public static final String THREAD_BACKGROUND = "thread-background"; public static final String THREAD_BACKGROUND = "thread-background";
public static final String DIR_FILES = "dir-files"; public static final String DIR_FILES = "dir-files";
public static final String PREFS_FLOATING = "prefs-floating"; public static final String PREFS_FLOATING = "prefs-floating";
public static final String PREFS_TABS = "prefs-tabs";
@NonNull @NonNull
private final Application application; private final Application application;
@ -88,6 +89,13 @@ public class AppModule {
return application.getSharedPreferences("floating-calculator", Context.MODE_PRIVATE); return application.getSharedPreferences("floating-calculator", Context.MODE_PRIVATE);
} }
@Provides
@Singleton
@Named(PREFS_TABS)
SharedPreferences provideTabsPreferences() {
return application.getSharedPreferences("tabs", Context.MODE_PRIVATE);
}
@Provides @Provides
@Singleton @Singleton
@Named(THREAD_INIT) @Named(THREAD_INIT)

View File

@ -1,34 +1,44 @@
package org.solovyev.android.calculator.view; 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.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout; import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.view.ViewTreeObserver;
import butterknife.Bind; import org.solovyev.android.calculator.AppModule;
import butterknife.ButterKnife;
import org.solovyev.android.calculator.FragmentTab; import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment; import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.android.calculator.entities.Category; 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.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Named;
public class Tabs { public class Tabs {
@Nonnull @Nonnull
private final AppCompatActivity activity; private final AppCompatActivity activity;
@Nonnull @Nonnull
private final TabFragments adapter; private final TabFragments adapter;
@Inject
@Named(AppModule.PREFS_TABS)
SharedPreferences preferences;
@Nullable @Nullable
@Bind(R.id.tabs) @Bind(R.id.tabs)
TabLayout tabLayout; TabLayout tabLayout;
@ -42,6 +52,7 @@ public class Tabs {
} }
public void onCreate() { public void onCreate() {
cast(activity.getApplicationContext()).getComponent().inject(this);
ButterKnife.bind(this, activity); ButterKnife.bind(this, activity);
if (tabLayout == null || viewPager == null) { if (tabLayout == null || viewPager == null) {
@ -55,26 +66,7 @@ public class Tabs {
viewPager.setAdapter(adapter); viewPager.setAdapter(adapter);
tabLayout.setTabMode(tabs > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED); tabLayout.setTabMode(tabs > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
tabLayout.setupWithViewPager(viewPager); tabLayout.setupWithViewPager(viewPager);
restoreSelectedTab();
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);
}
});
}
}
} }
public void addTab(@Nonnull Category category, @Nonnull FragmentTab tab) { public void addTab(@Nonnull Category category, @Nonnull FragmentTab tab) {
@ -103,6 +95,13 @@ public class Tabs {
return adapter.getItem(viewPager.getCurrentItem()); return adapter.getItem(viewPager.getCurrentItem());
} }
public int getCurrentTab() {
if (viewPager == null) {
return -1;
}
return viewPager.getCurrentItem();
}
private final class TabFragments extends FragmentPagerAdapter { private final class TabFragments extends FragmentPagerAdapter {
@Nonnull @Nonnull
@ -152,4 +151,43 @@ public class Tabs {
return Fragment.instantiate(activity, fragmentClass.getName(), fragmentArgs); 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();
}
} }