From aa47871f5f2c4f2f2a5b6a51b6e0b4d9bcc63fc9 Mon Sep 17 00:00:00 2001 From: serso Date: Sun, 21 Feb 2016 23:58:32 +0100 Subject: [PATCH] Toolbars --- app/build.gradle | 1 - .../android/calculator/ActivityUi.java | 16 +++++ .../android/calculator/AppComponent.java | 2 + .../solovyev/android/calculator/ads/AdUi.java | 18 +++-- .../entities/BaseEntitiesFragment.java | 3 - .../floating/FloatingCalculatorActivity.java | 2 +- .../functions/FunctionsActivity.java | 8 +++ .../functions/FunctionsFragment.java | 8 --- .../history/BaseHistoryFragment.java | 31 --------- .../history/EditHistoryFragment.java | 24 ++++--- .../calculator/history/HistoryActivity.java | 54 +++++++++++++-- .../variables/VariablesActivity.java | 9 +++ .../variables/VariablesFragment.java | 10 --- .../android/calculator/view/Tabs.java | 30 +++++++-- .../main/res/layout-land/activity_empty.xml | 65 +++++++++++++++++++ app/src/main/res/layout/activity_empty.xml | 21 ++++-- app/src/main/res/layout/fragment_entities.xml | 22 ++----- app/src/main/res/layout/fragment_history.xml | 15 +---- app/src/main/res/values/styles.xml | 11 ++-- 19 files changed, 226 insertions(+), 124 deletions(-) create mode 100644 app/src/main/res/layout-land/activity_empty.xml diff --git a/app/build.gradle b/app/build.gradle index d076ee1d..3d2934f6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -86,7 +86,6 @@ dependencies { compile 'com.google.android.gms:play-services-ads:8.4.0' compile 'com.google.android.gms:play-services-base:8.4.0' compile 'com.google.android.gms:play-services-analytics:8.4.0' - compile 'com.melnykov:floatingactionbutton:1.1.0' compile(name: 'plotter', ext: 'aar') testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3.1' debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3.1' 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 2a760ede..c55fb913 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/ActivityUi.java @@ -31,7 +31,9 @@ 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; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; @@ -43,6 +45,7 @@ import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; import org.solovyev.android.Activities; +import org.solovyev.android.Check; import org.solovyev.android.Views; import org.solovyev.android.calculator.history.History; import org.solovyev.android.calculator.language.Language; @@ -79,6 +82,9 @@ public class ActivityUi { @Nullable @Bind(R.id.toolbar) Toolbar toolbar; + @Nullable + @Bind(R.id.fab) + FloatingActionButton fab; @Nonnull private Preferences.Gui.Theme theme = Preferences.Gui.Theme.material_theme; @Nonnull @@ -337,4 +343,14 @@ public class ActivityUi { public Tabs getTabs() { return tabs; } + + public void withFab(@DrawableRes int icon, @Nonnull View.OnClickListener listener) { + if (fab == null) { + Check.shouldNotHappen(); + return; + } + fab.setVisibility(View.VISIBLE); + fab.setImageResource(icon); + fab.setOnClickListener(listener); + } } 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 3defbb88..a300aa2e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AppComponent.java +++ b/app/src/main/java/org/solovyev/android/calculator/AppComponent.java @@ -8,6 +8,7 @@ import org.solovyev.android.calculator.functions.EditFunctionFragment; import org.solovyev.android.calculator.functions.FunctionsFragment; import org.solovyev.android.calculator.history.BaseHistoryFragment; import org.solovyev.android.calculator.history.EditHistoryFragment; +import org.solovyev.android.calculator.history.HistoryActivity; import org.solovyev.android.calculator.keyboard.BaseKeyboardUi; import org.solovyev.android.calculator.floating.FloatingCalculatorView; import org.solovyev.android.calculator.operators.OperatorsFragment; @@ -49,4 +50,5 @@ public interface AppComponent { void inject(FloatingCalculatorView view); void inject(DragButtonWizardStep fragment); void inject(BaseFragment fragment); + void inject(HistoryActivity activity); } diff --git a/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java b/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java index 57902b21..b524aa0a 100644 --- a/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java +++ b/app/src/main/java/org/solovyev/android/calculator/ads/AdUi.java @@ -44,13 +44,17 @@ public class AdUi { return; } adView.resume(); - checkout.loadInventory().whenLoaded(onMainThread(new Inventory.Listener() { - @Override - public void onLoaded(@Nonnull Inventory.Products products) { - adFree = products.get(IN_APP).isPurchased("ad_free"); - updateAdView(); - } - })); + if (adFree != null) { + updateAdView(); + } else { + checkout.loadInventory().whenLoaded(onMainThread(new Inventory.Listener() { + @Override + public void onLoaded(@Nonnull Inventory.Products products) { + adFree = products.get(IN_APP).isPurchased("ad_free"); + updateAdView(); + } + })); + } } private void updateAdView() { diff --git a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java index afb28eaf..1c56a67f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/entities/BaseEntitiesFragment.java @@ -35,7 +35,6 @@ import android.view.*; import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; -import com.melnykov.fab.FloatingActionButton; import org.solovyev.android.Check; import org.solovyev.android.calculator.*; import org.solovyev.android.views.llm.DividerItemDecoration; @@ -61,8 +60,6 @@ public abstract class BaseEntitiesFragment extends BaseFra } }; - @Bind(R.id.entities_fab) - public FloatingActionButton fab; @Bind(R.id.entities_recyclerview) public RecyclerView recyclerView; @Inject diff --git a/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java b/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java index 1618e204..11edd71f 100644 --- a/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/floating/FloatingCalculatorActivity.java @@ -47,7 +47,7 @@ public class FloatingCalculatorActivity extends AppCompatActivity { final String permission = getString(R.string.cpp_permission_overlay); builder.setMessage(getString(R.string.cpp_missing_permission_msg, permission)); builder.setTitle(R.string.cpp_missing_permission_title); - builder.setPositiveButton(R.string.ok, this); + builder.setPositiveButton(R.string.ok, null); } @Override 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 29cadc00..bb4bf6f4 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 @@ -23,6 +23,7 @@ package org.solovyev.android.calculator.functions; import android.os.Bundle; +import android.view.View; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.FragmentTab; import org.solovyev.android.calculator.R; @@ -46,6 +47,13 @@ public class FunctionsActivity extends BaseActivity { EditFunctionFragment.show(function, this); } } + + ui.withFab(R.drawable.ic_add_white_36dp, new View.OnClickListener() { + @Override + public void onClick(View v) { + EditFunctionFragment.show(FunctionsActivity.this); + } + }); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java index e4f9b432..d84355ff 100644 --- a/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/functions/FunctionsFragment.java @@ -64,14 +64,6 @@ public class FunctionsFragment extends BaseEntitiesFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = super.onCreateView(inflater, container, savedInstanceState); bus.register(this); - fab.setVisibility(View.VISIBLE); - fab.attachToRecyclerView(recyclerView); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - EditFunctionFragment.show(getActivity()); - } - }); return view; } diff --git a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java index f98ee7ed..13668e1c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/BaseHistoryFragment.java @@ -24,11 +24,9 @@ package org.solovyev.android.calculator.history; import android.app.Activity; import android.content.Context; -import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.FragmentActivity; -import android.support.v7.app.AlertDialog; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.ClipboardManager; @@ -37,7 +35,6 @@ import android.view.*; import android.widget.TextView; import butterknife.Bind; import butterknife.ButterKnife; -import com.melnykov.fab.FloatingActionButton; import com.squareup.otto.Bus; import com.squareup.otto.Subscribe; import org.solovyev.android.Check; @@ -64,8 +61,6 @@ public abstract class BaseHistoryFragment extends BaseFragment { Bus bus; @Bind(R.id.history_recyclerview) RecyclerView recyclerView; - @Bind(R.id.history_fab) - FloatingActionButton fab; private HistoryAdapter adapter; protected BaseHistoryFragment(boolean recentHistory) { @@ -107,35 +102,9 @@ public abstract class BaseHistoryFragment extends BaseFragment { recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)); recyclerView.setAdapter(adapter); recyclerView.addItemDecoration(new DividerItemDecoration(context, null)); - fab.attachToRecyclerView(recyclerView); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - showClearHistoryDialog(); - } - }); return view; } - private void showClearHistoryDialog() { - new AlertDialog.Builder(getActivity(), App.getTheme().alertDialogTheme) - .setTitle(R.string.cpp_clear_history_title) - .setMessage(R.string.cpp_clear_history_message) - .setPositiveButton(R.string.cpp_clear_history, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if (recentHistory) { - history.clearRecent(); - } else { - history.clearSaved(); - } - } - }) - .setNegativeButton(R.string.c_cancel, null) - .create() - .show(); - } - @SuppressWarnings("deprecation") protected final void copyResult(@Nonnull HistoryState state) { final Context context = getActivity(); diff --git a/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java b/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java index fc46de4f..30c9efc3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/EditHistoryFragment.java @@ -12,16 +12,14 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; import android.widget.TextView; - +import butterknife.Bind; +import butterknife.ButterKnife; import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.BaseDialogFragment; import org.solovyev.android.calculator.R; import javax.inject.Inject; -import butterknife.Bind; -import butterknife.ButterKnife; - public class EditHistoryFragment extends BaseDialogFragment { public static final String ARG_STATE = "state"; @@ -74,14 +72,22 @@ public class EditHistoryFragment extends BaseDialogFragment { @Override protected void onPrepareDialog(@NonNull AlertDialog.Builder builder) { builder.setNegativeButton(R.string.c_cancel, null); - builder.setPositiveButton(R.string.c_save, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { + builder.setPositiveButton(R.string.c_save, null); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + switch (which) { + case DialogInterface.BUTTON_POSITIVE: final HistoryState.Builder b = HistoryState.builder(state, newState) .withComment(commentView.getText().toString()); history.updateSaved(b.build()); - } - }); + dismiss(); + break; + default: + super.onClick(dialog, which); + break; + } } @SuppressLint("InflateParams") diff --git a/app/src/main/java/org/solovyev/android/calculator/history/HistoryActivity.java b/app/src/main/java/org/solovyev/android/calculator/history/HistoryActivity.java index 4baf2438..63109b87 100644 --- a/app/src/main/java/org/solovyev/android/calculator/history/HistoryActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/history/HistoryActivity.java @@ -22,23 +22,67 @@ package org.solovyev.android.calculator.history; +import android.content.DialogInterface; import android.os.Bundle; - -import org.solovyev.android.calculator.BaseActivity; +import android.support.v4.app.Fragment; +import android.support.v7.app.AlertDialog; +import android.view.View; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.view.Tabs; import javax.annotation.Nonnull; -import javax.annotation.Nullable; +import javax.inject.Inject; -import static org.solovyev.android.calculator.FragmentTab.history; import static org.solovyev.android.calculator.FragmentTab.saved_history; public class HistoryActivity extends BaseActivity { + @Inject + History history; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + ui.withFab(R.drawable.ic_delete_white_36dp, new View.OnClickListener() { + @Override + public void onClick(View v) { + final Fragment fragment = ui.getTabs().getCurrentFragment(); + showClearHistoryDialog(fragment instanceof RecentHistoryFragment); + } + }); + } + + @Override + protected void inject(@Nonnull AppComponent component) { + super.inject(component); + component.inject(this); + } + @Override protected void populateTabs(@Nonnull Tabs tabs) { super.populateTabs(tabs); - tabs.addTab(history); + tabs.addTab(FragmentTab.history); tabs.addTab(saved_history); } + + private void showClearHistoryDialog(final boolean recentHistory) { + new AlertDialog.Builder(this, App.getTheme().alertDialogTheme) + .setTitle(R.string.cpp_clear_history_title) + .setMessage(R.string.cpp_clear_history_message) + .setPositiveButton(R.string.cpp_clear_history, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (recentHistory) { + history.clearRecent(); + } else { + history.clearSaved(); + } + } + }) + .setNegativeButton(R.string.c_cancel, null) + .create() + .show(); + } + } 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 6757619f..ca5949f4 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 @@ -23,8 +23,10 @@ package org.solovyev.android.calculator.variables; import android.os.Bundle; +import android.view.View; import org.solovyev.android.calculator.BaseActivity; import org.solovyev.android.calculator.FragmentTab; +import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.view.Tabs; import javax.annotation.Nonnull; @@ -45,6 +47,13 @@ public class VariablesActivity extends BaseActivity { EditVariableFragment.showDialog(variable, this); } } + + ui.withFab(R.drawable.ic_add_white_36dp, new View.OnClickListener() { + @Override + public void onClick(View v) { + EditVariableFragment.showDialog(VariablesActivity.this); + } + }); } @Override diff --git a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java index 1cce1599..550093ec 100644 --- a/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/variables/VariablesFragment.java @@ -46,8 +46,6 @@ import javax.inject.Inject; import java.util.ArrayList; import java.util.List; -import static org.solovyev.android.calculator.FragmentTab.variables; - public class VariablesFragment extends BaseEntitiesFragment { @Inject @@ -76,14 +74,6 @@ public class VariablesFragment extends BaseEntitiesFragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View view = super.onCreateView(inflater, container, savedInstanceState); bus.register(this); - fab.setVisibility(View.VISIBLE); - fab.attachToRecyclerView(recyclerView); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - EditVariableFragment.showDialog(null, getFragmentManager()); - } - }); return view; } 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 980bdaba..705c7da1 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 @@ -9,6 +9,7 @@ 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; @@ -32,8 +33,8 @@ public class Tabs { @Bind(R.id.tabs) TabLayout tabLayout; @Nullable - @Bind(R.id.viewpager) - ViewPager viewpager; + @Bind(R.id.viewPager) + ViewPager viewPager; public Tabs(@Nonnull AppCompatActivity activity) { this.activity = activity; @@ -43,14 +44,21 @@ public class Tabs { public void onCreate() { ButterKnife.bind(this, activity); - if (tabLayout == null || viewpager == null) { + if (tabLayout == null || viewPager == null) { return; } - viewpager.setAdapter(adapter); - tabLayout.setupWithViewPager(viewpager); + final int tabs = adapter.getCount(); + if (tabs == 0) { + tabLayout.setVisibility(View.GONE); + return; + } + viewPager.setAdapter(adapter); + tabLayout.setTabMode(tabs > 3 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED); + tabLayout.setupWithViewPager(viewPager); + if (ViewCompat.isLaidOut(tabLayout)) { - tabLayout.setupWithViewPager(viewpager); + tabLayout.setupWithViewPager(viewPager); } else { final ViewTreeObserver treeObserver = Adjuster.getTreeObserver(tabLayout); if (treeObserver != null) { @@ -62,7 +70,7 @@ public class Tabs { //noinspection deprecation anotherTreeObserver.removeGlobalOnLayoutListener(this); } - tabLayout.setupWithViewPager(viewpager); + tabLayout.setupWithViewPager(viewPager); } }); } @@ -87,6 +95,14 @@ public class Tabs { adapter.add(new TabFragment(fragmentClass, fragmentArgs, title)); } + @Nullable + public Fragment getCurrentFragment() { + if (viewPager == null) { + return null; + } + return adapter.getItem(viewPager.getCurrentItem()); + } + private final class TabFragments extends FragmentPagerAdapter { @Nonnull diff --git a/app/src/main/res/layout-land/activity_empty.xml b/app/src/main/res/layout-land/activity_empty.xml new file mode 100644 index 00000000..dc12f1d4 --- /dev/null +++ b/app/src/main/res/layout-land/activity_empty.xml @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_empty.xml b/app/src/main/res/layout/activity_empty.xml index a5c29c61..b0e93f1b 100644 --- a/app/src/main/res/layout/activity_empty.xml +++ b/app/src/main/res/layout/activity_empty.xml @@ -30,6 +30,12 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + @@ -43,13 +49,16 @@ + android:layout_height="wrap_content" + android:overScrollMode="never" + app:tabGravity="fill" + app:tabMaxWidth="0dp" + app:tabMode="fixed" /> - + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_entities.xml b/app/src/main/res/layout/fragment_entities.xml index 41f8f48c..b2186eaf 100644 --- a/app/src/main/res/layout/fragment_entities.xml +++ b/app/src/main/res/layout/fragment_entities.xml @@ -22,28 +22,16 @@ ~ Site: http://se.solovyev.org --> - - - - - - - + a:layout_height="match_parent" /> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_history.xml b/app/src/main/res/layout/fragment_history.xml index c78558e2..e64dfaa0 100644 --- a/app/src/main/res/layout/fragment_history.xml +++ b/app/src/main/res/layout/fragment_history.xml @@ -31,18 +31,9 @@ - + a:layout_height="match_parent" /> - - - - \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 15b08291..694352f6 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -279,9 +279,8 @@ wrap_content bottom|end 16dp - ?attr/cpp_fab_bg - ?attr/cpp_fab_bg - ?attr/colorControlHighlight + gone + ?attr/cpp_fab_bg - -