From 84805e5475486d63db6edb1d7c828417abd1b449 Mon Sep 17 00:00:00 2001 From: serso Date: Tue, 15 Mar 2016 22:00:19 +0100 Subject: [PATCH] Select tabs by default --- .../functions/FunctionsActivity.java | 3 +- .../variables/VariablesActivity.java | 3 +- .../android/calculator/view/Tabs.java | 98 ++++++++++--------- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsActivity.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsActivity.java index 82ec8d2d..44edd40e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsActivity.java @@ -26,7 +26,6 @@ import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.view.View; - import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.FragmentTab; @@ -35,6 +34,7 @@ import org.solovyev.android.calculator.operators.OperatorCategory; import org.solovyev.android.calculator.view.Tabs; import javax.annotation.Nonnull; +import java.util.Arrays; public class FunctionsActivity extends BaseActivity { @@ -76,6 +76,7 @@ public class FunctionsActivity extends BaseActivity { for (FunctionCategory category : FunctionCategory.values()) { tabs.addTab(category, FragmentTab.functions); } + tabs.setDefaultSelectedTab(Arrays.asList(FunctionCategory.values()).indexOf(FunctionCategory.trigonometric)); for (OperatorCategory category : OperatorCategory.values()) { final String title; diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesActivity.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesActivity.java index 6567f20c..ab0d1a80 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesActivity.java @@ -26,7 +26,6 @@ import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.view.View; - import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.FragmentTab; @@ -35,6 +34,7 @@ import org.solovyev.android.calculator.view.Tabs; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.Arrays; public class VariablesActivity extends BaseActivity { @@ -75,6 +75,7 @@ public class VariablesActivity extends BaseActivity { for (VariableCategory category : VariableCategory.values()) { tabs.addTab(category, FragmentTab.variables); } + tabs.setDefaultSelectedTab(Arrays.asList(VariableCategory.values()).indexOf(VariableCategory.system)); } public static final class Dialog extends VariablesActivity { 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 431e9950..6cb60dc1 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,7 +1,5 @@ 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; @@ -13,22 +11,21 @@ import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; - +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 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; +import java.util.ArrayList; +import java.util.List; + +import static org.solovyev.android.calculator.App.cast; public class Tabs { @@ -45,12 +42,18 @@ public class Tabs { @Nullable @Bind(R.id.viewPager) ViewPager viewPager; + private int defaultSelectedTab = -1; public Tabs(@Nonnull AppCompatActivity activity) { this.activity = activity; this.adapter = new TabFragments(activity.getSupportFragmentManager()); } + @Nonnull + private static String makeTabKey(@Nonnull Activity activity) { + return activity.getClass().getSimpleName(); + } + public void onCreate() { cast(activity.getApplicationContext()).getComponent().inject(this); ButterKnife.bind(this, activity); @@ -102,6 +105,44 @@ public class Tabs { return viewPager.getCurrentItem(); } + 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 setDefaultSelectedTab(int defaultSelectedTab) { + this.defaultSelectedTab = defaultSelectedTab; + } + + public void restoreSelectedTab() { + final int selectedTab = preferences.getInt(makeTabKey(activity), defaultSelectedTab); + 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(); + } + } + private final class TabFragments extends FragmentPagerAdapter { @Nonnull @@ -151,43 +192,4 @@ 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(); - } }