-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from nimbl3/chore/add-base-classes
[#18] Adding base components: BaseActivity, BaseFragment, BaseViewModel…
- Loading branch information
Showing
9 changed files
with
182 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/nimbl3/lib/viewmodel/ViewModelFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
app/src/main/java/com/nimbl3/ui/main/di/MainActivityModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |