App.cast method introduced

This commit is contained in:
serso 2016-02-04 12:15:08 +01:00
parent 89680ef544
commit d8a97112bc
9 changed files with 38 additions and 12 deletions

View File

@ -363,4 +363,17 @@ public final class App {
}
return null;
}
@NonNull
public static CalculatorApplication cast(@NonNull Context context) {
if (context instanceof CalculatorApplication) {
return (CalculatorApplication) context;
}
final Context appContext = context.getApplicationContext();
if (appContext instanceof CalculatorApplication) {
return (CalculatorApplication) appContext;
}
Check.shouldNotHappen();
return (CalculatorApplication) application;
}
}

View File

@ -1,6 +1,5 @@
package org.solovyev.android.calculator;
import dagger.Component;
import org.solovyev.android.calculator.errors.FixableErrorFragment;
import org.solovyev.android.calculator.errors.FixableErrorsActivity;
import org.solovyev.android.calculator.functions.EditFunctionFragment;
@ -14,6 +13,8 @@ import org.solovyev.android.calculator.variables.VariablesFragment;
import javax.inject.Singleton;
import dagger.Component;
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

View File

@ -12,6 +12,8 @@ import org.solovyev.android.calculator.entities.Category;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static org.solovyev.android.calculator.App.cast;
public class BaseActivity extends AppCompatActivity {
@Nonnull
@ -38,7 +40,7 @@ public class BaseActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
ui.onPreCreate(this);
super.onCreate(savedInstanceState);
inject(((CalculatorApplication) getApplication()).getComponent());
inject(cast(getApplication()).getComponent());
ui.onCreate(this);
}

View File

@ -17,6 +17,8 @@ import android.view.inputmethod.InputMethodManager;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public abstract class BaseDialogFragment extends DialogFragment {
@Inject
@ -25,7 +27,7 @@ public abstract class BaseDialogFragment extends DialogFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inject(((CalculatorApplication) getActivity().getApplication()).getComponent());
inject(cast(getActivity().getApplication()).getComponent());
}
protected void inject(@NonNull AppComponent component) {

View File

@ -8,6 +8,7 @@ import android.view.*;
import javax.annotation.Nonnull;
import static android.view.Menu.NONE;
import static org.solovyev.android.calculator.App.cast;
public abstract class BaseFragment extends Fragment {
@ -21,7 +22,7 @@ public abstract class BaseFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
inject(((CalculatorApplication) getActivity().getApplication()).getComponent());
inject(cast(getActivity().getApplication()).getComponent());
ui.onCreate(this);
}

View File

@ -51,6 +51,7 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple;
import static org.solovyev.android.calculator.Preferences.Gui.Layout.simple_mobile;
import static org.solovyev.android.calculator.Engine.Preferences.angleUnit;
@ -129,7 +130,7 @@ public abstract class BaseUi implements SharedPreferences.OnSharedPreferenceChan
PreferredPreferences preferredPreferences;
protected void onCreate(@Nonnull Activity activity) {
((CalculatorApplication) activity.getApplication()).getComponent().inject(this);
cast(activity.getApplication()).getComponent().inject(this);
layout = Preferences.Gui.layout.getPreferenceNoError(preferences);
theme = Preferences.Gui.theme.getPreferenceNoError(preferences);

View File

@ -40,6 +40,8 @@ import org.solovyev.android.menu.ListActivityMenu;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public class CalculatorEditorFragment extends Fragment {
private FragmentUi fragmentUi;
@ -77,7 +79,7 @@ public class CalculatorEditorFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((CalculatorApplication) getActivity().getApplication()).getComponent().inject(this);
cast(getActivity().getApplication()).getComponent().inject(this);
final Preferences.Gui.Layout layout = Preferences.Gui.getLayout(preferences);
if (!layout.optimized) {

View File

@ -29,16 +29,19 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import org.solovyev.android.Activities;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.Preferences;
import org.solovyev.common.msg.Message;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public class FixableErrorsActivity extends AppCompatActivity {
public static final String EXTRA_ERRORS = "errors";
@ -81,7 +84,7 @@ public class FixableErrorsActivity extends AppCompatActivity {
finish();
return;
}
((CalculatorApplication) getApplication()).getComponent().inject(this);
cast(getApplication()).getComponent().inject(this);
if (state == null) {
showNextError();
}

View File

@ -39,7 +39,6 @@ import com.squareup.otto.Subscribe;
import org.solovyev.android.Check;
import org.solovyev.android.Views;
import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.CalculatorApplication;
import org.solovyev.android.calculator.Display;
import org.solovyev.android.calculator.Editor;
import org.solovyev.android.calculator.Preferences;
@ -49,6 +48,8 @@ import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import static org.solovyev.android.calculator.App.cast;
public class CalculatorOnscreenService extends Service implements OnscreenViewListener, SharedPreferences.OnSharedPreferenceChangeListener {
public static final Class<CalculatorOnscreenBroadcastReceiver> INTENT_LISTENER_CLASS = CalculatorOnscreenBroadcastReceiver.class;
@ -126,7 +127,7 @@ public class CalculatorOnscreenService extends Service implements OnscreenViewLi
@Override
public void onCreate() {
super.onCreate();
((CalculatorApplication) getApplication()).getComponent().inject(this);
cast(getApplication()).getComponent().inject(this);
}
@Override