Skip to content

Commit

Permalink
Merge branch 'fix_scope'
Browse files Browse the repository at this point in the history
* fix_scope:
  Fix Console module
  disabled robolectric Snackbar tests
  Add retry and StateManager
  Fix Scope issue
  Fix scope issue

# Conflicts:
#	app/src/test/java/com/mirhoseini/marvel/activity/MainActivityRobolectricTest.java
  • Loading branch information
mohsenoid committed Jan 21, 2017
2 parents bdd6cbb + e3403cd commit 9d98ed1
Show file tree
Hide file tree
Showing 37 changed files with 497 additions and 380 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/com/mirhoseini/marvel/AndroidModule.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.mirhoseini.marvel;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.preference.PreferenceManager;

import javax.inject.Singleton;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.mirhoseini.marvel.activity.CharacterActivity;
import com.mirhoseini.marvel.activity.MainActivity;
import com.mirhoseini.marvel.activity.SplashActivity;
import com.mirhoseini.marvel.character.cache.AppCacheModule;
import com.mirhoseini.marvel.character.cache.CacheModule;
import com.mirhoseini.marvel.character.cache.CacheSubComponent;
import com.mirhoseini.marvel.character.search.AppSearchModule;
import com.mirhoseini.marvel.character.search.SearchModule;
import com.mirhoseini.marvel.character.search.SearchSubComponent;
import com.mirhoseini.marvel.database.DatabaseModule;
import com.mirhoseini.marvel.domain.ApiModule;
Expand Down Expand Up @@ -35,8 +35,8 @@ public interface ApplicationComponent {

void inject(CharacterActivity characterActivity);

SearchSubComponent plus(AppSearchModule module);
SearchSubComponent plus(SearchModule module);

CacheSubComponent plus(AppCacheModule module);
CacheSubComponent plus(CacheModule module);

}
29 changes: 25 additions & 4 deletions app/src/main/java/com/mirhoseini/marvel/ApplicationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import android.content.Context;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.mirhoseini.marvel.util.AppConstants;
import com.mirhoseini.marvel.util.AppSchedulerProvider;
import com.mirhoseini.marvel.util.Constants;
import com.mirhoseini.marvel.util.SchedulerProvider;
import com.mirhoseini.marvel.util.StateManager;
import com.mirhoseini.marvel.util.StateManagerImpl;
import com.mirhoseini.utils.Utils;

