FragmentUi removed, AdUi added

This commit is contained in:
serso 2016-02-20 14:34:23 +01:00
parent 07d3ebd65c
commit 8b87de2f29
19 changed files with 199 additions and 281 deletions

View File

@ -27,7 +27,6 @@ public interface AppComponent {
void inject(CalculatorApplication application);
void inject(EditorFragment fragment);
void inject(BaseUi ui);
void inject(FragmentUi ui);
void inject(FloatingCalculatorService service);
void inject(BaseHistoryFragment fragment);
void inject(BaseDialogFragment fragment);
@ -49,4 +48,5 @@ public interface AppComponent {
void inject(BaseKeyboardUi ui);
void inject(FloatingCalculatorView view);
void inject(DragButtonWizardStep fragment);
void inject(BaseFragment fragment);
}

View File

@ -1,71 +1,72 @@
package org.solovyev.android.calculator;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.view.*;
import org.solovyev.android.calculator.ads.AdUi;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static android.view.Menu.NONE;
import static org.solovyev.android.calculator.App.cast;
public abstract class BaseFragment extends Fragment {
protected FragmentUi ui;
private final int layout;
@Inject
AdUi adUi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inject(cast(getActivity().getApplication()).getComponent());
ui = createUi();
ui.onCreate(this);
}
@Nonnull
protected abstract FragmentUi createUi();
@Nonnull
protected final FragmentUi createUi(@Nonnull FragmentTab tab) {
return new FragmentUi(tab.layout);
}
protected void inject(@Nonnull AppComponent component) {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = ui.onCreateView(inflater, container);
ui.onCreateView(this, view);
return view;
}
@Override
public void onResume() {
super.onResume();
ui.onResume();
}
@Override
public void onPause() {
ui.onPause();
super.onPause();
}
@Override
public void onDestroyView() {
ui.onDestroyView();
super.onDestroyView();
}
@Override
public void onDestroy() {
ui.onDestroy();
super.onDestroy();
protected BaseFragment(@LayoutRes int layout) {
this.layout = layout;
}
@Nonnull
public static MenuItem addMenu(@Nonnull ContextMenu menu, @StringRes int label, @Nonnull MenuItem.OnMenuItemClickListener listener) {
return menu.add(NONE, label, NONE, label).setOnMenuItemClickListener(listener);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inject(cast(getActivity().getApplication()).getComponent());
adUi.onCreate();
}
protected void inject(@Nonnull AppComponent component) {
component.inject(this);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(layout, container, false);
adUi.onCreateView(view);
return view;
}
@Override
public void onResume() {
super.onResume();
adUi.onResume();
}
@Override
public void onPause() {
adUi.onPause();
super.onPause();
}
@Override
public void onDestroyView() {
adUi.onDestroyView();
super.onDestroyView();
}
@Override
public void onDestroy() {
adUi.onDestroy();
super.onDestroy();
}
}

View File

@ -26,6 +26,7 @@ import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
@ -93,10 +94,8 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene
@Inject
Calculator calculator;
@Nonnull
@Override
protected FragmentUi createUi() {
return new FragmentUi(R.layout.cpp_app_display);
public DisplayFragment() {
super(R.layout.cpp_app_display);
}
@Override

View File

@ -50,6 +50,10 @@ public class EditorFragment extends BaseFragment {
@Bind(R.id.calculator_editor)
EditorView editorView;
public EditorFragment() {
super(R.layout.cpp_app_editor);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -62,12 +66,6 @@ public class EditorFragment extends BaseFragment {
component.inject(this);
}
@Nonnull
@Override
protected FragmentUi createUi() {
return new FragmentUi(R.layout.cpp_app_editor);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = super.onCreateView(inflater, container, savedInstanceState);
@ -79,7 +77,6 @@ public class EditorFragment extends BaseFragment {
@Override
public void onDestroyView() {
editor.clearView(editorView);
ui.onDestroyView();
super.onDestroyView();
}

View File

@ -22,7 +22,6 @@
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;
@ -38,22 +37,17 @@ import javax.annotation.Nonnull;
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),
//keyboard(CalculatorHistoryFragment.class, "history", R.layout.fragment_history, R.string.c_history),
history(RecentHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_recent),
saved_history(SavedHistoryFragment.class, R.layout.fragment_history, R.string.cpp_history_tab_saved),
variables(VariablesFragment.class, R.layout.fragment_entities, R.string.c_vars),
functions(FunctionsFragment.class, R.layout.fragment_entities, R.string.c_functions),
operators(OperatorsFragment.class, R.layout.fragment_entities, R.string.c_operators),
about(AboutFragment.class, R.layout.about_fragment, R.string.c_about),
history(RecentHistoryFragment.class, R.string.cpp_history_tab_recent),
saved_history(SavedHistoryFragment.class, R.string.cpp_history_tab_saved),
variables(VariablesFragment.class, R.string.c_vars),
functions(FunctionsFragment.class, R.string.c_functions),
operators(OperatorsFragment.class, R.string.c_operators),
about(AboutFragment.class, R.string.c_about),
// todo serso: strings
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);
matrix_edit(EditMatrixFragment.class, R.string.c_release_notes),
release_notes(ReleaseNotesFragment.class, R.string.c_release_notes);
@LayoutRes
public final int layout;
@Nonnull
public final Class<? extends Fragment> type;
@StringRes
@ -61,9 +55,8 @@ public enum FragmentTab {
@Nonnull
public final String tag;
FragmentTab(@Nonnull Class<? extends Fragment> type, int layout, int title) {
FragmentTab(@Nonnull Class<? extends Fragment> type, int title) {
this.type = type;
this.layout = layout;
this.title = title;
this.tag = name();
}

View File

@ -1,117 +0,0 @@
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: se.solovyev@gmail.com
* Site: http://se.solovyev.org
*/
package org.solovyev.android.calculator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.checkout.CppCheckout;
import org.solovyev.android.checkout.Inventory;
import org.solovyev.android.checkout.ProductTypes;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public class FragmentUi {
private final int layoutId;
@Nullable
private Boolean adFree = null;
@Inject
CppCheckout checkout;
@Nullable
@Bind(R.id.admob)
AdView adView;
public FragmentUi(int layoutId) {
this.layoutId = layoutId;
}
public void onCreate(@Nonnull Fragment fragment) {
cast(fragment).getComponent().inject(this);
checkout.start();
}
public void onResume() {
if (adView != null) {
adView.resume();
}
checkout.loadInventory().whenLoaded(new Inventory.Listener() {
@Override
public void onLoaded(@Nonnull Inventory.Products products) {
adFree = products.get(ProductTypes.IN_APP).isPurchased("ad_free");
updateAdView();
}
});
}
private void updateAdView() {
if (adFree == null || adView == null) {
return;
}
if (adFree) {
adView.hide();
} else {
adView.show();
}
}
public void onPause() {
adFree = null;
if (adView != null) {
adView.pause();
}
}
public void onCreateView(@Nonnull Fragment fragment, @Nonnull View root) {
ButterKnife.bind(this, root);
if (fragment instanceof DisplayFragment || fragment instanceof EditorFragment || fragment instanceof KeyboardFragment) {
// no ads in those fragments
} else if (adView != null) {
updateAdView();
}
}
public void onDestroy() {
checkout.stop();
}
@Nonnull
public View onCreateView(@Nonnull LayoutInflater inflater, @Nullable ViewGroup container) {
return inflater.inflate(layoutId, container, false);
}
public void onDestroyView() {
if (adView == null) {
return;
}
adView.destroy();
adView = null;
}
}

View File

@ -41,6 +41,10 @@ public class KeyboardFragment extends BaseFragment {
@Inject
KeyboardUi keyboardUi;
public KeyboardFragment() {
super(R.layout.cpp_app_keyboard);
}
@Override
protected void inject(@Nonnull AppComponent component) {
super.inject(component);
@ -56,12 +60,6 @@ public class KeyboardFragment extends BaseFragment {
return view;
}
@Nonnull
@Override
protected FragmentUi createUi() {
return new FragmentUi(R.layout.cpp_app_keyboard);
}
@Override
public void onDestroyView() {
keyboardUi.onDestroyView();

View File

@ -30,11 +30,6 @@ import org.solovyev.android.calculator.R;
import javax.annotation.Nullable;
/**
* User: serso
* Date: 9/16/11
* Time: 11:52 PM
*/
public class AboutActivity extends EmptyActivity {
@Override

View File

@ -33,13 +33,9 @@ import butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.BaseFragment;
import org.solovyev.android.calculator.FragmentUi;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull;
import static android.view.View.GONE;
import static org.solovyev.android.calculator.FragmentTab.about;
import static org.solovyev.common.text.Strings.isEmpty;
public class AboutFragment extends BaseFragment {
@ -53,10 +49,8 @@ public class AboutFragment extends BaseFragment {
@Bind(R.id.about_translators)
TextView translatorsView;
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(about);
public AboutFragment() {
super(R.layout.about_fragment);
}
@Override

View File

@ -32,23 +32,16 @@ import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.calculator.BaseFragment;
import org.solovyev.android.calculator.FragmentUi;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.release.ReleaseNotes;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.FragmentTab.release_notes;
public class ReleaseNotesFragment extends BaseFragment {
@Bind(R.id.releasenotes_text)
TextView text;
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(release_notes);
public ReleaseNotesFragment() {
super(R.layout.release_notes_fragment);
}
@Override

View File

@ -0,0 +1,109 @@
package org.solovyev.android.calculator.ads;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.view.View;
import butterknife.Bind;
import butterknife.ButterKnife;
import org.solovyev.android.calculator.AdView;
import org.solovyev.android.calculator.R;
import org.solovyev.android.checkout.CppCheckout;
import org.solovyev.android.checkout.Inventory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static org.solovyev.android.checkout.ProductTypes.IN_APP;
public class AdUi {
@NonNull
private final CppCheckout checkout;
@NonNull
private final Handler handler;
@Nullable
@Bind(R.id.ad)
AdView adView;
@Nullable
private Boolean adFree = null;
@Inject
public AdUi(@NonNull CppCheckout checkout, @NonNull Handler handler) {
this.checkout = checkout;
this.handler = handler;
}
public void onCreate() {
checkout.start();
}
public void onResume() {
if (adView == null) {
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();
}
}));
}
private void updateAdView() {
if (adFree == null || adView == null) {
return;
}
if (adFree) {
adView.hide();
} else {
adView.show();
}
}
@Nonnull
private Inventory.Listener onMainThread(@Nonnull final Inventory.Listener listener) {
return new Inventory.Listener() {
@Override
public void onLoaded(@Nonnull final Inventory.Products products) {
if (handler.getLooper() == Looper.myLooper()) {
listener.onLoaded(products);
return;
}
handler.post(new Runnable() {
@Override
public void run() {
listener.onLoaded(products);
}
});
}
};
}
public void onCreateView(@NonNull View view) {
ButterKnife.bind(this, view);
}
public void onPause() {
adFree = null;
if (adView != null) {
adView.pause();
}
}
public void onDestroyView() {
if (adView == null) {
return;
}
adView.destroy();
adView = null;
}
public void onDestroy() {
checkout.stop();
}
}

View File

@ -71,6 +71,10 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
@Nullable
private String category;
public BaseEntitiesFragment() {
super(R.layout.fragment_entities);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -83,7 +87,7 @@ public abstract class BaseEntitiesFragment<E extends MathEntity> extends BaseFra
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = ui.onCreateView(inflater, container);
final View view = super.onCreateView(inflater, container, savedInstanceState);
ButterKnife.bind(this, view);
final Context context = inflater.getContext();
adapter = new EntitiesAdapter(context, TextUtils.isEmpty(category) ? getEntities() : getEntities(category));

View File

@ -32,7 +32,9 @@ import com.squareup.otto.Subscribe;
import jscl.math.function.Function;
import jscl.math.function.IFunction;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.Calculator;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
@ -43,8 +45,6 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.FragmentTab.functions;
public class FunctionsFragment extends BaseEntitiesFragment<Function> {
@Inject
@ -54,12 +54,6 @@ public class FunctionsFragment extends BaseEntitiesFragment<Function> {
@Inject
Bus bus;
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(functions);
}
@Override
protected void inject(@Nonnull AppComponent component) {
super.inject(component);

View File

@ -69,6 +69,7 @@ public abstract class BaseHistoryFragment extends BaseFragment {
private HistoryAdapter adapter;
protected BaseHistoryFragment(boolean recentHistory) {
super(R.layout.fragment_history);
this.recentHistory = recentHistory;
}

View File

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

View File

@ -22,21 +22,9 @@
package org.solovyev.android.calculator.history;
import org.solovyev.android.calculator.FragmentUi;
import javax.annotation.Nonnull;
import static org.solovyev.android.calculator.FragmentTab.saved_history;
public class SavedHistoryFragment extends BaseHistoryFragment {
public SavedHistoryFragment() {
super(false);
}
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(saved_history);
}
}

View File

@ -24,16 +24,14 @@ package org.solovyev.android.calculator.matrix;
import android.os.Bundle;
import android.view.View;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.BaseFragment;
import org.solovyev.android.calculator.R;
import org.solovyev.android.view.IntegerRange;
import org.solovyev.android.view.Picker;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
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;
private static final int MIN_COUNT = 2;
@ -43,15 +41,10 @@ public class EditMatrixFragment extends BaseFragment implements Picker.OnChanged
private static final String MATRIX = "matrix";
public EditMatrixFragment() {
super(R.layout.matrix_edit_fragment);
setRetainInstance(true);
}
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(matrix_edit);
}
@Override
public void onViewCreated(View root, @Nullable Bundle in) {
super.onViewCreated(root, in);

View File

@ -26,7 +26,8 @@ import android.support.annotation.NonNull;
import android.view.ContextMenu;
import android.view.MenuItem;
import jscl.math.operator.Operator;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.AppComponent;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.android.calculator.entities.Category;
import org.solovyev.common.text.Strings;
@ -37,8 +38,6 @@ import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import static org.solovyev.android.calculator.FragmentTab.operators;
public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
@Inject
@ -46,12 +45,6 @@ public class OperatorsFragment extends BaseEntitiesFragment<Operator> {
@Inject
PostfixFunctionsRegistry postfixFunctionsRegistry;
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(operators);
}
@Override
protected void inject(@Nonnull AppComponent component) {
super.inject(component);

View File

@ -32,10 +32,10 @@ import com.squareup.otto.Subscribe;
import jscl.math.function.IConstant;
import org.solovyev.android.Check;
import org.solovyev.android.calculator.*;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.android.calculator.entities.Category;
import org.solovyev.android.calculator.entities.EntityRemovalDialog;
import org.solovyev.android.calculator.math.MathType;
import org.solovyev.android.calculator.entities.BaseEntitiesFragment;
import org.solovyev.common.JPredicate;
import org.solovyev.common.collections.Collections;
import org.solovyev.common.text.Strings;
@ -57,12 +57,6 @@ public class VariablesFragment extends BaseEntitiesFragment<IConstant> {
@Inject
Bus bus;
@Nonnull
@Override
protected FragmentUi createUi() {
return createUi(variables);
}
public static boolean isValidValue(@Nonnull String value) {
try {
final PreparedExpression pe = ToJsclTextProcessor.getInstance().process(value);