Select tabs by default

This commit is contained in:
serso 2016-03-15 22:00:19 +01:00
parent 13bf1bcf41
commit 84805e5475
3 changed files with 54 additions and 50 deletions

View File

@ -26,7 +26,6 @@ import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.view.View; import android.view.View;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.FragmentTab; 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 org.solovyev.android.calculator.view.Tabs;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.Arrays;
public class FunctionsActivity extends BaseActivity { public class FunctionsActivity extends BaseActivity {
@ -76,6 +76,7 @@ public class FunctionsActivity extends BaseActivity {
for (FunctionCategory category : FunctionCategory.values()) { for (FunctionCategory category : FunctionCategory.values()) {
tabs.addTab(category, FragmentTab.functions); tabs.addTab(category, FragmentTab.functions);
} }
tabs.setDefaultSelectedTab(Arrays.asList(FunctionCategory.values()).indexOf(FunctionCategory.trigonometric));
for (OperatorCategory category : OperatorCategory.values()) { for (OperatorCategory category : OperatorCategory.values()) {
final String title; final String title;

View File

@ -26,7 +26,6 @@ import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.view.View; import android.view.View;
import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.FragmentTab; import org.solovyev.android.calculator.FragmentTab;
@ -35,6 +34,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 java.util.Arrays;
public class VariablesActivity extends BaseActivity { public class VariablesActivity extends BaseActivity {
@ -75,6 +75,7 @@ public class VariablesActivity extends BaseActivity {
for (VariableCategory category : VariableCategory.values()) { for (VariableCategory category : VariableCategory.values()) {
tabs.addTab(category, FragmentTab.variables); tabs.addTab(category, FragmentTab.variables);
} }
tabs.setDefaultSelectedTab(Arrays.asList(VariableCategory.values()).indexOf(VariableCategory.system));
} }
public static final class Dialog extends VariablesActivity { public static final class Dialog extends VariablesActivity {

View File

@ -1,7 +1,5 @@
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.app.Activity;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
@ -13,22 +11,21 @@ import android.support.v4.app.FragmentPagerAdapter;
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 butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.calculator.AppModule; import org.solovyev.android.calculator.AppModule;
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 butterknife.Bind;
import butterknife.ButterKnife;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.App.cast;
public class Tabs { public class Tabs {
@ -45,12 +42,18 @@ public class Tabs {
@Nullable @Nullable
@Bind(R.id.viewPager) @Bind(R.id.viewPager)
ViewPager viewPager; ViewPager viewPager;
private int defaultSelectedTab = -1;
public Tabs(@Nonnull AppCompatActivity activity) { public Tabs(@Nonnull AppCompatActivity activity) {
this.activity = activity; this.activity = activity;
this.adapter = new TabFragments(activity.getSupportFragmentManager()); this.adapter = new TabFragments(activity.getSupportFragmentManager());
} }
@Nonnull
private static String makeTabKey(@Nonnull Activity activity) {
return activity.getClass().getSimpleName();
}
public void onCreate() { public void onCreate() {
cast(activity.getApplicationContext()).getComponent().inject(this); cast(activity.getApplicationContext()).getComponent().inject(this);
ButterKnife.bind(this, activity); ButterKnife.bind(this, activity);
@ -102,6 +105,44 @@ public class Tabs {
return viewPager.getCurrentItem(); 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 { private final class TabFragments extends FragmentPagerAdapter {
@Nonnull @Nonnull
@ -151,43 +192,4 @@ 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();
}
} }