-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`
- Loading branch information
Showing
56 changed files
with
914 additions
and
0 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
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 @@ | ||
/build |
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,57 @@ | ||
apply plugin: 'com.android.application' | ||
apply plugin: 'kotlin-android' | ||
apply plugin: 'kotlin-android-extensions' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
buildToolsVersion "28.0.2" | ||
|
||
defaultConfig { | ||
applicationId "com.sddamico.rxbroadcastreciever" | ||
minSdkVersion 14 | ||
targetSdkVersion 28 | ||
versionCode 1 | ||
versionName "1.0" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
testImplementation 'junit:junit:4.12' | ||
|
||
implementation 'com.google.android.material:material:1.0.0-rc01' | ||
|
||
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.2.61' | ||
|
||
implementation 'io.reactivex.rxjava2:rxjava:2.2.1' | ||
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' | ||
implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0' | ||
|
||
implementation 'com.uber.autodispose:autodispose-ktx:1.0.0-RC2' | ||
implementation 'com.uber.autodispose:autodispose-lifecycle-ktx:1.0.0-RC2' | ||
implementation 'com.uber.autodispose:autodispose-android-ktx:1.0.0-RC2' | ||
implementation 'com.uber.autodispose:autodispose-rxlifecycle:1.0.0-RC2' | ||
|
||
implementation 'com.jakewharton.rxbinding2:rxbinding-kotlin:2.1.1' | ||
implementation 'com.jakewharton.rxbinding2:rxbinding-support-v4-kotlin:2.1.1' | ||
implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7-kotlin:2.1.1' | ||
implementation 'com.jakewharton.rxbinding2:rxbinding-design-kotlin:2.1.1' | ||
implementation 'com.jakewharton.rxbinding2:rxbinding-recyclerview-v7-kotlin:2.1.1' | ||
|
||
implementation 'com.trello.rxlifecycle2:rxlifecycle-android:2.2.2' | ||
implementation 'com.trello.rxlifecycle2:rxlifecycle-components:2.2.2' | ||
implementation 'com.trello.rxlifecycle2:rxlifecycle-kotlin:2.2.2' | ||
implementation 'com.trello.rxlifecycle2:rxlifecycle-android-lifecycle-kotlin:2.2.2' | ||
|
||
implementation 'com.jakewharton.rxrelay2:rxrelay:2.0.0' | ||
|
||
implementation 'androidx.core:core-ktx:1.0.0-rc01' | ||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0-rc01' | ||
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01' | ||
|
||
} |
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 @@ | ||
# Add project specific ProGuard rules here. | ||
# By default, the flags in this file are appended to flags specified | ||
# in /usr/local/opt/android-sdk/tools/proguard/proguard-android.txt | ||
# You can edit the include path and order by changing the proguardFiles | ||
# directive in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# Add any project specific keep options here: | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} |
13 changes: 13 additions & 0 deletions
13
app/src/androidTest/java/com/sddamico/mvp/ApplicationTest.java
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,13 @@ | ||
package com.sddamico.mvp; | ||
|
||
import android.app.Application; | ||
import android.test.ApplicationTestCase; | ||
|
||
/** | ||
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a> | ||
*/ | ||
public class ApplicationTest extends ApplicationTestCase<Application> { | ||
public ApplicationTest() { | ||
super(Application.class); | ||
} | ||
} |
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,49 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.sddamico.mvp" > | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
tools:ignore="GoogleAppIndexingWarning" | ||
> | ||
|
||
<activity android:name=".IncrementActivityMvp" | ||
android:configChanges="orientation|screenSize|keyboard|keyboardHidden" | ||
android:rotationAnimation="seamless" | ||
android:windowSoftInputMode="adjustPan|stateHidden" | ||
android:exported="true" | ||
android:label="MVP" | ||
> | ||
|
||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
|
||
</activity> | ||
|
||
<activity android:name=".IncrementActivityRxMvp" | ||
android:configChanges="orientation|screenSize|keyboard|keyboardHidden" | ||
android:rotationAnimation="seamless" | ||
android:windowSoftInputMode="adjustPan|stateHidden" | ||
android:exported="true" | ||
android:label="MVP + RX" | ||
/> | ||
|
||
<activity android:name=".IncrementActivityRxMvmvp" | ||
android:configChanges="orientation|screenSize|keyboard|keyboardHidden" | ||
android:rotationAnimation="seamless" | ||
android:windowSoftInputMode="adjustPan|stateHidden" | ||
android:exported="true" | ||
android:label="MVMVP" | ||
/> | ||
|
||
</application> | ||
|
||
</manifest> |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/com/sddamico/mvp/IncrementActivityMvp.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,50 @@ | ||
package com.sddamico.mvp | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
import kotlinx.android.synthetic.main.activity_increment.* | ||
|
||
typealias View = IncrementActivityContract.IncrementActivityView | ||
typealias Presenter = IncrementActivityContract.IncrementActivityPresenter | ||
|
||
class IncrementActivityMvp : AppCompatActivity(), View { | ||
|
||
private lateinit var presenter: Presenter | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val maybePresenter = lastCustomNonConfigurationInstance as Presenter? | ||
|
||
if (maybePresenter != null) { | ||
presenter = maybePresenter | ||
} | ||
|
||
if (!::presenter.isInitialized) { | ||
presenter = IncrementActivityPresenterImpl() | ||
} | ||
|
||
setContentView(R.layout.activity_increment) | ||
|
||
increment.setOnClickListener { presenter.onIncrementClicked() } | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
|
||
presenter.attach(this) | ||
} | ||
|
||
override fun onStop() { | ||
presenter.detach() | ||
|
||
super.onStop() | ||
} | ||
|
||
override fun setCountView(countString: String) { | ||
counter.text = countString | ||
} | ||
|
||
override fun onRetainCustomNonConfigurationInstance(): Any = presenter | ||
} |
62 changes: 62 additions & 0 deletions
62
app/src/main/java/com/sddamico/mvp/IncrementActivityRxMvmvp.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,62 @@ | ||
package com.sddamico.mvp | ||
|
||
import android.os.Bundle | ||
import androidx.lifecycle.ViewModelProviders | ||
import com.jakewharton.rxbinding2.view.clicks | ||
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity | ||
import com.uber.autodispose.AutoDispose.autoDisposable | ||
import com.uber.autodispose.ScopeProvider | ||
import com.ubercab.autodispose.rxlifecycle.RxLifecycleInterop | ||
import io.reactivex.CompletableSource | ||
|
||
import kotlinx.android.synthetic.main.activity_increment.* | ||
|
||
typealias PresenterMvmvp = IncrementActivityMvmvpContract.PresenterMvmvp | ||
typealias ViewModelMvmvp = IncrementActivityMvmvpContract.IncrementActivityStateViewModel | ||
typealias StateMvmvp = IncrementActivityMvmvpContract.IncrementActivityMvmvpState | ||
|
||
class IncrementActivityRxMvmvp : RxAppCompatActivity(), ScopeProvider { | ||
|
||
override fun requestScope(): CompletableSource = RxLifecycleInterop.from(this).requestScope() | ||
|
||
private val viewModel by lazy { ViewModelProviders.of(this).get(ViewModelMvmvp::class.java) } | ||
|
||
private lateinit var presenter: IncrementActivityMvmvpContract.PresenterMvmvp | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val maybePresenter = lastCustomNonConfigurationInstance as PresenterMvmvp? | ||
|
||
if (maybePresenter != null) { | ||
presenter = maybePresenter | ||
} | ||
|
||
if (!::presenter.isInitialized) { | ||
presenter = PresenterMvmvpImpl(viewModel) | ||
} | ||
|
||
setContentView(R.layout.activity_increment) | ||
|
||
observeActions() | ||
observeState() | ||
} | ||
|
||
private fun observeState() { | ||
viewModel.state | ||
.`as`(autoDisposable(this)) | ||
.subscribe { render(it) } | ||
} | ||
|
||
private fun render(state: StateMvmvp) { | ||
counter.text = state.count | ||
} | ||
|
||
private fun observeActions() { | ||
increment.clicks() | ||
.`as`(autoDisposable(this)) | ||
.subscribe { presenter.onIncrementClicked() } | ||
} | ||
|
||
override fun onRetainCustomNonConfigurationInstance(): Any = presenter | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/sddamico/mvp/IncrementActivityRxMvp.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,52 @@ | ||
package com.sddamico.mvp | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.jakewharton.rxbinding2.view.clicks | ||
|
||
import kotlinx.android.synthetic.main.activity_increment.* | ||
|
||
typealias ViewRx = IncrementActivityRxContract.IncrementActivityRxView | ||
typealias PresenterRx = IncrementActivityRxContract.IncrementActivityRxPresenter | ||
|
||
class IncrementActivityRxMvp : AppCompatActivity(), ViewRx { | ||
|
||
private lateinit var presenter: PresenterRx | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val maybePresenter = lastCustomNonConfigurationInstance as PresenterRx? | ||
|
||
if (maybePresenter != null) { | ||
presenter = maybePresenter | ||
} | ||
|
||
if (!::presenter.isInitialized) { | ||
presenter = IncrementActivityRxPresenterImpl() | ||
} | ||
|
||
setContentView(R.layout.activity_increment) | ||
|
||
increment.clicks() | ||
.subscribe { presenter.onIncrementClicked() } | ||
} | ||
|
||
override fun onStart() { | ||
super.onStart() | ||
|
||
presenter.attach(this) | ||
} | ||
|
||
override fun onStop() { | ||
presenter.detach() | ||
|
||
super.onStop() | ||
} | ||
|
||
override fun setCountView(countString: String) { | ||
counter.text = countString | ||
} | ||
|
||
override fun onRetainCustomNonConfigurationInstance(): Any = presenter | ||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/com/sddamico/mvp/mvmvp-IncrementActivityMvmvp.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,23 @@ | ||
package com.sddamico.mvp | ||
|
||
interface IncrementActivityMvmvpContract { | ||
interface PresenterMvmvp : MvmvpPresenter<IncrementActivityMvmvpState, IncrementActivityStateViewModel> { | ||
fun onIncrementClicked() | ||
} | ||
|
||
data class IncrementActivityMvmvpState( | ||
var count: String = 0.toString() | ||
) | ||
|
||
class IncrementActivityStateViewModel : MvmvpViewModel<IncrementActivityMvmvpState>(IncrementActivityMvmvpState()) | ||
} | ||
|
||
class PresenterMvmvpImpl(override val viewModel: ViewModelMvmvp) | ||
: MvmvpPresenterBase<StateMvmvp, ViewModelMvmvp>(viewModel), PresenterMvmvp { | ||
|
||
override fun onIncrementClicked() { | ||
sendToViewModel { it.copy( | ||
count = it.count.toInt().plus(1).toString() | ||
)} | ||
} | ||
} |
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,27 @@ | ||
package com.sddamico.mvp | ||
|
||
import android.annotation.SuppressLint | ||
import androidx.lifecycle.ViewModel | ||
import com.jakewharton.rxrelay2.BehaviorRelay | ||
import io.reactivex.Observable | ||
import io.reactivex.android.schedulers.AndroidSchedulers | ||
import io.reactivex.rxkotlin.Observables | ||
|
||
interface MvmvpPresenter<State, out VM: MvmvpViewModel<State>> { | ||
val viewModel: VM | ||
} | ||
|
||
abstract class MvmvpViewModel<State>(initialState: State) : ViewModel() { | ||
val state = BehaviorRelay.createDefault(initialState)!! | ||
} | ||
|
||
abstract class MvmvpPresenterBase<State, out VM: MvmvpViewModel<State>>(override val viewModel: VM) : MvmvpPresenter<State, VM> { | ||
|
||
@SuppressLint("CheckResult") | ||
fun sendToViewModel(reducer: (State) -> State) { | ||
Observables.zip(Observable.just(reducer), viewModel.state) | ||
.observeOn(AndroidSchedulers.mainThread()) // ensures mutations happen serially on main thread | ||
.map { (reducer, state) -> reducer.invoke(state) } | ||
.subscribe(viewModel.state) | ||
} | ||
} |
Oops, something went wrong.