Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration from MVP to MVVM with Android Architecture Components #1

Merged
merged 2 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply from: "$project.rootDir/tools/findbugs.gradle"
apply from: "$project.rootDir/tools/checkstyle.gradle"
apply from: "$project.rootDir/tools/pmd.gradle"
Expand Down Expand Up @@ -49,10 +46,6 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

packagingOptions {
exclude 'META-INF/ASL2.0'
exclude 'META-INF/DEPENDENCIES'
Expand Down
20 changes: 15 additions & 5 deletions app/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ dependencies {
testImplementation deps.junit
androidTestImplementation deps.findbugs_jsr

implementation deps.kotlin.stdlib

implementation deps.rxjava.android
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
Expand All @@ -24,7 +22,6 @@ dependencies {

// Glide
implementation deps.glide.runtime
kapt deps.glide.compiler
implementation deps.glide_okhttp_integration

// Retrofit
Expand All @@ -38,11 +35,24 @@ dependencies {

// Dagger2
implementation deps.dagger.main
kapt deps.dagger.compiler
annotationProcessor deps.dagger.compiler
implementation deps.dagger.android
kapt deps.dagger.android_processor
annotationProcessor deps.dagger.android_processor
implementation deps.dagger.android_support

// Viewmodel
implementation deps.lifecycle.extensions
annotationProcessor deps.lifecycle.compiler

//Auto value
implementation deps.auto_value.annotations
annotationProcessor deps.auto_value.compiler
implementation deps.auto_value_gson.annotations
// included again as annotation processor because of
// an issue https://github.com/rharter/auto-value-gson/issues/171
annotationProcessor deps.auto_value.annotations
annotationProcessor deps.auto_value_gson.compiler
annotationProcessor deps.auto_value_parcel
// Espresso
androidTestImplementation(deps.espresso, {
exclude group: 'com.android.support', module: 'support-annotations'
Expand Down
19 changes: 0 additions & 19 deletions app/src/main/java/com/android/ashwiask/tvmaze/AppComponent.java

This file was deleted.

36 changes: 0 additions & 36 deletions app/src/main/java/com/android/ashwiask/tvmaze/AppModule.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
package com.android.ashwiask.tvmaze;

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

import com.android.ashwiask.tvmaze.network.NetworkModule;
import com.android.ashwiask.tvmaze.di.AppComponent;
import com.android.ashwiask.tvmaze.di.AppInjector;
import com.android.ashwiask.tvmaze.di.DaggerAppComponent;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;

/**
* @author Ashwini Kumar.
*/
public class TvMazeApplication extends Application
{
private AppComponent appComponent;
implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

public static TvMazeApplication get(Context context)
{
return (TvMazeApplication) context.getApplicationContext();
}
private AppComponent appComponent;

@Override
public void onCreate()
{
public void onCreate() {
super.onCreate();
initAppComponent();
}

private void initAppComponent()
{
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.networkModule(new NetworkModule())
.application(this)
.build();
appComponent.inject(this);
AppInjector.init(this);
}

public AppComponent getAppComponent()
{
public AppComponent getAppComponent() {
return appComponent;
}

@Override
public AndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.android.ashwiask.tvmaze.base;

import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;

import javax.inject.Inject;

import dagger.android.AndroidInjector;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.support.HasSupportFragmentInjector;

public class TvMazeBaseActivity extends AppCompatActivity
implements HasSupportFragmentInjector {
@Inject
DispatchingAndroidInjector<Fragment> fragmentDispatchingAndroidInjector;

@Override
public AndroidInjector<Fragment> supportFragmentInjector() {
return fragmentDispatchingAndroidInjector;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.android.ashwiask.tvmaze.di;

import com.android.ashwiask.tvmaze.home.HomeActivity;

import dagger.Module;
import dagger.android.ContributesAndroidInjector;

@Module
abstract class ActivityBuildersModule {
@ContributesAndroidInjector
abstract HomeActivity bindHomeActivity();
}
33 changes: 33 additions & 0 deletions app/src/main/java/com/android/ashwiask/tvmaze/di/AppComponent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.android.ashwiask.tvmaze.di;

import android.app.Application;

import com.android.ashwiask.tvmaze.TvMazeApplication;
import com.android.ashwiask.tvmaze.network.NetworkModule;

import javax.inject.Singleton;

import dagger.BindsInstance;
import dagger.Component;
import dagger.android.support.AndroidSupportInjectionModule;

/**
* @author Ashwini Kumar.
*/
@Singleton
@Component(modules = {
AndroidSupportInjectionModule.class,
AppModule.class,
ActivityBuildersModule.class,
NetworkModule.class})
public interface AppComponent {
void inject(TvMazeApplication tvMazeApplication);

@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);

AppComponent build();
}
}
80 changes: 80 additions & 0 deletions app/src/main/java/com/android/ashwiask/tvmaze/di/AppInjector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.android.ashwiask.tvmaze.di;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.android.ashwiask.tvmaze.TvMazeApplication;

import dagger.android.AndroidInjection;
import dagger.android.support.AndroidSupportInjection;
import dagger.android.support.HasSupportFragmentInjector;

/**
* Helper class to automatically inject fragments if they implement {@link Injectable}.
*/
public class AppInjector {
private AppInjector() {
}

public static void init(TvMazeApplication tvMazeApplication) {
tvMazeApplication.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
handleActivity(activity);
}

@Override
public void onActivityStarted(Activity activity) {
//do nothing
}

@Override
public void onActivityResumed(Activity activity) {
//do nothing
}

@Override
public void onActivityPaused(Activity activity) {
//do nothing
}

@Override
public void onActivityStopped(Activity activity) {
//do nothing
}

@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
//do nothing
}

@Override
public void onActivityDestroyed(Activity activity) {
//do nothing
}
});
}

private static void handleActivity(Activity activity) {
if (activity instanceof HasSupportFragmentInjector) {
AndroidInjection.inject(activity);
}
if (activity instanceof FragmentActivity) {
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentCreated(FragmentManager fm, Fragment f,
Bundle savedInstanceState) {
if (f instanceof Injectable) {
AndroidSupportInjection.inject(f);
}
}
}, true);
}
}
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/android/ashwiask/tvmaze/di/AppModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.android.ashwiask.tvmaze.di;

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

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

/**
* @author Ashwini Kumar.
*/
@Module(includes = ViewModelModule.class)
class AppModule {
@Provides
@Singleton
Context provideContext(Application application) {
return application;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.android.ashwiask.tvmaze.di;

/**
* Marks an activity / fragment injectable.
*/
public interface Injectable {
}
Loading