├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── adammcneilly
│ │ └── mviexample
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── adammcneilly
│ │ │ └── mviexample
│ │ │ ├── LoggingMiddleware.kt
│ │ │ ├── LoginNetworkingMiddleware.kt
│ │ │ ├── LoginRepository.kt
│ │ │ ├── MainActivity.kt
│ │ │ ├── ProdLoginService.kt
│ │ │ ├── redux
│ │ │ ├── Action.kt
│ │ │ ├── Middleware.kt
│ │ │ ├── Reducer.kt
│ │ │ ├── State.kt
│ │ │ └── Store.kt
│ │ │ └── ui
│ │ │ ├── login
│ │ │ ├── LoginAction.kt
│ │ │ ├── LoginFragment.kt
│ │ │ ├── LoginReducer.kt
│ │ │ ├── LoginViewModel.kt
│ │ │ └── LoginViewState.kt
│ │ │ └── profile
│ │ │ └── ProfileFragment.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── fragment_login.xml
│ │ ├── fragment_profile.xml
│ │ └── main_activity.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── values-night
│ │ └── themes.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ └── test
│ └── java
│ └── com
│ └── adammcneilly
│ └── mviexample
│ ├── ActionCaptureMiddleware.kt
│ ├── ExampleUnitTest.kt
│ ├── FakeLoginRepository.kt
│ ├── LoginNetworkingMiddlewareTest.kt
│ ├── redux
│ └── StoreTest.kt
│ └── ui
│ └── login
│ └── LoginReducerTest.kt
├── assets
└── MVIDiagram.png
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/
5 | /.idea/caches
6 | /.idea/libraries
7 | /.idea/modules.xml
8 | /.idea/workspace.xml
9 | /.idea/navEditor.xml
10 | /.idea/assetWizardSettings.xml
11 | .DS_Store
12 | /build
13 | /captures
14 | .externalNativeBuild
15 | .cxx
16 | local.properties
17 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MVI Example
2 |
3 | This application was streamed live on Twitch to demonstrate how to build an application using MVI.
4 |
5 | You can find the VOD here for now: https://www.twitch.tv/videos/1036306656
6 |
7 | And on YouTube: https://www.youtube.com/watch?v=wTJX_lWdh60
8 |
9 | Much of the codebase is documented, but you can expect a blog post coming soon as well.
10 |
11 | ## MVI Diagram
12 |
13 | During the stream we created a diagram to understand the flow of data in an MVI application, which you can find here. This may be helpful to review before looking into the codebase:
14 |
15 | 
16 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdk 30
8 | buildToolsVersion "30.0.3"
9 |
10 | defaultConfig {
11 | applicationId "com.adammcneilly.mviexample"
12 | minSdk 21
13 | targetSdk 30
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_1_8
28 | targetCompatibility JavaVersion.VERSION_1_8
29 | }
30 | kotlinOptions {
31 | jvmTarget = '1.8'
32 | }
33 |
34 | buildFeatures {
35 | viewBinding = true
36 | }
37 | }
38 |
39 | dependencies {
40 |
41 | implementation 'androidx.core:core-ktx:1.5.0'
42 | implementation 'androidx.appcompat:appcompat:1.3.0'
43 | implementation 'com.google.android.material:material:1.3.0'
44 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
45 | implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
46 | implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
47 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
48 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3'
49 | testImplementation 'junit:junit:4.13.2'
50 | testImplementation "com.google.truth:truth:1.1.3"
51 | androidTestImplementation 'androidx.test.ext:junit:1.1.2'
52 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
53 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/adammcneilly/mviexample/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.adammcneilly.mviexample", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/LoggingMiddleware.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import android.util.Log
4 | import com.adammcneilly.mviexample.redux.Action
5 | import com.adammcneilly.mviexample.redux.Middleware
6 | import com.adammcneilly.mviexample.redux.State
7 | import com.adammcneilly.mviexample.redux.Store
8 |
9 | /**
10 | * This [Middleware] is responsible for logging every [Action] that is processed to the Logcat, so
11 | * that we can use this for debugging.
12 | */
13 | class LoggingMiddleware : Middleware {
14 | override suspend fun process(action: A, currentState: S, store: Store) {
15 | Log.v(
16 | "LoggingMiddleware",
17 | "Processing action: $action; Current state: $currentState"
18 | )
19 | }
20 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/LoginNetworkingMiddleware.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import com.adammcneilly.mviexample.redux.Middleware
4 | import com.adammcneilly.mviexample.redux.Store
5 | import com.adammcneilly.mviexample.ui.login.LoginAction
6 | import com.adammcneilly.mviexample.ui.login.LoginViewState
7 |
8 | class LoginNetworkingMiddleware(
9 | private val loginRepository: LoginRepository,
10 | ) : Middleware {
11 |
12 | override suspend fun process(
13 | action: LoginAction,
14 | currentState: LoginViewState,
15 | store: Store,
16 | ) {
17 | when (action) {
18 | is LoginAction.SignInButtonClicked -> {
19 | if (currentState.email.isEmpty()) {
20 | store.dispatch(LoginAction.InvalidEmailSubmitted)
21 | return
22 | }
23 |
24 | loginUser(store, currentState)
25 | }
26 | }
27 | }
28 |
29 | private suspend fun loginUser(
30 | store: Store,
31 | currentState: LoginViewState
32 | ) {
33 | store.dispatch(LoginAction.LoginStarted)
34 |
35 | val isSuccessful = loginRepository.login(
36 | email = currentState.email,
37 | password = currentState.password,
38 | )
39 |
40 | if (isSuccessful) {
41 | store.dispatch(LoginAction.LoginCompleted)
42 | } else {
43 | store.dispatch(LoginAction.LoginFailed(null))
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/LoginRepository.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | interface LoginRepository {
4 | suspend fun login(email: String, password: String): Boolean
5 | }
6 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import com.adammcneilly.mviexample.ui.login.LoginFragment
6 | import com.adammcneilly.mviexample.ui.profile.ProfileFragment
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | setContentView(R.layout.main_activity)
13 | if (savedInstanceState == null) {
14 | supportFragmentManager.beginTransaction()
15 | .replace(R.id.container, LoginFragment())
16 | .commitNow()
17 | }
18 | }
19 |
20 | /**
21 | * To navigate to the profile screen, we'll run a Fragment transaction to replace the login
22 | * screen with the profile screen.
23 | */
24 | fun navigateToProfile() {
25 | supportFragmentManager.beginTransaction()
26 | .replace(R.id.container, ProfileFragment())
27 | .commitNow()
28 | }
29 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ProdLoginService.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import kotlinx.coroutines.delay
4 |
5 | class ProdLoginService : LoginRepository {
6 |
7 | override suspend fun login(email: String, password: String): Boolean {
8 | delay(2000)
9 |
10 | return true
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/redux/Action.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.redux
2 |
3 | /**
4 | * While this interface is empty, it is used to clearly define any Action or Intent that was taken
5 | * on a given screen.
6 | */
7 | interface Action
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/redux/Middleware.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.redux
2 |
3 | /**
4 | * A [Middleware] is any class that deals with side effects of actions. This can be logging,
5 | * triggering network calls, and other examples.
6 | */
7 | interface Middleware {
8 | /**
9 | * This will process the given [action] and [currentState] and determine if we need to
10 | * perform any side effects, or trigger a new action.
11 | *
12 | * @param[store] This is a reference to the [Store] that dispatched this action. We should only
13 | * call this with a _new_ action, and not trigger the same action again or risk ending up in a
14 | * loop.
15 | */
16 | suspend fun process(
17 | action: A,
18 | currentState: S,
19 | store: Store,
20 | )
21 | }
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/redux/Reducer.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.redux
2 |
3 | interface Reducer {
4 |
5 | /**
6 | * Given a [currentState] and some [action] that the user took, produce a new [State].
7 | *
8 | * This will give us clear and predictable state management, that ensures each state is associated
9 | * with some specific user intent or action.
10 | */
11 | fun reduce(currentState: S, action: A): S
12 | }
13 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/redux/State.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.redux
2 |
3 | /**
4 | * While this interface is empty, it is used to clearly define any class that is representative
5 | * of a View state for a given screen.
6 | */
7 | interface State
8 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/redux/Store.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.redux
2 |
3 | import kotlinx.coroutines.flow.MutableStateFlow
4 | import kotlinx.coroutines.flow.StateFlow
5 |
6 | /**
7 | * A [Store] is our state container for a given screen.
8 | *
9 | * @param[initialState] This is the initial state of the screen when it is first created.
10 | * @param[reducer] A system for taking in the current state, and a new action, and outputting the
11 | * updated state.
12 | * @param[middlewares] This is a list of [Middleware] entities for handling any side effects
13 | * for actions dispatched to this store.
14 | */
15 | class Store(
16 | initialState: S,
17 | private val reducer: Reducer,
18 | private val middlewares: List> = emptyList(),
19 | ) {
20 |
21 | private val _state = MutableStateFlow(initialState)
22 | val state: StateFlow = _state
23 |
24 | private val currentState: S
25 | get() = _state.value
26 |
27 | suspend fun dispatch(action: A) {
28 | middlewares.forEach { middleware ->
29 | middleware.process(action, currentState, this)
30 | }
31 |
32 | val newState = reducer.reduce(currentState, action)
33 | _state.value = newState
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ui/login/LoginAction.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.login
2 |
3 | import com.adammcneilly.mviexample.redux.Action
4 |
5 | /**
6 | * These are all of the possible actions that can be triggered from the login screen.
7 | */
8 | sealed class LoginAction : Action {
9 | data class EmailChanged(val newEmail: String) : LoginAction()
10 | data class PasswordChanged(val newPassword: String) : LoginAction()
11 | object SignInButtonClicked : LoginAction()
12 | object LoginStarted : LoginAction()
13 | object LoginCompleted : LoginAction()
14 | data class LoginFailed(val error: Throwable?) : LoginAction()
15 | object InvalidEmailSubmitted : LoginAction()
16 | }
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ui/login/LoginFragment.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.login
2 |
3 | import androidx.lifecycle.ViewModelProvider
4 | import android.os.Bundle
5 | import androidx.fragment.app.Fragment
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import android.widget.Button
10 | import androidx.core.widget.doOnTextChanged
11 | import androidx.lifecycle.lifecycleScope
12 | import com.adammcneilly.mviexample.MainActivity
13 | import com.adammcneilly.mviexample.R
14 | import com.adammcneilly.mviexample.databinding.FragmentLoginBinding
15 | import kotlinx.coroutines.flow.collect
16 |
17 | class LoginFragment : Fragment() {
18 | private lateinit var binding: FragmentLoginBinding
19 | private lateinit var viewModel: LoginViewModel
20 |
21 | override fun onCreate(savedInstanceState: Bundle?) {
22 | super.onCreate(savedInstanceState)
23 |
24 | viewModel = ViewModelProvider(this).get(LoginViewModel::class.java)
25 |
26 | // Whenever the view is resumed, subscribe to our viewmodel's view state StateFlow
27 | lifecycleScope.launchWhenResumed {
28 | viewModel.viewState.collect { viewState ->
29 | processViewState(viewState)
30 | }
31 | }
32 | }
33 |
34 | override fun onCreateView(
35 | inflater: LayoutInflater,
36 | container: ViewGroup?,
37 | savedInstanceState: Bundle?,
38 | ): View {
39 | binding = FragmentLoginBinding.inflate(inflater, container, false)
40 | return binding.root
41 | }
42 |
43 | /**
44 | * Whenever a relevant UI action occurs like a text change or a button click, proxy that
45 | * to the view model to handle.
46 | */
47 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
48 | super.onViewCreated(view, savedInstanceState)
49 |
50 | binding.emailInput.doOnTextChanged { text, _, _, _ ->
51 | viewModel.emailChanged(text?.toString().orEmpty())
52 | }
53 |
54 | binding.passwordInput.doOnTextChanged { text, _, _, _ ->
55 | viewModel.passwordChanged(text?.toString().orEmpty())
56 | }
57 |
58 | binding.signInButton.setOnClickListener {
59 | viewModel.signInButtonClicked()
60 | }
61 | }
62 |
63 | private fun processViewState(viewState: LoginViewState) {
64 | binding.progressBar.visibility = if (viewState.showProgressBar) {
65 | View.VISIBLE
66 | } else {
67 | View.GONE
68 | }
69 |
70 | binding.emailInputLayout.error = viewState.emailError
71 | }
72 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ui/login/LoginReducer.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.login
2 |
3 | import com.adammcneilly.mviexample.redux.Reducer
4 |
5 | /**
6 | * This reducer is responsible for handling any [LoginAction], and using that to create
7 | * a new [LoginViewState].
8 | */
9 | class LoginReducer : Reducer {
10 |
11 | /**
12 | * Side note: Notice that all of the functions are named in a way that they signify they're
13 | * returning a new state, and not just processing information. This helps keep your when statements
14 | * clear that they're returning stuff, so that context isn't lost.
15 | */
16 | override fun reduce(currentState: LoginViewState, action: LoginAction): LoginViewState {
17 | return when (action) {
18 | is LoginAction.EmailChanged -> {
19 | stateWithNewEmail(currentState, action)
20 | }
21 | is LoginAction.PasswordChanged -> {
22 | stateWithNewPassword(currentState, action)
23 | }
24 | LoginAction.LoginStarted -> {
25 | stateAfterLoginStarted(currentState)
26 | }
27 | LoginAction.LoginCompleted -> {
28 | stateAfterLoginCompleted(currentState)
29 | }
30 | is LoginAction.LoginFailed -> {
31 | stateAfterLoginFailed(currentState)
32 | }
33 | LoginAction.InvalidEmailSubmitted -> {
34 | stateWithInvalidEmailError(currentState)
35 | }
36 | else -> currentState
37 | }
38 | }
39 |
40 | private fun stateWithInvalidEmailError(currentState: LoginViewState) =
41 | currentState.copy(
42 | emailError = "Please enter an email address.",
43 | )
44 |
45 | private fun stateAfterLoginStarted(currentState: LoginViewState) =
46 | currentState.copy(
47 | showProgressBar = true,
48 | )
49 |
50 | private fun stateAfterLoginCompleted(currentState: LoginViewState) =
51 | currentState.copy(
52 | showProgressBar = false,
53 | )
54 |
55 | private fun stateAfterLoginFailed(currentState: LoginViewState) =
56 | currentState.copy(
57 | showProgressBar = false,
58 | )
59 |
60 | private fun stateWithNewPassword(
61 | currentState: LoginViewState,
62 | action: LoginAction.PasswordChanged
63 | ) = currentState.copy(
64 | password = action.newPassword,
65 | )
66 |
67 | private fun stateWithNewEmail(
68 | currentState: LoginViewState,
69 | action: LoginAction.EmailChanged
70 | ) = currentState.copy(
71 | email = action.newEmail,
72 | )
73 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ui/login/LoginViewModel.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.login
2 |
3 | import androidx.lifecycle.ViewModel
4 | import androidx.lifecycle.viewModelScope
5 | import com.adammcneilly.mviexample.LoggingMiddleware
6 | import com.adammcneilly.mviexample.LoginNetworkingMiddleware
7 | import com.adammcneilly.mviexample.ProdLoginService
8 | import com.adammcneilly.mviexample.redux.Store
9 | import kotlinx.coroutines.flow.StateFlow
10 | import kotlinx.coroutines.launch
11 |
12 | /**
13 | * The [LoginViewModel] is responsible for controlling the UI logic of the login screen. It will
14 | * listen for text changes and button clicks, and update the UI state accordingly and expose that so
15 | * the View can update.
16 | *
17 | * Whenever a view action occurs, such as [emailChanged] or [signInButtonClicked], proxy the
18 | * corresponding [LoginAction] to our [store].
19 | */
20 | class LoginViewModel : ViewModel() {
21 | private val store = Store(
22 | initialState = LoginViewState(),
23 | reducer = LoginReducer(),
24 | middlewares = listOf(
25 | LoggingMiddleware(),
26 | LoginNetworkingMiddleware(
27 | loginRepository = ProdLoginService(),
28 | ),
29 | )
30 | )
31 |
32 | val viewState: StateFlow = store.state
33 |
34 | fun emailChanged(newEmail: String) {
35 | val action = LoginAction.EmailChanged(newEmail)
36 |
37 | viewModelScope.launch {
38 | store.dispatch(action)
39 | }
40 | }
41 |
42 | fun passwordChanged(newPassword: String) {
43 | val action = LoginAction.PasswordChanged(newPassword)
44 |
45 | viewModelScope.launch {
46 | store.dispatch(action)
47 | }
48 | }
49 |
50 | fun signInButtonClicked() {
51 | val action = LoginAction.SignInButtonClicked
52 |
53 | viewModelScope.launch {
54 | store.dispatch(action)
55 | }
56 | }
57 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ui/login/LoginViewState.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.login
2 |
3 | import com.adammcneilly.mviexample.redux.State
4 |
5 | /**
6 | * An implementation of [State] that describes the configuration of the login screen at a given time.
7 | */
8 | data class LoginViewState(
9 | val email: String = "",
10 | val password: String = "",
11 | val showProgressBar: Boolean = false,
12 | val emailError: String? = null,
13 | val passwordError: String? = null,
14 | ) : State
--------------------------------------------------------------------------------
/app/src/main/java/com/adammcneilly/mviexample/ui/profile/ProfileFragment.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.profile
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import com.adammcneilly.mviexample.R
9 |
10 | /**
11 | * At the moment, this fragment is just a stub to show that we can test navigation, but doesn't
12 | * actually hold any logic for this sample application.
13 | */
14 | class ProfileFragment : Fragment() {
15 |
16 | override fun onCreateView(
17 | inflater: LayoutInflater, container: ViewGroup?,
18 | savedInstanceState: Bundle?
19 | ): View {
20 | return inflater.inflate(R.layout.fragment_profile, container, false)
21 | }
22 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_login.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
18 |
19 |
25 |
26 |
27 |
36 |
37 |
43 |
44 |
45 |
54 |
55 |
64 |
65 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/fragment_profile.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/main_activity.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | MVIExample
3 | Email
4 | Password
5 | Sign in
6 | This is the profile fragment.
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/test/java/com/adammcneilly/mviexample/ActionCaptureMiddleware.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import com.adammcneilly.mviexample.redux.Action
4 | import com.adammcneilly.mviexample.redux.Middleware
5 | import com.adammcneilly.mviexample.redux.State
6 | import com.adammcneilly.mviexample.redux.Store
7 | import com.google.common.truth.Truth.assertThat
8 |
9 | /**
10 | * This is a implementation of [Middleware] that will log every action sent to it.
11 | *
12 | * In a unit test, we can apply this middleware to a Store, and then use this middleware for asserting
13 | * that an action was sent to that store.
14 | */
15 | class ActionCaptureMiddleware : Middleware {
16 | private val actionHistory: MutableList = mutableListOf()
17 |
18 | override suspend fun process(action: A, currentState: S, store: Store) {
19 | actionHistory.add(action)
20 | }
21 |
22 | fun assertActionProcessed(expectedAction: Action) {
23 | assertThat(actionHistory).contains(expectedAction)
24 | }
25 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/adammcneilly/mviexample/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/adammcneilly/mviexample/FakeLoginRepository.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | /**
4 | * This is a custom implementation of [LoginRepository] that allows the developer in the unit tests
5 | * to determine if a login should behave as a success or failure scenario, by setting the [shouldMockSuccess]
6 | * boolean accordingly.
7 | */
8 | class FakeLoginRepository : LoginRepository {
9 | var shouldMockSuccess: Boolean = true
10 |
11 | override suspend fun login(email: String, password: String): Boolean {
12 | return shouldMockSuccess
13 | }
14 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/adammcneilly/mviexample/LoginNetworkingMiddlewareTest.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample
2 |
3 | import com.adammcneilly.mviexample.redux.Store
4 | import com.adammcneilly.mviexample.ui.login.LoginAction
5 | import com.adammcneilly.mviexample.ui.login.LoginReducer
6 | import com.adammcneilly.mviexample.ui.login.LoginViewState
7 | import kotlinx.coroutines.runBlocking
8 | import org.junit.Test
9 |
10 | class LoginNetworkingMiddlewareTest {
11 |
12 | /**
13 | * When running this test, we want to ensure that after having [LoginNetworkingMiddleware] process
14 | * the [LoginAction.SignInButtonClicked] action when the email is invalid, it should then
15 | * have the store process the [LoginAction.InvalidEmailSubmitted] action.
16 | */
17 | @Test
18 | fun submitInvalidEmail() {
19 | // Givens
20 | val fakeLoginRepository = FakeLoginRepository()
21 | fakeLoginRepository.shouldMockSuccess = true
22 |
23 | val inputState = LoginViewState(email = "")
24 | val inputAction = LoginAction.SignInButtonClicked
25 |
26 | val middlewareUnderTest = LoginNetworkingMiddleware(fakeLoginRepository)
27 | val actionCaptureMiddleware = ActionCaptureMiddleware()
28 |
29 | val loginStore = Store(
30 | inputState,
31 | LoginReducer(),
32 | listOf(actionCaptureMiddleware),
33 | )
34 |
35 | // When
36 | runBlocking {
37 | middlewareUnderTest.process(inputAction, inputState, loginStore)
38 | }
39 |
40 | // Then
41 | actionCaptureMiddleware.assertActionProcessed(LoginAction.InvalidEmailSubmitted)
42 | }
43 |
44 | /**
45 | * When the [LoginNetworkingMiddleware] processes the [LoginAction.SignInButtonClicked] action
46 | * and there are valid inputs, we want to ensure that the corresponding actions are sent back
47 | * into the store.
48 | */
49 | @Test
50 | fun validLogin() {
51 | // Givens
52 | val fakeLoginRepository = FakeLoginRepository()
53 | fakeLoginRepository.shouldMockSuccess = true
54 |
55 | val inputState = LoginViewState(
56 | email = "testy@mctestface.com",
57 | password = "hunter2",
58 | )
59 | val inputAction = LoginAction.SignInButtonClicked
60 |
61 | val middlewareUnderTest = LoginNetworkingMiddleware(fakeLoginRepository)
62 | val actionCaptureMiddleware = ActionCaptureMiddleware()
63 |
64 | val loginStore = Store(
65 | inputState,
66 | LoginReducer(),
67 | listOf(actionCaptureMiddleware),
68 | )
69 |
70 | // When
71 | runBlocking {
72 | middlewareUnderTest.process(inputAction, inputState, loginStore)
73 | }
74 |
75 | // Then
76 | actionCaptureMiddleware.assertActionProcessed(LoginAction.LoginStarted)
77 | actionCaptureMiddleware.assertActionProcessed(LoginAction.LoginCompleted)
78 | }
79 |
80 | @Test
81 | fun loginFailure() {
82 | // Givens
83 | val fakeLoginRepository = FakeLoginRepository()
84 | fakeLoginRepository.shouldMockSuccess = false
85 |
86 | val inputState = LoginViewState(
87 | email = "testy@mctestface.com",
88 | password = "hunter2",
89 | )
90 | val inputAction = LoginAction.SignInButtonClicked
91 |
92 | val middlewareUnderTest = LoginNetworkingMiddleware(fakeLoginRepository)
93 | val actionCaptureMiddleware = ActionCaptureMiddleware()
94 |
95 | val loginStore = Store(
96 | inputState,
97 | LoginReducer(),
98 | listOf(actionCaptureMiddleware),
99 | )
100 |
101 | // When
102 | runBlocking {
103 | middlewareUnderTest.process(inputAction, inputState, loginStore)
104 | }
105 |
106 | // Then
107 | actionCaptureMiddleware.assertActionProcessed(LoginAction.LoginStarted)
108 | actionCaptureMiddleware.assertActionProcessed(LoginAction.LoginFailed(null))
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/app/src/test/java/com/adammcneilly/mviexample/redux/StoreTest.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.redux
2 |
3 | import com.adammcneilly.mviexample.ActionCaptureMiddleware
4 | import com.google.common.truth.Truth.assertThat
5 | import kotlinx.coroutines.runBlocking
6 | import org.junit.Test
7 |
8 | class StoreTest {
9 | @Test
10 | fun dispatchSendsActionToReducerAndMiddlewares() {
11 | // Given
12 | val inputState = TestState
13 | val inputAction = TestAction
14 | val reducer = TestReducer()
15 | val middleware = ActionCaptureMiddleware()
16 |
17 | val store = Store(
18 | inputState,
19 | reducer,
20 | listOf(middleware)
21 | )
22 |
23 | // When
24 | runBlocking {
25 | store.dispatch(inputAction)
26 | }
27 |
28 | // Then
29 | middleware.assertActionProcessed(inputAction)
30 | reducer.assertActionProcessed(inputAction)
31 | }
32 | }
33 |
34 | object TestState : State
35 |
36 | object TestAction : Action
37 |
38 | /**
39 | * Please see notes in LoginReducer and Reducer in app directory for real documentation.
40 | */
41 | class TestReducer : Reducer {
42 | private val actionHistory: MutableList = mutableListOf()
43 |
44 | override fun reduce(currentState: State, action: Action): State {
45 | actionHistory.add(action)
46 |
47 | return currentState
48 | }
49 |
50 | fun assertActionProcessed(expectedAction: Action) {
51 | assertThat(actionHistory).contains(expectedAction)
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/app/src/test/java/com/adammcneilly/mviexample/ui/login/LoginReducerTest.kt:
--------------------------------------------------------------------------------
1 | package com.adammcneilly.mviexample.ui.login
2 |
3 | import com.google.common.truth.Truth.assertThat
4 | import org.junit.Test
5 |
6 | class LoginReducerTest {
7 |
8 | /**
9 | * A standard reducer test will take an input state and input action and have the reducer
10 | * reduce that into a new state.
11 | *
12 | * We then want to compare that new state with what is expected.
13 | */
14 | @Test
15 | fun emailChangedUpdatesEmail() {
16 | val inputState = LoginViewState()
17 | val inputAction = LoginAction.EmailChanged("testy@mctestface.com")
18 |
19 | val expectedState = inputState.copy(
20 | email = "testy@mctestface.com",
21 | )
22 |
23 | val reducer = LoginReducer()
24 | val newState = reducer.reduce(inputState, inputAction)
25 | assertThat(newState).isEqualTo(expectedState)
26 | }
27 |
28 | /**
29 | * An example of a simplified reducer test is to take an input state and action, have the reducer
30 | * reduce this to a new state, and then perform assertions only on the properties we expected
31 | * to be updated.
32 | *
33 | * This can make for simpler and shorter tests, but we should acknowledge that this means potential
34 | * side effects could sneak in our tests.
35 | */
36 | @Test
37 | fun loginStartedShowsProgressBar() {
38 | val inputState = LoginViewState()
39 | val inputAction = LoginAction.LoginStarted
40 |
41 | val reducer = LoginReducer()
42 | val newState = reducer.reduce(inputState, inputAction)
43 |
44 | assertThat(newState.showProgressBar).isTrue()
45 | }
46 |
47 | /**
48 | * This tests ensures that our fallback scenario - dispatching an action that isn't processed
49 | * by this reducer - will return the original state.
50 | */
51 | @Test
52 | fun unsupportedActionReturnsOriginalState() {
53 | val inputState = LoginViewState()
54 | val inputAction = LoginAction.SignInButtonClicked
55 |
56 | val reducer = LoginReducer()
57 | val newState = reducer.reduce(inputState, inputAction)
58 |
59 | assertThat(newState).isEqualTo(inputState)
60 | }
61 | }
--------------------------------------------------------------------------------
/assets/MVIDiagram.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/assets/MVIDiagram.png
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | repositories {
4 | google()
5 | mavenCentral()
6 | }
7 | dependencies {
8 | classpath "com.android.tools.build:gradle:4.2.1"
9 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0"
10 |
11 | // NOTE: Do not place your application dependencies here; they belong
12 | // in the individual module build.gradle files
13 | }
14 | }
15 |
16 | task clean(type: Delete) {
17 | delete rootProject.buildDir
18 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdamMc331/MVIExample/63378680c74c7a8975de7c1bbc1dc6f2b266e004/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed May 26 10:40:18 EDT 2021
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-rc-1-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | dependencyResolutionManagement {
2 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
3 | repositories {
4 | google()
5 | mavenCentral()
6 | jcenter() // Warning: this repository is going to shut down soon
7 | }
8 | }
9 | rootProject.name = "MVIExample"
10 | include ':app'
11 |
--------------------------------------------------------------------------------