import java.io.File;
Expand Down Expand Up @@ -33,7 +37,7 @@ boolean provideIsDebug() {
@Singleton
@Named("networkTimeoutInSeconds")
int provideNetworkTimeoutInSeconds() {
return Constants.NETWORK_CONNECTION_TIMEOUT;
return AppConstants.NETWORK_CONNECTION_TIMEOUT;
}

@Provides
Expand All @@ -52,21 +56,28 @@ SchedulerProvider provideAppScheduler() {
@Singleton
@Named("cacheSize")
long provideCacheSize() {
return Constants.CACHE_SIZE;
return AppConstants.CACHE_SIZE;
}

@Provides
@Singleton
@Named("cacheMaxAge")
int provideCacheMaxAgeMinutes() {
return Constants.CACHE_MAX_AGE;
return AppConstants.CACHE_MAX_AGE;
}

@Provides
@Singleton
@Named("cacheMaxStale")
int provideCacheMaxStaleDays() {
return Constants.CACHE_MAX_STALE;
return AppConstants.CACHE_MAX_STALE;
}

@Provides
@Singleton
@Named("retryCount")
public int provideApiRetryCount() {
return AppConstants.API_RETRY_COUNT;
}

@Provides
Expand All @@ -82,4 +93,14 @@ boolean provideIsConnect(Context context) {
return Utils.isConnected(context);
}

@Provides
FirebaseAnalytics provideFirebaseAnalytics(Context context) {
return FirebaseAnalytics.getInstance(context);
}

@Provides
@Singleton
public StateManager provideStateManager(StateManagerImpl stateManager) {
return stateManager;
}
}
44 changes: 44 additions & 0 deletions app/src/main/java/com/mirhoseini/marvel/MarvelApplication.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.mirhoseini.marvel;

import android.app.Application;
import android.content.Context;

import com.mirhoseini.marvel.character.cache.CacheModule;
import com.mirhoseini.marvel.character.cache.CacheSubComponent;
import com.mirhoseini.marvel.character.search.SearchModule;
import com.mirhoseini.marvel.character.search.SearchSubComponent;

/**
* Created by Mohsen on 20/10/2016.
Expand All @@ -9,11 +15,49 @@
public abstract class MarvelApplication extends Application {

private static ApplicationComponent component;
private CacheSubComponent cacheSubComponent;
private SearchSubComponent searchSubComponent;

public static ApplicationComponent getComponent() {
return component;
}

public static MarvelApplication get(Context context) {
return (MarvelApplication) context.getApplicationContext();
}

public CacheSubComponent getCacheSubComponent() {
if (null == cacheSubComponent)
createCacheSubComponent();

return cacheSubComponent;
}

public CacheSubComponent createCacheSubComponent() {
cacheSubComponent = component.plus(new CacheModule());
return cacheSubComponent;
}

public void releaseCacheSubComponent() {
cacheSubComponent = null;
}

public SearchSubComponent getSearchSubComponent() {
if (null == searchSubComponent)
createSearchSubComponent();

return searchSubComponent;
}

public SearchSubComponent createSearchSubComponent() {
searchSubComponent = component.plus(new SearchModule());
return searchSubComponent;
}

public void releaseSearchSubComponent() {
searchSubComponent = null;
}

@Override
public void onCreate() {
super.onCreate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.mirhoseini.marvel.ApplicationComponent;
import com.mirhoseini.marvel.BR;
import com.mirhoseini.marvel.MarvelApplication;
import com.mirhoseini.marvel.R;
import com.mirhoseini.marvel.base.BaseActivity;
import com.mirhoseini.marvel.database.model.CharacterModel;
Expand Down Expand Up @@ -55,6 +56,11 @@ protected void onCreate(Bundle savedInstanceState) {
Timber.d("Character Activity Created");
}

@Override
protected void injectDependencies(MarvelApplication application, ApplicationComponent component) {
component.inject(this);
}

private void setupToolbar(String characterName) {
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.logo);
Expand All @@ -63,8 +69,8 @@ private void setupToolbar(String characterName) {
}

@Override
protected void injectDependencies(ApplicationComponent component) {
component.inject(this);
protected void releaseSubComponents(MarvelApplication application) {

}

}
73 changes: 53 additions & 20 deletions app/src/main/java/com/mirhoseini/marvel/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
Expand All @@ -12,43 +11,45 @@
import android.widget.Toast;

import com.mirhoseini.marvel.ApplicationComponent;
import com.mirhoseini.marvel.MarvelApplication;
import com.mirhoseini.marvel.R;
import com.mirhoseini.marvel.base.BaseActivity;
import com.mirhoseini.marvel.character.cache.CharacterCacheFragment;
import com.mirhoseini.marvel.character.search.CharacterSearchFragment;
import com.mirhoseini.marvel.character.cache.CacheFragment;
import com.mirhoseini.marvel.character.search.SearchFragment;
import com.mirhoseini.marvel.database.model.CharacterModel;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.subscriptions.CompositeSubscription;
import timber.log.Timber;

/**
* Created by Mohsen on 20/10/2016.
*/

public class MainActivity extends BaseActivity implements CharacterSearchFragment.OnListFragmentInteractionListener, CharacterCacheFragment.OnListFragmentInteractionListener {
public class MainActivity extends BaseActivity {

public static final String TAG_SEARCH_FRAGMENT = "search_fragment";
public static final String TAG_CACHE_FRAGMENT = "cache_fragment";

// injecting dependencies via Dagger
@Inject
Context context;
@Inject
Resources resources;

// injecting views via ButterKnife
@BindView(R.id.toolbar)
Toolbar toolbar;

private CharacterSearchFragment searchFragment;
private CharacterCacheFragment cacheFragment;
CompositeSubscription subscriptions;
private SearchFragment searchFragment;
private CacheFragment cacheFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// inject views using ButterKnife
Expand All @@ -57,19 +58,19 @@ protected void onCreate(Bundle savedInstanceState) {
setupToolbar();

if (null == savedInstanceState) {
searchFragment = CharacterSearchFragment.newInstance();
cacheFragment = CharacterCacheFragment.newInstance();
searchFragment = SearchFragment.newInstance();
cacheFragment = CacheFragment.newInstance();
attachFragments();
} else {
searchFragment = (CharacterSearchFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT);
cacheFragment = (CharacterCacheFragment) getSupportFragmentManager().findFragmentByTag(TAG_CACHE_FRAGMENT);
searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag(TAG_SEARCH_FRAGMENT);
cacheFragment = (CacheFragment) getSupportFragmentManager().findFragmentByTag(TAG_CACHE_FRAGMENT);
}

Timber.d("Main Activity Created");
}

@Override
protected void injectDependencies(ApplicationComponent component) {
protected void injectDependencies(MarvelApplication application, ApplicationComponent component) {
component.inject(this);
}

Expand All @@ -87,27 +88,59 @@ private void attachFragments() {
}

@Override
protected void onResume() {
super.onResume();

if (null == subscriptions || subscriptions.isUnsubscribed())
subscriptions = new CompositeSubscription();

subscriptions.addAll(
searchFragment.characterObservable()
.subscribe(this::showCharacter),
searchFragment.messageObservable()
.subscribe(this::showMessage),
searchFragment.offlineObservable()
.subscribe(this::showOfflineMessage),
cacheFragment.characterObservable()
.subscribe(this::showCharacter),
cacheFragment.messageObservable()
.subscribe(this::showMessage),
cacheFragment.offlineObservable()
.subscribe(this::showOfflineMessage)
);
}

public void showMessage(String message) {
Timber.d("Showing Message: %s", message);

Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}

@Override
public void showOfflineMessage() {
public void showOfflineMessage(boolean isCritical) {
Timber.d("Showing Offline Message");

Snackbar.make(toolbar, R.string.offline_message, Snackbar.LENGTH_LONG)
.setAction(R.string.go_online, v -> {
startActivity(new Intent(
Settings.ACTION_WIFI_SETTINGS));
})
.setAction(R.string.go_online, v -> startActivity(new Intent(
Settings.ACTION_WIFI_SETTINGS)))
.setActionTextColor(Color.GREEN)
.show();
}

@Override
public void showCharacter(CharacterModel character) {
startActivity(CharacterActivity.newIntent(this, character));
}

@Override
protected void onPause() {
super.onPause();

subscriptions.unsubscribe();
}

@Override
protected void releaseSubComponents(MarvelApplication application) {
application.releaseCacheSubComponent();
application.releaseSearchSubComponent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.MotionEvent;

import com.mirhoseini.marvel.ApplicationComponent;
import com.mirhoseini.marvel.MarvelApplication;
import com.mirhoseini.marvel.R;
import com.mirhoseini.marvel.base.BaseActivity;
import com.mirhoseini.marvel.util.AppConstants;
Expand Down Expand Up @@ -61,6 +62,11 @@ public void run() {
splashThread.start();
}

@Override
protected void injectDependencies(MarvelApplication application, ApplicationComponent component) {
component.inject(this);
}

// Listening to whole activity touch events
@Override
public boolean onTouchEvent(MotionEvent evt) {
Expand All @@ -74,7 +80,8 @@ public boolean onTouchEvent(MotionEvent evt) {
}

@Override
protected void injectDependencies(ApplicationComponent component) {
component.inject(this);
protected void releaseSubComponents(MarvelApplication application) {

}

}
Loading

0 comments on commit 9d98ed1

Please sign in to comment.