Skip to content

Commit

Permalink
Merge pull request #25 from nimbl3/chore/add-base-classes
Browse files Browse the repository at this point in the history
[#18] Adding base components: BaseActivity, BaseFragment, BaseViewModel…
  • Loading branch information
sleepylee authored Jun 13, 2018
2 parents 7896146 + 77d46ff commit 8b80dd4
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 4 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/com/nimbl3/di/ViewModelKey.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nimbl3.di

import android.arch.lifecycle.ViewModel
import dagger.MapKey
import kotlin.reflect.KClass

@MustBeDocumented
@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package com.nimbl3.di.modules
import dagger.Module
import dagger.android.ContributesAndroidInjector
import com.nimbl3.MainActivity
import com.nimbl3.ui.main.di.MainActivityModule

@Module
abstract class ActivityBuilderModule {

@ContributesAndroidInjector
@ContributesAndroidInjector(modules = [MainActivityModule::class])
abstract fun bindMainActivity(): MainActivity
}
32 changes: 32 additions & 0 deletions app/src/main/java/com/nimbl3/lib/viewmodel/ViewModelFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nimbl3.lib.viewmodel

import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import javax.inject.Inject
import javax.inject.Provider

@Suppress("NestedBlockDepth")
class ViewModelFactory @Inject constructor(
private val creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<ViewModel>>)
: ViewModelProvider.Factory {

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
var creator: Provider<out ViewModel>? = creators[modelClass]
if (creator == null) {
for ((key, value) in creators) {
if (modelClass.isAssignableFrom(key)) {
creator = value
break
}
}
}
if (creator == null) throw IllegalArgumentException("Unknown ViewModel class type" + modelClass)

try {
return creator.get() as T
} catch (e: ClassCastException) {
throw e
}
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/com/nimbl3/ui/base/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.nimbl3.ui.base

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.app.AppCompatActivity
import dagger.android.AndroidInjection
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import javax.inject.Inject

abstract class BaseActivity: AppCompatActivity(), HasSupportFragmentInjector {
@Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Fragment>

private var disposables = CompositeDisposable()


override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
}

override fun supportFragmentInjector(): AndroidInjector<Fragment> {
return this.dispatchingAndroidInjector
}

protected fun Disposable.bindForDisposable() {
disposables.add(this)
}

override fun onDestroy() {
super.onDestroy()
disposables.clear()
}
}
36 changes: 36 additions & 0 deletions app/src/main/java/com/nimbl3/ui/base/BaseFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.nimbl3.ui.base

import android.os.Bundle
import android.support.v4.app.Fragment
import dagger.android.support.AndroidSupportInjection
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable


abstract class BaseFragment: Fragment() {
private var disposables = CompositeDisposable()

override fun onCreate(savedInstanceState: Bundle?) {
AndroidSupportInjection.inject(this)
super.onCreate(savedInstanceState)
}

override fun onDetach() {
super.onDetach()
disposables.dispose()
}

override fun onDestroy() {
super.onDestroy()
disposables.dispose()
}

override fun onDestroyView() {
super.onDestroyView()
disposables.clear()
}

protected fun Disposable.bindForDisposable() {
disposables.add(this)
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/nimbl3/ui/base/BaseViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.nimbl3.ui.base

import android.arch.lifecycle.ViewModel
import android.content.Intent
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.subjects.PublishSubject

abstract class BaseViewModel : ViewModel() {

private val disposables: CompositeDisposable = CompositeDisposable()
private val intent = PublishSubject.create<Intent>()

override fun onCleared() {
super.onCleared()
disposables.clear()
}

protected fun Disposable.bindForDisposable() {
disposables.add(this)
}

/**
* The intent with its bundle taken over from the Activity
*/
protected fun intent(): Observable<Intent> = this.intent

/**
* Emitting the Intent data to the corresponding ViewModel of a View for processing
*/
fun intent(intent: Intent) {
this.intent.onNext(intent)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.nimbl3

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
Expand All @@ -10,14 +9,14 @@ import io.reactivex.android.schedulers.AndroidSchedulers
import com.nimbl3.extension.setImageUrl
import com.nimbl3.data.service.ApiRepository
import com.nimbl3.data.service.response.ExampleResponse
import com.nimbl3.ui.base.BaseActivity
import javax.inject.Inject

class MainActivity : AppCompatActivity() {
class MainActivity : BaseActivity() {

@Inject lateinit var appRepository: ApiRepository

override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this) // If we have baseActivity. we can bring it into the base class.
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/nimbl3/ui/main/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.nimbl3.ui.main

import com.nimbl3.ui.base.BaseViewModel

// TODO: remove this rule later!
@Suppress("EmptyClassBlock")
class MainViewModel : BaseViewModel() {

}
17 changes: 17 additions & 0 deletions app/src/main/java/com/nimbl3/ui/main/di/MainActivityModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.nimbl3.ui.main.di

import android.arch.lifecycle.ViewModel
import com.nimbl3.di.ViewModelKey
import com.nimbl3.ui.main.MainViewModel
import dagger.Binds
import dagger.Module
import dagger.multibindings.IntoMap

@Module
internal abstract class MainActivityModule {

@Binds
@IntoMap
@ViewModelKey(MainViewModel::class)
abstract fun bindMainViewModel(viewModel: MainViewModel): ViewModel
}

0 comments on commit 8b80dd4

Please sign in to comment.