CalculatorFragmentType -> FragmentTab

This commit is contained in:
serso 2016-02-06 10:48:44 +01:00
parent 05024841e7
commit dbf4c14d42
21 changed files with 66 additions and 102 deletions

View File

@ -195,14 +195,14 @@ public class ActivityUi extends BaseUi {
if (selectedNavigationIndex >= 0) {
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
final SharedPreferences.Editor editor = preferences.edit();
editor.putInt(getSavedTabPreferenceName(activity), selectedNavigationIndex);
editor.putInt(makeLastTabKey(activity), selectedNavigationIndex);
editor.apply();
}
}
}
@Nonnull
private String getSavedTabPreferenceName(@Nonnull Activity activity) {
private static String makeLastTabKey(@Nonnull Activity activity) {
return "tab_" + activity.getClass().getSimpleName();
}
@ -246,18 +246,18 @@ public class ActivityUi extends BaseUi {
actionBar.addTab(tab);
}
public void addTab(@Nonnull AppCompatActivity activity, @Nonnull CalculatorFragmentType fragmentType, @Nullable Bundle fragmentArgs, int parentViewId) {
addTab(activity, fragmentType.getFragmentTag(), fragmentType.getFragmentClass(), fragmentArgs, fragmentType.getDefaultTitleResId(), parentViewId);
public void addTab(@Nonnull AppCompatActivity activity, @Nonnull FragmentTab tab, @Nullable Bundle fragmentArgs, int parentViewId) {
addTab(activity, tab.tag, tab.type, fragmentArgs, tab.title, parentViewId);
}
public void setFragment(@Nonnull AppCompatActivity activity, @Nonnull CalculatorFragmentType fragmentType, @Nullable Bundle fragmentArgs, int parentViewId) {
public void setFragment(@Nonnull AppCompatActivity activity, @Nonnull FragmentTab tab, @Nullable Bundle fragmentArgs, int parentViewId) {
final FragmentManager fm = activity.getSupportFragmentManager();
Fragment fragment = fm.findFragmentByTag(fragmentType.getFragmentTag());
Fragment fragment = fm.findFragmentByTag(tab.tag);
if (fragment == null) {
fragment = Fragment.instantiate(activity, fragmentType.getFragmentClass().getName(), fragmentArgs);
fragment = Fragment.instantiate(activity, tab.type.getName(), fragmentArgs);
final FragmentTransaction ft = fm.beginTransaction();
ft.add(parentViewId, fragment, fragmentType.getFragmentTag());
ft.add(parentViewId, fragment, tab.tag);
ft.commit();
} else {
if (fragment.isDetached()) {
@ -265,25 +265,9 @@ public class ActivityUi extends BaseUi {
ft.attach(fragment);
ft.commit();
}
}
}
public void selectTab(@Nonnull AppCompatActivity activity, @Nonnull CalculatorFragmentType fragmentType) {
final ActionBar actionBar = activity.getSupportActionBar();
for (int i = 0; i < actionBar.getTabCount(); i++) {
final ActionBar.Tab tab = actionBar.getTabAt(i);
if (tab != null && fragmentType.getFragmentTag().equals(tab.getTag())) {
actionBar.setSelectedNavigationItem(i);
break;
}
}
}
public int getLayoutId() {
return layoutId;
}
public void setLayoutId(int layoutId) {
this.layoutId = layoutId;
}
@ -302,7 +286,7 @@ public class ActivityUi extends BaseUi {
onResume((Activity) activity);
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity);
selectedNavigationIndex = preferences.getInt(getSavedTabPreferenceName(activity), -1);
selectedNavigationIndex = preferences.getInt(makeLastTabKey(activity), -1);
restoreSavedTab(activity);
}

View File

@ -2,7 +2,6 @@ package org.solovyev.android.calculator;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
@ -102,16 +101,15 @@ public class BaseActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
protected final void addTab(@Nonnull Category category, @Nonnull CalculatorFragmentType type) {
protected final void addTab(@Nonnull Category category, @Nonnull FragmentTab tab) {
final CharSequence title = getString(category.title());
addTab(category, type, title);
addTab(category, tab, title);
}
protected final void addTab(@Nonnull Category category, @Nonnull CalculatorFragmentType type, @Nullable CharSequence title) {
protected final void addTab(@Nonnull Category category, @Nonnull FragmentTab tab, @Nullable CharSequence title) {
final Bundle arguments = new Bundle(1);
arguments.putString(BaseEntitiesFragment.ARG_CATEGORY, category.name());
final String fragmentTag = type.createSubFragmentTag(category.name());
final Class<? extends Fragment> fragmentClass = type.getFragmentClass();
ui.addTab(this, fragmentTag, fragmentClass, arguments, title, R.id.main_layout);
final String fragmentTag = tab.subTag(category.name());
ui.addTab(this, fragmentTag, tab.type, arguments, title, R.id.main_layout);
}
}

View File

@ -26,8 +26,8 @@ public abstract class BaseFragment extends Fragment {
protected abstract FragmentUi createUi();
@Nonnull
protected final FragmentUi createUi(@Nonnull CalculatorFragmentType type) {
return new FragmentUi(type.getDefaultLayoutId(), type.getDefaultTitleResId(), false);
protected final FragmentUi createUi(@Nonnull FragmentTab tab) {
return new FragmentUi(tab.layout, tab.title, false);
}
protected void inject(@Nonnull AppComponent component) {

View File

@ -158,11 +158,11 @@ public class CalculatorActivity extends BaseActivity implements SharedPreference
super.onCreate(savedInstanceState);
if (isMultiPane()) {
ui.addTab(this, CalculatorFragmentType.history, null, R.id.main_second_pane);
ui.addTab(this, CalculatorFragmentType.saved_history, null, R.id.main_second_pane);
ui.addTab(this, CalculatorFragmentType.variables, null, R.id.main_second_pane);
ui.addTab(this, CalculatorFragmentType.functions, null, R.id.main_second_pane);
ui.addTab(this, CalculatorFragmentType.operators, null, R.id.main_second_pane);
ui.addTab(this, FragmentTab.history, null, R.id.main_second_pane);
ui.addTab(this, FragmentTab.saved_history, null, R.id.main_second_pane);
ui.addTab(this, FragmentTab.variables, null, R.id.main_second_pane);
ui.addTab(this, FragmentTab.functions, null, R.id.main_second_pane);
ui.addTab(this, FragmentTab.operators, null, R.id.main_second_pane);
} else {
final ActionBar actionBar = getSupportActionBar();
if (Build.VERSION.SDK_INT <= GINGERBREAD_MR1 || (Build.VERSION.SDK_INT >= ICE_CREAM_SANDWICH && hasPermanentMenuKey())) {

View File

@ -22,6 +22,8 @@
package org.solovyev.android.calculator;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import org.solovyev.android.calculator.about.AboutFragment;
@ -36,12 +38,7 @@ import org.solovyev.android.calculator.preferences.PurchaseDialogActivity;
import javax.annotation.Nonnull;
/**
* User: Solovyev_S
* Date: 03.10.12
* Time: 11:30
*/
public enum CalculatorFragmentType {
public enum FragmentTab {
editor(EditorFragment.class, R.layout.cpp_app_editor, R.string.editor),
//display(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history),
@ -60,39 +57,24 @@ public enum CalculatorFragmentType {
matrix_edit(EditMatrixFragment.class, R.layout.matrix_edit_fragment, R.string.c_release_notes),
release_notes(ReleaseNotesFragment.class, R.layout.release_notes_fragment, R.string.c_release_notes);
private final int defaultLayoutId;
@LayoutRes
public final int layout;
@Nonnull
private Class<? extends Fragment> fragmentClass;
private int defaultTitleResId;
public final Class<? extends Fragment> type;
@StringRes
public final int title;
@Nonnull
public final String tag;
CalculatorFragmentType(@Nonnull Class<? extends Fragment> fragmentClass,
int defaultLayoutId,
int defaultTitleResId) {
this.fragmentClass = fragmentClass;
this.defaultLayoutId = defaultLayoutId;
this.defaultTitleResId = defaultTitleResId;
FragmentTab(@Nonnull Class<? extends Fragment> type, int layout, int title) {
this.type = type;
this.layout = layout;
this.title = title;
this.tag = name();
}
@Nonnull
public String getFragmentTag() {
return this.name();
}
public int getDefaultTitleResId() {
return defaultTitleResId;
}
@Nonnull
public Class<? extends Fragment> getFragmentClass() {
return fragmentClass;
}
public int getDefaultLayoutId() {
return defaultLayoutId;
}
@Nonnull
public String createSubFragmentTag(@Nonnull String subFragmentTag) {
return this.getFragmentTag() + "_" + subFragmentTag;
public String subTag(@Nonnull String subTag) {
return tag + "_" + subTag;
}
}

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.about;
import android.os.Bundle;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.EmptyActivity;
import org.solovyev.android.calculator.R;
@ -41,7 +41,7 @@ public class AboutActivity extends EmptyActivity {
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getUi().addTab(this, CalculatorFragmentType.about, null, R.id.main_layout);
getUi().addTab(this, CalculatorFragmentType.release_notes, null, R.id.main_layout);
getUi().addTab(this, FragmentTab.about, null, R.id.main_layout);
getUi().addTab(this, FragmentTab.release_notes, null, R.id.main_layout);
}
}

View File

@ -38,7 +38,7 @@ import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
import static android.view.View.GONE;
import static org.solovyev.android.calculator.CalculatorFragmentType.about;
import static org.solovyev.android.calculator.FragmentTab.about;
import static org.solovyev.common.text.Strings.isEmpty;
public class AboutFragment extends BaseFragment {

View File

@ -38,7 +38,7 @@ import org.solovyev.android.calculator.release.ReleaseNotes;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.CalculatorFragmentType.release_notes;
import static org.solovyev.android.calculator.FragmentTab.release_notes;
public class ReleaseNotesFragment extends BaseFragment {

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.functions;
import android.os.Bundle;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.operators.OperatorCategory;
@ -43,7 +43,7 @@ public class FunctionsActivity extends BaseActivity {
super.onCreate(savedInstanceState);
for (FunctionCategory category : FunctionCategory.values()) {
addTab(category, CalculatorFragmentType.functions);
addTab(category, FragmentTab.functions);
}
for (OperatorCategory category : OperatorCategory.values()) {
@ -53,7 +53,7 @@ public class FunctionsActivity extends BaseActivity {
} else {
title = getString(category.title());
}
addTab(category, CalculatorFragmentType.operators, title);
addTab(category, FragmentTab.operators, title);
}
if (savedInstanceState == null) {

View File

@ -43,7 +43,7 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.CalculatorFragmentType.functions;
import static org.solovyev.android.calculator.FragmentTab.functions;
public class FunctionsFragment extends BaseEntitiesFragment<Function> {

View File

@ -68,8 +68,8 @@ public abstract class BaseHistoryFragment extends BaseFragment {
FloatingActionButton fab;
private HistoryAdapter adapter;
protected BaseHistoryFragment(@Nonnull CalculatorFragmentType type) {
recentHistory = type == CalculatorFragmentType.history;
protected BaseHistoryFragment(boolean recentHistory) {
this.recentHistory = recentHistory;
}
@Override

View File

@ -29,8 +29,8 @@ import org.solovyev.android.calculator.R;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.CalculatorFragmentType.history;
import static org.solovyev.android.calculator.CalculatorFragmentType.saved_history;
import static org.solovyev.android.calculator.FragmentTab.history;
import static org.solovyev.android.calculator.FragmentTab.saved_history;
public class HistoryActivity extends BaseActivity {

View File

@ -22,7 +22,7 @@
package org.solovyev.android.calculator.history;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.FragmentUi;
import javax.annotation.Nonnull;
@ -30,12 +30,12 @@ import javax.annotation.Nonnull;
public class RecentHistoryFragment extends BaseHistoryFragment {
public RecentHistoryFragment() {
super(CalculatorFragmentType.history);
super(true);
}
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(CalculatorFragmentType.history);
return createUi(FragmentTab.history);
}
}

View File

@ -26,12 +26,12 @@ import org.solovyev.android.calculator.FragmentUi;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.CalculatorFragmentType.saved_history;
import static org.solovyev.android.calculator.FragmentTab.saved_history;
public class SavedHistoryFragment extends BaseHistoryFragment {
public SavedHistoryFragment() {
super(saved_history);
super(false);
}
@Nonnull

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.matrix;
import android.os.Bundle;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.EmptyActivity;
import org.solovyev.android.calculator.R;
@ -42,6 +42,6 @@ public class CalculatorMatrixActivity extends EmptyActivity {
super.onCreate(savedInstanceState);
getSupportActionBar().setNavigationMode(NAVIGATION_MODE_STANDARD);
getUi().setFragment(this, CalculatorFragmentType.matrix_edit, null, R.id.main_layout);
getUi().setFragment(this, FragmentTab.matrix_edit, null, R.id.main_layout);
}
}

View File

@ -32,7 +32,7 @@ import org.solovyev.android.view.Picker;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.CalculatorFragmentType.matrix_edit;
import static org.solovyev.android.calculator.FragmentTab.matrix_edit;
public class EditMatrixFragment extends BaseFragment implements Picker.OnChangedListener<Integer> {
private static final int MAX_COUNT = 10;

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.operators;
import android.os.Bundle;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.R;
import javax.annotation.Nullable;
@ -40,7 +40,7 @@ public class OperatorsActivity extends BaseActivity {
super.onCreate(savedInstanceState);
for (OperatorCategory category : OperatorCategory.values()) {
addTab(category, CalculatorFragmentType.operators);
addTab(category, FragmentTab.operators);
}
}
}

View File

@ -37,7 +37,7 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.CalculatorFragmentType.operators;
import static org.solovyev.android.calculator.FragmentTab.operators;
public class OperatorsFragment extends BaseEntitiesFragment<Operator> {

View File

@ -45,7 +45,7 @@ import org.solovyev.android.fragments.FragmentUtils;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.CalculatorFragmentType.purchase_dialog;
import static org.solovyev.android.calculator.FragmentTab.purchase_dialog;
public class PurchaseDialogActivity extends BaseActivity {

View File

@ -24,7 +24,7 @@ package org.solovyev.android.calculator.variables;
import android.os.Bundle;
import org.solovyev.android.calculator.BaseActivity;
import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.FragmentTab;
import org.solovyev.android.calculator.R;
import javax.annotation.Nullable;
@ -42,7 +42,7 @@ public class VariablesActivity extends BaseActivity {
super.onCreate(savedInstanceState);
for (VariableCategory category : VariableCategory.values()) {
addTab(category, CalculatorFragmentType.variables);
addTab(category, FragmentTab.variables);
}
if (savedInstanceState == null) {

View File

@ -46,7 +46,7 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.CalculatorFragmentType.variables;
import static org.solovyev.android.calculator.FragmentTab.variables;
public class VariablesFragment extends BaseEntitiesFragment<IConstant> {