├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── ru │ │ └── kontur │ │ └── mobile │ │ └── visualfsm │ │ └── sample_android │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── ru │ │ │ └── kontur │ │ │ └── mobile │ │ │ └── visualfsm │ │ │ └── sample_android │ │ │ ├── App.kt │ │ │ ├── BaseActivity.kt │ │ │ ├── MainActivity.kt │ │ │ ├── di │ │ │ └── Modules.kt │ │ │ ├── feature │ │ │ └── auth │ │ │ │ ├── data │ │ │ │ ├── AuthResult.kt │ │ │ │ ├── RegistrationResult.kt │ │ │ │ └── UserFlow.kt │ │ │ │ ├── di │ │ │ │ └── Modules.kt │ │ │ │ ├── fsm │ │ │ │ ├── AuthFSMAsyncWorker.kt │ │ │ │ ├── AuthFSMState.kt │ │ │ │ ├── AuthFeature.kt │ │ │ │ └── actions │ │ │ │ │ ├── AuthFSMAction.kt │ │ │ │ │ ├── Authenticate.kt │ │ │ │ │ ├── ChangeFlow.kt │ │ │ │ │ ├── HandleAuthResult.kt │ │ │ │ │ ├── HandleChangeLoginData.kt │ │ │ │ │ ├── HandleChangeRegistrationData.kt │ │ │ │ │ ├── HandleConfirmation.kt │ │ │ │ │ ├── HandleRegistrationResult.kt │ │ │ │ │ ├── HandleSnackBarShowed.kt │ │ │ │ │ ├── Logout.kt │ │ │ │ │ └── StartRegistration.kt │ │ │ │ └── interactor │ │ │ │ └── AuthInteractor.kt │ │ │ └── ui │ │ │ ├── auth │ │ │ ├── ScreenDataMapper.kt │ │ │ ├── component │ │ │ │ ├── AuthScreenComponents.kt │ │ │ │ ├── LoginScreenContent.kt │ │ │ │ ├── RegistrationScreenContent.kt │ │ │ │ └── UserAuthorizedScreenContent.kt │ │ │ ├── data │ │ │ │ ├── AuthScreenData.kt │ │ │ │ ├── LoginScreenData.kt │ │ │ │ ├── RegistrationScreenData.kt │ │ │ │ └── UserAuthorizedScreenData.kt │ │ │ └── screen │ │ │ │ ├── LoginScreen.kt │ │ │ │ ├── RegistrationScreen.kt │ │ │ │ └── UserAuthorizedScreen.kt │ │ │ ├── common │ │ │ └── CustomView.kt │ │ │ └── theme │ │ │ ├── Color.kt │ │ │ ├── Shape.kt │ │ │ ├── Theme.kt │ │ │ └── Type.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.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 │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── ru │ └── kontur │ └── mobile │ └── visualfsm │ └── sample_android │ └── AuthFSMTests.kt ├── build.gradle ├── docs ├── README-RU.md ├── confirm.png ├── error.png ├── graph.png ├── login.png ├── reg.png ├── reg_progress.png ├── snack.png └── welcome.png ├── 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 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | .cxx 10 | local.properties 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 SKB Kontur 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Sample of usage VisualFSM for Android application - Kotlin Coroutines, Jetpack Compose 2 | 3 | [![Telegram](https://img.shields.io/static/v1?label=Telegram&message=Channel&color=0088CC)](https://t.me/visualfsm) 4 | [![Telegram](https://img.shields.io/static/v1?label=Telegram&message=Chat&color=0088CC)](https://t.me/visualfsm_support) 5 | 6 | ENG | [RUS](docs/README-RU.md) 7 | 8 | [VisualFSM](https://github.com/Kontur-Mobile/VisualFSM) is a Kotlin library that implements an **MVI architecture** 9 | (`Model-View-Intent`)[[1]](#what-is-mvi) and a set of tools for visualization and analysis of 10 | **FSM**'s (`Finite-state machine`)[[2]](#what-is-fsm) diagram of states. 11 | 12 | The graph is being built from source code of **FSM**'s implementation. There is no need of custom 13 | written configurations for **FSM**, you can just create new State and Action classes, they would be 14 | automatically added to the graph of States and Transitions. 15 | 16 | Source code analysis and the graph built are being performed with reflection and declared as a 17 | separate module that would allow it to be connected to testing environment. 18 | 19 | ### Authorization and registration process 20 | 21 | graph 22 | 23 | Feature: [AuthFeature.kt](./app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFeature.kt) 24 | 25 | States: [AuthFSMState.kt](./app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFSMState.kt) 26 | 27 | Actions: [actions](./app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions) 28 | 29 | AsyncWorker: [AuthFSMAsyncWorker.kt](./app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFSMAsyncWorker.kt) 30 | 31 | States to Ui data models mapper: [ScreenDataMapper.kt](./app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/ScreenDataMapper.kt) 32 | 33 | Generate digraph and tests 34 | sample: [AuthFSMTests.kt](./app/src/test/java/ru/kontur/mobile/visualfsm/sample_android/AuthFSMTests.kt) 35 | 36 | For CI visualization use [graphviz](https://graphviz.org/doc/info/command.html), for the local visualization (on your 37 | PC) use [webgraphviz](http://www.webgraphviz.com/). 38 | 39 | ### Screenshots 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 |
LoginRegistrationConfirmationRequested
AsyncWorkState.RegisteringLogin with snackbarUserAuthorized
63 | 64 | ### What is MVI 65 | 66 | `MVI` stands for **Model-View-Intent**. It is an architectural pattern that utilizes _unidirectional 67 | data flow_. The data circulates between `Model` and `View` only in one direction - from `Model` 68 | to `View` and from `View` to `Model`. 69 | 70 | [More on hannesdorfmann](http://hannesdorfmann.com/android/model-view-intent/) 71 | 72 | ### What is FSM 73 | 74 | A `finite-state machine` (FSM) is an abstract machine that can be in exactly one of a finite number 75 | of states at any given time. The `FSM` can change from one state to another in response to some 76 | inputs. 77 | 78 | [More on wikipedia](https://en.wikipedia.org/wiki/Finite-state_machine) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | id 'com.google.devtools.ksp' version "$ksp_version" 5 | id 'kotlin-parcelize' 6 | } 7 | 8 | android { 9 | compileSdk 33 10 | 11 | defaultConfig { 12 | applicationId "ru.kontur.mobile.visualfsm.sample_android" 13 | minSdk 23 14 | targetSdk 33 15 | versionCode 1 16 | versionName "1.0" 17 | 18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 19 | vectorDrawables { 20 | useSupportLibrary true 21 | } 22 | } 23 | 24 | applicationVariants.all { variant -> 25 | variant.sourceSets.java.each { 26 | it.srcDirs += "build/generated/ksp/${variant.name}/kotlin" 27 | } 28 | } 29 | 30 | buildTypes { 31 | release { 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | compileOptions { 37 | sourceCompatibility JavaVersion.VERSION_1_8 38 | targetCompatibility JavaVersion.VERSION_1_8 39 | } 40 | kotlinOptions { 41 | jvmTarget = '1.8' 42 | } 43 | buildFeatures { 44 | compose true 45 | } 46 | composeOptions { 47 | kotlinCompilerExtensionVersion compose_compiler_version 48 | } 49 | packagingOptions { 50 | resources { 51 | excludes += '/META-INF/{AL2.0,LGPL2.1}' 52 | } 53 | } 54 | namespace 'ru.kontur.mobile.visualfsm.sample_android' 55 | } 56 | 57 | dependencies { 58 | // VisualFSM base classes 59 | implementation "ru.kontur.mobile.visualfsm:visualfsm-core:$visualfsm_version" 60 | 61 | // Code generation 62 | ksp "ru.kontur.mobile.visualfsm:visualfsm-compiler:$visualfsm_version" 63 | 64 | // Classes for easy getting generated code 65 | implementation "ru.kontur.mobile.visualfsm:visualfsm-providers:$visualfsm_version" 66 | 67 | // Graph creation and analysis 68 | testImplementation "ru.kontur.mobile.visualfsm:visualfsm-tools:$visualfsm_version" 69 | 70 | implementation "androidx.core:core-ktx:$core_ktx_version" 71 | implementation "androidx.compose.ui:ui:$compose_version" 72 | implementation "androidx.compose.material:material:$compose_version" 73 | implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" 74 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_runtime_ktx_version" 75 | implementation "androidx.activity:activity-compose:$activity_compose_version" 76 | implementation "io.insert-koin:koin-core:$koin_version" 77 | implementation "io.insert-koin:koin-android:$koin_version" 78 | 79 | testImplementation "junit:junit:$junit_version" 80 | testImplementation "io.insert-koin:koin-test:$koin_version" 81 | testImplementation "io.insert-koin:koin-test-junit4:$koin_version" 82 | 83 | androidTestImplementation "androidx.test.ext:junit:$test_ext_junit_version" 84 | androidTestImplementation "androidx.test.espresso:espresso-core:$espresso_core_version" 85 | androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" 86 | 87 | debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" 88 | } -------------------------------------------------------------------------------- /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/ru/kontur/mobile/visualfsm/sample_android/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android 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("ru.kontur.mobile.visualfsm.sample_android", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/App.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android 2 | 3 | import android.app.Application 4 | import org.koin.android.ext.koin.androidLogger 5 | import org.koin.core.context.startKoin 6 | import ru.kontur.mobile.visualfsm.sample_android.di.appModule 7 | import java.util.* 8 | 9 | class App : Application() { 10 | override fun onCreate() { 11 | super.onCreate() 12 | initKoin() 13 | } 14 | 15 | private fun initKoin() { 16 | startKoin { 17 | androidLogger() 18 | modules(appModule) 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/BaseActivity.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android 2 | 3 | import android.os.Bundle 4 | import android.util.Log 5 | import androidx.activity.ComponentActivity 6 | import org.koin.core.component.KoinScopeComponent 7 | import org.koin.core.component.getScopeId 8 | import org.koin.core.context.loadKoinModules 9 | import org.koin.core.context.unloadKoinModules 10 | import org.koin.core.module.Module 11 | import org.koin.core.qualifier.Qualifier 12 | import org.koin.core.qualifier.StringQualifier 13 | import org.koin.core.scope.Scope 14 | 15 | abstract class BaseActivity : ComponentActivity(), KoinScopeComponent { 16 | override val scope: Scope by lazy { 17 | // Scope id and qualifier use same string for load state based module 18 | getKoin().getOrCreateScope(stateScopeId, StringQualifier(stateScopeId)) 19 | } 20 | 21 | protected open val stateScopeModule: (Qualifier, Bundle?) -> Module? = { _, _ -> null } 22 | 23 | private lateinit var stateScopeId: String 24 | 25 | override fun onCreate(savedInstanceState: Bundle?) { 26 | super.onCreate(savedInstanceState) 27 | stateScopeId = savedInstanceState?.getString(STATE_SCOPE_ID) ?: getScopeId() 28 | 29 | if (getKoin().getScopeOrNull(stateScopeId) == null) { 30 | Log.d(this::class.simpleName, "Init new UI scope: ${scope.id}") 31 | stateScopeModule(scope.scopeQualifier, savedInstanceState)?.let { 32 | loadKoinModules(it) 33 | } 34 | } else { 35 | Log.d(this::class.simpleName, "Use exist UI scope: ${scope.id}") 36 | } 37 | } 38 | 39 | override fun onSaveInstanceState(outState: Bundle) { 40 | outState.putString(STATE_SCOPE_ID, scope.id) 41 | super.onSaveInstanceState(outState) 42 | } 43 | 44 | override fun onDestroy() { 45 | super.onDestroy() 46 | if (!isChangingConfigurations || isFinishing) { 47 | closeScopeAndRemoveStateModule() 48 | } 49 | } 50 | 51 | private fun closeScopeAndRemoveStateModule() { 52 | Log.d(this::class.simpleName, "Destroy UI scope: ${scope.id}") 53 | scope.close() 54 | stateScopeModule(scope.scopeQualifier, null)?.let { module -> 55 | unloadKoinModules(module) 56 | } 57 | } 58 | 59 | companion object { 60 | private const val STATE_SCOPE_ID = "state_scope_id" 61 | } 62 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android 2 | 3 | import android.os.Bundle 4 | import androidx.activity.compose.setContent 5 | import androidx.compose.foundation.layout.fillMaxSize 6 | import androidx.compose.material.MaterialTheme 7 | import androidx.compose.material.Surface 8 | import androidx.compose.runtime.Composable 9 | import androidx.compose.runtime.collectAsState 10 | import androidx.compose.ui.Modifier 11 | import org.koin.android.ext.android.inject 12 | import org.koin.core.module.dsl.scopedOf 13 | import org.koin.core.qualifier.Qualifier 14 | import org.koin.dsl.module 15 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.di.AuthStateModuleFactory 16 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.di.AuthStateModuleFactory.AUTH_FSM_SAVED_STATE 17 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMAsyncWorker 18 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 19 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFeature 20 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.ScreenDataMapper 21 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.LoginScreenData 22 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.RegistrationScreenData 23 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.UserAuthorizedScreenData 24 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.screen.LoginScreen 25 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.screen.RegistrationScreen 26 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.screen.UserAuthorizedScreen 27 | import ru.kontur.mobile.visualfsm.sample_android.ui.theme.VisualFSMSampleAndroidTheme 28 | 29 | class MainActivity : BaseActivity() { 30 | 31 | private val authFeature: AuthFeature by inject() 32 | 33 | override val stateScopeModule = { stateScopeQualifier: Qualifier, bundle: Bundle? -> 34 | AuthStateModuleFactory.create(stateScopeQualifier, bundle) 35 | } 36 | 37 | override fun onCreate(savedInstanceState: Bundle?) { 38 | super.onCreate(savedInstanceState) 39 | 40 | setContent { 41 | VisualFSMSampleAndroidTheme { 42 | Surface( 43 | modifier = Modifier 44 | .fillMaxSize(), 45 | color = MaterialTheme.colors.background 46 | ) { 47 | AuthFlow(authFeature) 48 | } 49 | } 50 | } 51 | } 52 | 53 | override fun onSaveInstanceState(outState: Bundle) { 54 | outState.putParcelable(AUTH_FSM_SAVED_STATE, authFeature.getCurrentState()) 55 | super.onSaveInstanceState(outState) 56 | } 57 | } 58 | 59 | @Composable 60 | private fun AuthFlow(authFeature: AuthFeature) { 61 | val state = authFeature.observeState() 62 | .collectAsState( 63 | initial = authFeature.getCurrentState() 64 | ).value 65 | 66 | when (val data = ScreenDataMapper.map(state)) { 67 | is LoginScreenData -> LoginScreen(data, authFeature) 68 | is RegistrationScreenData -> RegistrationScreen(data, authFeature) 69 | is UserAuthorizedScreenData -> UserAuthorizedScreen(data, authFeature) 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/di/Modules.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.di 2 | 3 | import org.koin.core.module.dsl.singleOf 4 | import org.koin.dsl.module 5 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.interactor.AuthInteractor 6 | 7 | val appModule = module { 8 | singleOf(::AuthInteractor) 9 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/data/AuthResult.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.data 2 | 3 | enum class AuthResult { 4 | SUCCESS, 5 | BAD_CREDENTIAL, 6 | NO_INTERNET 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/data/RegistrationResult.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.data 2 | 3 | enum class RegistrationResult { 4 | SUCCESS, 5 | USER_ALREADY_REGISTERED, 6 | NO_INTERNET 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/data/UserFlow.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.data 2 | 3 | enum class UserFlow { 4 | LOGIN, 5 | PASSWORD_RESTORE, 6 | REGISTRATION 7 | } 8 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/di/Modules.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.di 2 | 3 | import android.os.Bundle 4 | import org.koin.core.module.dsl.scopedOf 5 | import org.koin.core.qualifier.Qualifier 6 | import org.koin.dsl.module 7 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMAsyncWorker 8 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 9 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFeature 10 | 11 | object AuthStateModuleFactory { 12 | 13 | fun create(stateScopeQualifier: Qualifier, bundle: Bundle?) = module { 14 | scope(stateScopeQualifier) { 15 | scopedOf(::AuthFSMAsyncWorker) 16 | scoped { 17 | AuthFeature( 18 | getSavedOrInitialAuthFSMState(bundle), 19 | get() 20 | ) 21 | } 22 | } 23 | } 24 | 25 | private fun getSavedOrInitialAuthFSMState(bundle: Bundle?): AuthFSMState { 26 | val initialState = AuthFSMState.Login("", "") 27 | 28 | return bundle?.getParcelable(AUTH_FSM_SAVED_STATE) ?: initialState 29 | } 30 | 31 | const val AUTH_FSM_SAVED_STATE = "auth_fsm_saved_state" 32 | } 33 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFSMAsyncWorker.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm 2 | 3 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions.AuthFSMAction 4 | import ru.kontur.mobile.visualfsm.AsyncWorker 5 | import ru.kontur.mobile.visualfsm.AsyncWorkerTask 6 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.interactor.AuthInteractor 7 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 8 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions.HandleAuthResult 9 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions.HandleRegistrationResult 10 | 11 | class AuthFSMAsyncWorker(private val authInteractor: AuthInteractor) : 12 | AsyncWorker() { 13 | 14 | override fun onNextState(state: AuthFSMState): AsyncWorkerTask { 15 | return when (state) { 16 | is AsyncWorkState.Authenticating -> { 17 | AsyncWorkerTask.ExecuteAndCancelExist(state) { 18 | val result = authInteractor.check(state.mail, state.password) 19 | proceed(HandleAuthResult(result)) 20 | } 21 | } 22 | is AsyncWorkState.Registering -> { 23 | AsyncWorkerTask.ExecuteIfNotExist(state) { 24 | val result = authInteractor.register(state.mail, state.password) 25 | proceed(HandleRegistrationResult(result)) 26 | } 27 | } 28 | else -> AsyncWorkerTask.Cancel() 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFSMState.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm 2 | 3 | import android.os.Parcelable 4 | import kotlinx.parcelize.Parcelize 5 | import ru.kontur.mobile.visualfsm.State 6 | 7 | @Parcelize 8 | sealed class AuthFSMState : State, Parcelable { 9 | data class Login( 10 | val mail: String, 11 | val password: String, 12 | val errorMessage: String? = null, 13 | val snackBarMessage: String? = null, 14 | ) : AuthFSMState() 15 | 16 | data class Registration( 17 | val mail: String, 18 | val password: String, 19 | val repeatedPassword: String, 20 | val errorMessage: String? = null 21 | ) : AuthFSMState() 22 | 23 | data class ConfirmationRequested( 24 | val mail: String, 25 | val password: String 26 | ) : AuthFSMState() 27 | 28 | @Parcelize 29 | sealed class AsyncWorkState : AuthFSMState() { 30 | data class Authenticating( 31 | val mail: String, 32 | val password: String 33 | ) : AsyncWorkState() 34 | 35 | data class Registering( 36 | val mail: String, 37 | val password: String 38 | ) : AsyncWorkState() 39 | } 40 | 41 | data class UserAuthorized(val mail: String) : AuthFSMState() 42 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFeature.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm 2 | 3 | import ru.kontur.mobile.visualfsm.AsyncWorker 4 | import ru.kontur.mobile.visualfsm.Feature 5 | import ru.kontur.mobile.visualfsm.GenerateTransitionsFactory 6 | import ru.kontur.mobile.visualfsm.providers.GeneratedTransitionsFactoryProvider.provideTransitionsFactory 7 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.data.UserFlow 8 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions.* 9 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.interactor.AuthInteractor 10 | 11 | @GenerateTransitionsFactory 12 | class AuthFeature(initialState: AuthFSMState, asyncWorker: AuthFSMAsyncWorker) : Feature( 13 | initialState = initialState, 14 | asyncWorker = asyncWorker, 15 | transitionsFactory = provideTransitionsFactory() 16 | ) { 17 | 18 | fun toRegistration() { 19 | proceed(ChangeFlow(UserFlow.REGISTRATION)) 20 | } 21 | 22 | fun toLogin() { 23 | proceed(ChangeFlow(UserFlow.LOGIN)) 24 | } 25 | 26 | fun logout() { 27 | proceed(Logout()) 28 | } 29 | 30 | fun confirmRegistrationData() { 31 | proceed(HandleConfirmation(true)) 32 | } 33 | 34 | fun declineRegistrationData() { 35 | proceed(HandleConfirmation(false)) 36 | } 37 | 38 | fun startAuthenticating() { 39 | proceed(Authenticate()) 40 | } 41 | 42 | fun startRegistration() { 43 | proceed(StartRegistration()) 44 | } 45 | 46 | fun handleChangeRegistrationData(mail: String, password: String, repeatPassword: String) { 47 | proceed(HandleChangeRegistrationData(mail, password, repeatPassword)) 48 | } 49 | 50 | fun handleChangeLoginData(mail: String, password: String) { 51 | proceed(HandleChangeLoginData(mail, password)) 52 | } 53 | 54 | fun handleSnackBarShowed() { 55 | proceed(HandleSnackBarShowed()) 56 | } 57 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/AuthFSMAction.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 4 | import ru.kontur.mobile.visualfsm.Action 5 | 6 | sealed class AuthFSMAction : Action() 7 | -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/Authenticate.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 5 | 6 | class Authenticate : AuthFSMAction() { 7 | inner class AuthenticationStart : Transition() { 8 | override fun transform(state: Login): AsyncWorkState.Authenticating { 9 | return AsyncWorkState.Authenticating(state.mail, state.password) 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/ChangeFlow.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.data.UserFlow 5 | import ru.kontur.mobile.visualfsm.Edge 6 | import ru.kontur.mobile.visualfsm.Transition 7 | 8 | class ChangeFlow(val newFlow: UserFlow) : AuthFSMAction() { 9 | 10 | @Edge("ToLogin") 11 | inner class RegisterToLogin : Transition() { 12 | override fun transform(state: Registration): Login { 13 | return Login(state.mail, "") 14 | } 15 | } 16 | 17 | @Edge("ToRegistration") 18 | inner class LoginToRegistration : Transition() { 19 | override fun transform(state: Login): Registration { 20 | return Registration(state.mail, "", "") 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/HandleAuthResult.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 5 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.data.AuthResult 6 | 7 | class HandleAuthResult(val result: AuthResult) : AuthFSMAction() { 8 | 9 | inner class Success : Transition() { 10 | override fun predicate(state: AsyncWorkState.Authenticating): Boolean { 11 | return result == AuthResult.SUCCESS 12 | } 13 | 14 | override fun transform(state: AsyncWorkState.Authenticating): UserAuthorized { 15 | return UserAuthorized(state.mail) 16 | } 17 | } 18 | 19 | inner class BadCredential : Transition() { 20 | override fun predicate(state: AsyncWorkState.Authenticating): Boolean { 21 | return result == AuthResult.BAD_CREDENTIAL 22 | } 23 | 24 | override fun transform(state: AsyncWorkState.Authenticating): Login { 25 | return Login(state.mail, state.password, "Bad credential") 26 | } 27 | } 28 | 29 | inner class ConnectionFailed : Transition() { 30 | override fun predicate(state: AsyncWorkState.Authenticating): Boolean { 31 | return result == AuthResult.NO_INTERNET 32 | } 33 | 34 | override fun transform(state: AsyncWorkState.Authenticating): Login { 35 | return Login(state.mail, state.password, "No internet") 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/HandleChangeLoginData.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 5 | 6 | class HandleChangeLoginData( 7 | val mail: String, 8 | val password: String, 9 | ): AuthFSMAction() { 10 | 11 | inner class ChangeLoginData: Transition() { 12 | override fun transform(state: AuthFSMState.Login): AuthFSMState.Login { 13 | return AuthFSMState.Login(mail, password) 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/HandleChangeRegistrationData.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 5 | 6 | class HandleChangeRegistrationData( 7 | val mail: String, 8 | val password: String, 9 | val repeatPassword: String, 10 | ) : AuthFSMAction() { 11 | 12 | inner class ChangeRegistrationData : Transition() { 13 | override fun transform(state: AuthFSMState.Registration): AuthFSMState.Registration { 14 | return AuthFSMState.Registration(mail, password, repeatPassword) 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/HandleConfirmation.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 5 | 6 | class HandleConfirmation(val confirmed: Boolean) : AuthFSMAction() { 7 | inner class Confirm : Transition() { 8 | override fun predicate(state: ConfirmationRequested): Boolean { 9 | return confirmed 10 | } 11 | 12 | override fun transform(state: ConfirmationRequested): AsyncWorkState.Registering { 13 | return AsyncWorkState.Registering(state.mail, state.password) 14 | } 15 | } 16 | 17 | inner class Cancel : Transition() { 18 | override fun predicate(state: ConfirmationRequested): Boolean { 19 | return !confirmed 20 | } 21 | 22 | override fun transform(state: ConfirmationRequested): Registration { 23 | return Registration(state.mail, state.password, state.password) 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/HandleRegistrationResult.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 5 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.data.RegistrationResult 6 | 7 | class HandleRegistrationResult(val result: RegistrationResult) : AuthFSMAction() { 8 | 9 | inner class Success : Transition() { 10 | override fun predicate(state: AsyncWorkState.Registering): Boolean { 11 | return result == RegistrationResult.SUCCESS 12 | } 13 | 14 | override fun transform(state: AsyncWorkState.Registering): Login { 15 | return Login( 16 | mail = state.mail, 17 | password = state.password, 18 | snackBarMessage = "${state.mail} registered" 19 | ) 20 | } 21 | } 22 | 23 | inner class BadCredential : Transition() { 24 | override fun predicate(state: AsyncWorkState.Registering): Boolean { 25 | return result == RegistrationResult.USER_ALREADY_REGISTERED 26 | } 27 | 28 | override fun transform(state: AsyncWorkState.Registering): Registration { 29 | return Registration( 30 | mail = state.mail, 31 | password = state.password, 32 | repeatedPassword = state.password, 33 | errorMessage = "User already registered" 34 | ) 35 | } 36 | } 37 | 38 | inner class ConnectionFailed : Transition() { 39 | override fun predicate(state: AsyncWorkState.Registering): Boolean { 40 | return result == RegistrationResult.NO_INTERNET 41 | } 42 | 43 | override fun transform(state: AsyncWorkState.Registering): Registration { 44 | return Registration( 45 | mail = state.mail, 46 | password = state.password, 47 | repeatedPassword = state.password, 48 | errorMessage = "No internet" 49 | ) 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/HandleSnackBarShowed.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 5 | 6 | class HandleSnackBarShowed() : AuthFSMAction() { 7 | 8 | inner class SnackBarShowed : Transition() { 9 | override fun transform(state: AuthFSMState.Login): AuthFSMState.Login { 10 | return state.copy(snackBarMessage = null) 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/Logout.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 4 | import ru.kontur.mobile.visualfsm.Transition 5 | 6 | class Logout : AuthFSMAction() { 7 | inner class Logout : Transition() { 8 | override fun transform(state: UserAuthorized) = Login("", "") 9 | } 10 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions/StartRegistration.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions 2 | 3 | import ru.kontur.mobile.visualfsm.Transition 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState.* 5 | 6 | class StartRegistration : AuthFSMAction() { 7 | inner class RegistrationStart : Transition() { 8 | override fun predicate(state: Registration): Boolean { 9 | return state.password == state.repeatedPassword && state.password.isNotBlank() 10 | } 11 | 12 | override fun transform(state: Registration): ConfirmationRequested { 13 | return ConfirmationRequested(state.mail, state.password) 14 | } 15 | } 16 | 17 | inner class ValidationFailed : Transition() { 18 | override fun predicate(state: Registration): Boolean { 19 | return state.password != state.repeatedPassword || state.password.isBlank() 20 | } 21 | 22 | override fun transform(state: Registration): Registration { 23 | return Registration( 24 | state.mail, 25 | state.password, 26 | state.repeatedPassword, 27 | "Password and repeated password must be equals and not empty" 28 | ) 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/interactor/AuthInteractor.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.feature.auth.interactor 2 | 3 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.data.AuthResult 4 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.data.RegistrationResult 5 | import kotlinx.coroutines.delay 6 | 7 | class AuthInteractor { 8 | var registeredMail: String = "" 9 | var registeredPassword: String = "" 10 | 11 | suspend fun check(mail: String, password: String): AuthResult { 12 | delay(1500) 13 | return if (registeredMail == mail && registeredPassword == password 14 | && mail.isNotBlank() && password.isNotBlank() 15 | ) { 16 | AuthResult.SUCCESS 17 | } else { 18 | AuthResult.BAD_CREDENTIAL 19 | } 20 | } 21 | 22 | suspend fun register(mail: String, password: String): RegistrationResult { 23 | delay(1500) 24 | return if (registeredMail == mail) { 25 | RegistrationResult.USER_ALREADY_REGISTERED 26 | } else { 27 | registeredMail = mail 28 | registeredPassword = password 29 | RegistrationResult.SUCCESS 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/ScreenDataMapper.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth 2 | 3 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 4 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.AuthScreenData 5 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.LoginScreenData 6 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.RegistrationScreenData 7 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.UserAuthorizedScreenData 8 | 9 | object ScreenDataMapper { 10 | fun map(state: AuthFSMState): AuthScreenData { 11 | return when (state) { 12 | is AuthFSMState.Login -> LoginScreenData( 13 | mail = state.mail, 14 | password = state.password, 15 | errorMessage = state.errorMessage, 16 | isAuthenticationInProgress = false, 17 | snackBarMessage = state.snackBarMessage 18 | ) 19 | is AuthFSMState.AsyncWorkState.Authenticating -> LoginScreenData( 20 | mail = state.mail, 21 | password = state.password, 22 | errorMessage = null, 23 | isAuthenticationInProgress = true, 24 | snackBarMessage = null 25 | ) 26 | is AuthFSMState.Registration -> RegistrationScreenData( 27 | mail = state.mail, 28 | password = state.password, 29 | repeatedPassword = state.repeatedPassword, 30 | errorMessage = state.errorMessage, 31 | isRegistrationInProgress = false, 32 | isConfirmationRequested = false 33 | ) 34 | is AuthFSMState.AsyncWorkState.Registering -> RegistrationScreenData( 35 | mail = state.mail, 36 | password = state.password, 37 | repeatedPassword = state.password, 38 | errorMessage = null, 39 | isRegistrationInProgress = true, 40 | isConfirmationRequested = false 41 | ) 42 | is AuthFSMState.ConfirmationRequested -> RegistrationScreenData( 43 | mail = state.mail, 44 | password = state.password, 45 | repeatedPassword = state.password, 46 | errorMessage = null, 47 | isRegistrationInProgress = false, 48 | isConfirmationRequested = true 49 | ) 50 | is AuthFSMState.UserAuthorized -> UserAuthorizedScreenData( 51 | mail = state.mail 52 | ) 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/component/AuthScreenComponents.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.component 2 | 3 | import androidx.compose.material.Icon 4 | import androidx.compose.material.icons.Icons 5 | import androidx.compose.material.icons.filled.Email 6 | import androidx.compose.material.icons.filled.Lock 7 | import androidx.compose.runtime.Composable 8 | import androidx.compose.ui.Modifier 9 | import androidx.compose.ui.text.input.PasswordVisualTransformation 10 | import ru.kontur.mobile.visualfsm.sample_android.ui.common.CustomInputField 11 | 12 | @Composable 13 | fun EmailInputField( 14 | modifier: Modifier = Modifier, 15 | message: String, 16 | onValueChange: (String) -> Unit, 17 | ) { 18 | CustomInputField( 19 | message = message, 20 | onValueChange = onValueChange, 21 | placeHolder = "Email", 22 | leadingIcon = { Icon(Icons.Filled.Email, contentDescription = "input email") }, 23 | modifier = modifier 24 | ) 25 | } 26 | 27 | @Composable 28 | fun PasswordInputField( 29 | modifier: Modifier = Modifier, 30 | message: String, 31 | onValueChange: (String) -> Unit, 32 | placeHolder: String, 33 | ) { 34 | CustomInputField( 35 | message = message, 36 | onValueChange = onValueChange, 37 | placeHolder = placeHolder, 38 | leadingIcon = { Icon(Icons.Filled.Lock, contentDescription = "input password") }, 39 | modifier = modifier, 40 | visualTransformation = PasswordVisualTransformation() 41 | ) 42 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/component/LoginScreenContent.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.component 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.material.CircularProgressIndicator 6 | import androidx.compose.material.MaterialTheme 7 | import androidx.compose.material.Text 8 | import androidx.compose.runtime.Composable 9 | import androidx.compose.ui.Alignment 10 | import androidx.compose.ui.Modifier 11 | import androidx.compose.ui.graphics.Color 12 | import androidx.compose.ui.tooling.preview.Preview 13 | import androidx.compose.ui.unit.dp 14 | import androidx.compose.ui.unit.sp 15 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.LoginScreenData 16 | import ru.kontur.mobile.visualfsm.sample_android.ui.common.CustomButton 17 | import ru.kontur.mobile.visualfsm.sample_android.ui.common.CustomTextButton 18 | 19 | @Composable 20 | fun LoginScreenContent( 21 | data: LoginScreenData, 22 | onMailChange: (String) -> Unit, 23 | onPasswordChange: (String) -> Unit, 24 | onSignInClick: () -> Unit, 25 | onSignUpClick: () -> Unit, 26 | ) { 27 | Column( 28 | modifier = Modifier 29 | .padding(horizontal = 20.dp), 30 | horizontalAlignment = Alignment.CenterHorizontally, 31 | verticalArrangement = Arrangement.Center, 32 | ) { 33 | Spacer(modifier = Modifier.height(64.dp)) 34 | 35 | LoginText() 36 | 37 | Spacer(modifier = Modifier.height(64.dp)) 38 | EmailInputField( 39 | modifier = Modifier.fillMaxWidth(), 40 | message = data.mail, 41 | onValueChange = onMailChange 42 | ) 43 | Spacer(modifier = Modifier.height(16.dp)) 44 | PasswordInputField( 45 | placeHolder = "Password", 46 | modifier = Modifier.fillMaxWidth(), 47 | message = data.password, 48 | onValueChange = onPasswordChange 49 | ) 50 | 51 | if (data.errorMessage != null && data.errorMessage.isNotBlank()) { 52 | Spacer(modifier = Modifier.height(16.dp)) 53 | Text( 54 | text = data.errorMessage, 55 | color = MaterialTheme.colors.error 56 | ) 57 | } 58 | 59 | Spacer(modifier = Modifier.height(32.dp)) 60 | SignUpText( 61 | modifier = Modifier.fillMaxWidth(), 62 | onSignUpClick 63 | ) 64 | Spacer(modifier = Modifier.height(32.dp)) 65 | 66 | if (data.isAuthenticationInProgress) { 67 | CircularProgressIndicator() 68 | } else { 69 | SignInButton( 70 | modifier = Modifier 71 | .fillMaxWidth(), 72 | onSignInClick 73 | ) 74 | } 75 | } 76 | } 77 | 78 | @Composable 79 | private fun LoginText( 80 | modifier: Modifier = Modifier 81 | ) { 82 | Text( 83 | text = "Login", 84 | fontSize = 20.sp, 85 | modifier = modifier 86 | ) 87 | } 88 | 89 | @Composable 90 | private fun SignInButton( 91 | modifier: Modifier = Modifier, 92 | onClick: () -> Unit, 93 | ) { 94 | CustomButton( 95 | onClick = onClick, 96 | text = "Sign in", 97 | modifier = modifier 98 | ) 99 | } 100 | 101 | @Composable 102 | private fun SignUpText( 103 | modifier: Modifier = Modifier, 104 | onClick: () -> Unit, 105 | ) { 106 | Box( 107 | modifier = modifier, 108 | contentAlignment = Alignment.CenterEnd 109 | ) { 110 | CustomTextButton( 111 | text = "Sign up", 112 | onClick = onClick, 113 | ) 114 | } 115 | } 116 | 117 | 118 | @Preview 119 | @Composable 120 | fun LoginScreenContentPreview() { 121 | Box(modifier = Modifier.background(MaterialTheme.colors.background)) { 122 | LoginScreenContent( 123 | LoginScreenData( 124 | mail = "test@test.com", 125 | password = "", 126 | errorMessage = null, 127 | isAuthenticationInProgress = false, 128 | snackBarMessage = null, 129 | ), 130 | {}, 131 | {}, 132 | {}, 133 | {}) 134 | } 135 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/component/RegistrationScreenContent.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.component 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.material.* 6 | import androidx.compose.material.icons.Icons 7 | import androidx.compose.material.icons.filled.ArrowBack 8 | import androidx.compose.runtime.* 9 | import androidx.compose.ui.Alignment 10 | import androidx.compose.ui.Modifier 11 | import androidx.compose.ui.tooling.preview.Preview 12 | import androidx.compose.ui.unit.dp 13 | import androidx.compose.ui.unit.sp 14 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.RegistrationScreenData 15 | import ru.kontur.mobile.visualfsm.sample_android.ui.common.CustomButton 16 | 17 | 18 | @Composable 19 | fun RegistrationScreenContent( 20 | data: RegistrationScreenData, 21 | onMailChange: (String) -> Unit, 22 | onPasswordChange: (String) -> Unit, 23 | onRepeatedPasswordChange: (String) -> Unit, 24 | onRegistrationClick: () -> Unit, 25 | ) { 26 | Column( 27 | modifier = Modifier 28 | .padding(horizontal = 20.dp), 29 | horizontalAlignment = Alignment.CenterHorizontally, 30 | verticalArrangement = Arrangement.Center, 31 | ) { 32 | Spacer(modifier = Modifier.height(16.dp)) 33 | RegistrationText() 34 | Spacer(modifier = Modifier.height(64.dp)) 35 | EmailInputField( 36 | modifier = Modifier.fillMaxWidth(), 37 | message = data.mail, 38 | onValueChange = onMailChange 39 | ) 40 | Spacer(modifier = Modifier.height(16.dp)) 41 | PasswordInputField( 42 | message = data.password, 43 | onValueChange = onPasswordChange, 44 | placeHolder = "Password", 45 | modifier = Modifier.fillMaxWidth() 46 | ) 47 | Spacer(modifier = Modifier.height(16.dp)) 48 | PasswordInputField( 49 | message = data.repeatedPassword, 50 | onValueChange = onRepeatedPasswordChange, 51 | placeHolder = "Repeat password", 52 | modifier = Modifier.fillMaxWidth() 53 | ) 54 | if (data.errorMessage != null && data.errorMessage.isNotBlank()) { 55 | Spacer(modifier = Modifier.height(16.dp)) 56 | Text( 57 | text = data.errorMessage, 58 | color = MaterialTheme.colors.error 59 | ) 60 | } 61 | Spacer(modifier = Modifier.height(96.dp)) 62 | 63 | if (data.isRegistrationInProgress) { 64 | CircularProgressIndicator() 65 | } else { 66 | SignUpButton( 67 | modifier = Modifier.fillMaxWidth(), 68 | onClick = onRegistrationClick, 69 | ) 70 | } 71 | } 72 | } 73 | 74 | @Composable 75 | private fun ButtonArrowBack( 76 | modifier: Modifier = Modifier, 77 | onClick: () -> Unit 78 | ) { 79 | IconButton( 80 | modifier = modifier, 81 | onClick = onClick, 82 | ) { 83 | Icon( 84 | imageVector = Icons.Filled.ArrowBack, 85 | contentDescription = "return previous screen" 86 | ) 87 | } 88 | } 89 | 90 | @Composable 91 | private fun RegistrationText( 92 | modifier: Modifier = Modifier 93 | ) { 94 | Text( 95 | text = "Registration", 96 | fontSize = 20.sp, 97 | modifier = modifier 98 | ) 99 | } 100 | 101 | @Composable 102 | private fun SignUpButton( 103 | modifier: Modifier = Modifier, 104 | onClick: () -> Unit 105 | ) { 106 | CustomButton( 107 | onClick = onClick, 108 | text = "Sign up", 109 | modifier = modifier 110 | ) 111 | } 112 | 113 | @Preview 114 | @Composable 115 | fun RegistrationScreenContentPreview() { 116 | Box(modifier = Modifier.background(MaterialTheme.colors.background)) { 117 | RegistrationScreenContent( 118 | RegistrationScreenData( 119 | mail = "test@test.com", 120 | password = "", 121 | repeatedPassword = "", 122 | errorMessage = null, 123 | isRegistrationInProgress = false, 124 | isConfirmationRequested = false 125 | ), 126 | {}, 127 | {}, 128 | {}, 129 | {}) 130 | } 131 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/component/UserAuthorizedScreenContent.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.component 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.material.MaterialTheme 6 | import androidx.compose.material.Text 7 | import androidx.compose.material.TextButton 8 | import androidx.compose.runtime.Composable 9 | import androidx.compose.ui.Alignment 10 | import androidx.compose.ui.Modifier 11 | import androidx.compose.ui.tooling.preview.Preview 12 | import androidx.compose.ui.unit.dp 13 | import androidx.compose.ui.unit.sp 14 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.UserAuthorizedScreenData 15 | 16 | @Composable 17 | fun UserAuthorizedScreenContent( 18 | state: UserAuthorizedScreenData, 19 | onLogoutClick: () -> Unit 20 | ) { 21 | Column( 22 | modifier = Modifier 23 | .padding(horizontal = 20.dp), 24 | horizontalAlignment = Alignment.CenterHorizontally, 25 | verticalArrangement = Arrangement.Top 26 | ) { 27 | Spacer(modifier = Modifier.height(128.dp)) 28 | Text( 29 | text = "Welcome!", 30 | fontSize = 26.sp, 31 | ) 32 | Text( 33 | text = state.mail, 34 | fontSize = 18.sp, 35 | ) 36 | Spacer(modifier = Modifier.height(32.dp)) 37 | TextButton( 38 | onClick = onLogoutClick, 39 | modifier = Modifier.fillMaxWidth() 40 | ) { 41 | Text( 42 | text = "Log out", 43 | fontSize = 18.sp 44 | ) 45 | } 46 | } 47 | } 48 | 49 | @Preview 50 | @Composable 51 | fun UserAuthorizedScreenContentPreview() { 52 | Box(modifier = Modifier.background(MaterialTheme.colors.background)) { 53 | UserAuthorizedScreenContent( 54 | UserAuthorizedScreenData( 55 | mail = "test@test.com" 56 | ) 57 | ) {} 58 | } 59 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/data/AuthScreenData.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.data 2 | 3 | sealed class AuthScreenData -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/data/LoginScreenData.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.data 2 | 3 | data class LoginScreenData( 4 | val mail: String, 5 | val password: String, 6 | val errorMessage: String?, 7 | val isAuthenticationInProgress: Boolean, 8 | val snackBarMessage: String? 9 | ) : AuthScreenData() -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/data/RegistrationScreenData.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.data 2 | 3 | data class RegistrationScreenData( 4 | val mail: String, 5 | val password: String, 6 | val repeatedPassword: String, 7 | val errorMessage: String?, 8 | val isRegistrationInProgress: Boolean, 9 | val isConfirmationRequested: Boolean, 10 | ) : AuthScreenData() -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/data/UserAuthorizedScreenData.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.data 2 | 3 | data class UserAuthorizedScreenData( 4 | val mail: String, 5 | ) : AuthScreenData() -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/screen/LoginScreen.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.screen 2 | 3 | import androidx.compose.foundation.layout.* 4 | import androidx.compose.foundation.rememberScrollState 5 | import androidx.compose.foundation.verticalScroll 6 | import androidx.compose.material.Scaffold 7 | import androidx.compose.material.rememberScaffoldState 8 | import androidx.compose.runtime.Composable 9 | import androidx.compose.runtime.LaunchedEffect 10 | import androidx.compose.ui.Modifier 11 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFeature 12 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.component.LoginScreenContent 13 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.LoginScreenData 14 | 15 | @Composable 16 | fun LoginScreen( 17 | data: LoginScreenData, 18 | authFeature: AuthFeature 19 | ) { 20 | val scaffoldState = rememberScaffoldState() 21 | 22 | if (!data.snackBarMessage.isNullOrBlank()) { 23 | LaunchedEffect(scaffoldState.snackbarHostState) { 24 | scaffoldState.snackbarHostState.showSnackbar(data.snackBarMessage) 25 | authFeature.handleSnackBarShowed() 26 | } 27 | } 28 | 29 | Scaffold(scaffoldState = scaffoldState) { padding -> 30 | Box( 31 | modifier = Modifier 32 | .verticalScroll(rememberScrollState()) 33 | .fillMaxSize() 34 | .padding(padding) 35 | ) { 36 | LoginScreenContent(data = data, 37 | onMailChange = { mail -> 38 | authFeature.handleChangeLoginData(mail, data.password) 39 | }, 40 | onPasswordChange = { password -> 41 | authFeature.handleChangeLoginData( 42 | data.mail, 43 | password 44 | ) 45 | }, 46 | onSignInClick = { authFeature.startAuthenticating() }, 47 | onSignUpClick = { authFeature.toRegistration() }) 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/screen/RegistrationScreen.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.screen 2 | 3 | import androidx.activity.compose.BackHandler 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.foundation.rememberScrollState 6 | import androidx.compose.foundation.verticalScroll 7 | import androidx.compose.material.* 8 | import androidx.compose.material.icons.Icons 9 | import androidx.compose.material.icons.filled.ArrowBack 10 | import androidx.compose.runtime.* 11 | import androidx.compose.ui.Modifier 12 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFeature 13 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.component.RegistrationScreenContent 14 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.RegistrationScreenData 15 | import ru.kontur.mobile.visualfsm.sample_android.ui.common.ConfirmDialog 16 | 17 | @Composable 18 | fun RegistrationScreen( 19 | data: RegistrationScreenData, 20 | authFeature: AuthFeature 21 | ) { 22 | BackHandler(enabled = true) { 23 | authFeature.toLogin() 24 | } 25 | 26 | Scaffold( 27 | topBar = { 28 | TopAppBar( 29 | backgroundColor = MaterialTheme.colors.background, 30 | ) { 31 | ButtonArrowBack { 32 | authFeature.toLogin() 33 | } 34 | } 35 | }, 36 | ) { padding -> 37 | Box( 38 | modifier = Modifier 39 | .verticalScroll(rememberScrollState()) 40 | .fillMaxSize() 41 | .padding(padding) 42 | ) { 43 | RegistrationScreenContent(data = data, 44 | onMailChange = { mail -> 45 | authFeature.handleChangeRegistrationData( 46 | mail, 47 | data.password, data.repeatedPassword 48 | ) 49 | }, 50 | onPasswordChange = { password -> 51 | authFeature.handleChangeRegistrationData( 52 | data.mail, 53 | password, data.repeatedPassword 54 | ) 55 | }, 56 | onRepeatedPasswordChange = { repeatedPassword -> 57 | authFeature.handleChangeRegistrationData( 58 | data.mail, 59 | data.password, 60 | repeatedPassword 61 | ) 62 | }, 63 | onRegistrationClick = { authFeature.startRegistration() }) 64 | } 65 | if (data.isConfirmationRequested) { 66 | ConfirmDialog( 67 | title = "Confirm registration", 68 | description = "Continue with current data?", 69 | onDismiss = { authFeature.declineRegistrationData() }, 70 | onConfirm = { authFeature.confirmRegistrationData() }, 71 | onCancel = { authFeature.declineRegistrationData() }, 72 | ) 73 | } 74 | } 75 | } 76 | 77 | 78 | @Composable 79 | private fun ButtonArrowBack( 80 | modifier: Modifier = Modifier, 81 | onClick: () -> Unit 82 | ) { 83 | IconButton( 84 | modifier = modifier, 85 | onClick = onClick, 86 | ) { 87 | Icon( 88 | imageVector = Icons.Filled.ArrowBack, 89 | contentDescription = "return previous screen" 90 | ) 91 | } 92 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/screen/UserAuthorizedScreen.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.auth.screen 2 | 3 | import androidx.compose.foundation.layout.* 4 | import androidx.compose.material.Scaffold 5 | import androidx.compose.runtime.Composable 6 | import androidx.compose.ui.Modifier 7 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFeature 8 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.component.UserAuthorizedScreenContent 9 | import ru.kontur.mobile.visualfsm.sample_android.ui.auth.data.UserAuthorizedScreenData 10 | 11 | @Composable 12 | fun UserAuthorizedScreen( 13 | data: UserAuthorizedScreenData, 14 | authFeature: AuthFeature 15 | ) { 16 | Scaffold { padding -> 17 | Box( 18 | modifier = Modifier 19 | .fillMaxSize() 20 | .padding(padding) 21 | ) { 22 | UserAuthorizedScreenContent( 23 | data, 24 | onLogoutClick = { authFeature.logout() } 25 | ) 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/common/CustomView.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.common 2 | 3 | import androidx.compose.foundation.background 4 | import androidx.compose.foundation.layout.* 5 | import androidx.compose.foundation.shape.RoundedCornerShape 6 | import androidx.compose.material.* 7 | import androidx.compose.runtime.* 8 | import androidx.compose.ui.Alignment 9 | import androidx.compose.ui.Modifier 10 | import androidx.compose.ui.graphics.Color 11 | import androidx.compose.ui.text.TextStyle 12 | import androidx.compose.ui.text.input.VisualTransformation 13 | import androidx.compose.ui.text.style.TextAlign 14 | import androidx.compose.ui.unit.dp 15 | import androidx.compose.ui.unit.sp 16 | import androidx.compose.ui.window.Dialog 17 | 18 | @Composable 19 | fun CustomInputField( 20 | modifier: Modifier = Modifier, 21 | message: String, 22 | onValueChange: (String) -> Unit, 23 | placeHolder: String, 24 | leadingIcon: @Composable (() -> Unit), 25 | visualTransformation: VisualTransformation = VisualTransformation.None 26 | ) { 27 | TextField( 28 | value = message, 29 | placeholder = { Text(placeHolder) }, 30 | onValueChange = onValueChange, 31 | colors = TextFieldDefaults.textFieldColors( 32 | backgroundColor = Color.White, 33 | ), 34 | leadingIcon = leadingIcon, 35 | modifier = modifier, 36 | textStyle = TextStyle( 37 | fontSize = 16.sp 38 | ), 39 | visualTransformation = visualTransformation, 40 | singleLine = true 41 | ) 42 | } 43 | 44 | @Composable 45 | fun CustomButton( 46 | onClick: () -> Unit, 47 | text: String, 48 | modifier: Modifier = Modifier, 49 | ) { 50 | Button( 51 | onClick = onClick, 52 | shape = RoundedCornerShape(50), 53 | modifier = modifier, 54 | contentPadding = PaddingValues(16.dp) 55 | ) { 56 | Text( 57 | text = text, 58 | color = Color.White, 59 | fontSize = 20.sp 60 | ) 61 | } 62 | } 63 | 64 | @Composable 65 | fun CustomTextButton( 66 | text: String, 67 | onClick: () -> Unit, 68 | modifier: Modifier = Modifier, 69 | ) { 70 | TextButton( 71 | onClick = onClick, 72 | ) { 73 | Text( 74 | text = text, 75 | textAlign = TextAlign.Right, 76 | modifier = modifier 77 | ) 78 | } 79 | } 80 | 81 | @Composable 82 | fun ConfirmDialog( 83 | title: String, 84 | description: String, 85 | onDismiss: () -> Unit, 86 | onConfirm: () -> Unit, 87 | onCancel: () -> Unit, 88 | ) { 89 | Dialog(onDismissRequest = onDismiss) { 90 | Box( 91 | modifier = Modifier 92 | .background( 93 | color = Color.White, 94 | shape = RoundedCornerShape(16.dp) 95 | ) 96 | ) { 97 | Column( 98 | modifier = Modifier 99 | .padding(16.dp) 100 | .wrapContentSize(), 101 | horizontalAlignment = Alignment.CenterHorizontally, 102 | verticalArrangement = Arrangement.Center 103 | ) { 104 | Text(text = title) 105 | Spacer(modifier = Modifier.height(8.dp)) 106 | Text(text = description) 107 | Spacer(modifier = Modifier.height(32.dp)) 108 | Row( 109 | modifier = Modifier.fillMaxWidth() 110 | ) { 111 | Button( 112 | onClick = onConfirm, 113 | modifier = Modifier 114 | .fillMaxWidth() 115 | .weight(1f) 116 | ) { 117 | Text( 118 | text = "Ok" 119 | ) 120 | } 121 | Spacer(modifier = Modifier.width(16.dp)) 122 | Button( 123 | onClick = onCancel, 124 | modifier = Modifier 125 | .fillMaxWidth() 126 | .weight(1f) 127 | ) { 128 | Text( 129 | text = "Cancel" 130 | ) 131 | } 132 | } 133 | } 134 | } 135 | } 136 | } 137 | 138 | @Composable 139 | fun CustomInformationDialog( 140 | title: String, 141 | description: String, 142 | onDismiss: () -> Unit, 143 | onConfirm: () -> Unit, 144 | ) { 145 | Dialog(onDismissRequest = onDismiss) { 146 | Box( 147 | modifier = Modifier 148 | .padding(horizontal = 16.dp, vertical = 256.dp) 149 | .fillMaxSize() 150 | .background( 151 | color = Color.White, 152 | shape = RoundedCornerShape(16.dp) 153 | ) 154 | ) { 155 | Column( 156 | modifier = Modifier 157 | .padding(8.dp) 158 | .fillMaxSize(), 159 | horizontalAlignment = Alignment.CenterHorizontally, 160 | verticalArrangement = Arrangement.Center 161 | ) { 162 | Text(text = title) 163 | Spacer(modifier = Modifier.height(8.dp)) 164 | Text(text = description) 165 | Spacer(modifier = Modifier.height(32.dp)) 166 | Row( 167 | modifier = Modifier.fillMaxWidth() 168 | ) { 169 | Button( 170 | onClick = onConfirm, 171 | modifier = Modifier 172 | .fillMaxWidth() 173 | .weight(1f) 174 | ) { 175 | Text( 176 | text = "Ok" 177 | ) 178 | } 179 | } 180 | } 181 | } 182 | } 183 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/theme/Color.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.theme 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | val Blue400 = Color(0xff42a5f5) 6 | val Blue600 = Color(0xff1e88e5) 7 | val Blue800 = Color(0xff1565c0) 8 | val Cyan200 = Color(0xff80deea) -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/theme/Shape.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.theme 2 | 3 | import androidx.compose.foundation.shape.RoundedCornerShape 4 | import androidx.compose.material.Shapes 5 | import androidx.compose.ui.unit.dp 6 | 7 | val Shapes = Shapes( 8 | small = RoundedCornerShape(4.dp), 9 | medium = RoundedCornerShape(4.dp), 10 | large = RoundedCornerShape(0.dp) 11 | ) -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/theme/Theme.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.theme 2 | 3 | import androidx.compose.foundation.isSystemInDarkTheme 4 | import androidx.compose.material.MaterialTheme 5 | import androidx.compose.material.darkColors 6 | import androidx.compose.material.lightColors 7 | import androidx.compose.runtime.Composable 8 | 9 | private val DarkColorPalette = darkColors( 10 | primary = Blue400, 11 | primaryVariant = Blue800, 12 | secondary = Cyan200 13 | ) 14 | 15 | private val LightColorPalette = lightColors( 16 | primary = Blue600, 17 | primaryVariant = Blue800, 18 | secondary = Cyan200 19 | 20 | /* Other default colors to override 21 | background = Color.White, 22 | surface = Color.White, 23 | onPrimary = Color.White, 24 | onSecondary = Color.Black, 25 | onBackground = Color.Black, 26 | onSurface = Color.Black, 27 | */ 28 | ) 29 | 30 | @Composable 31 | fun VisualFSMSampleAndroidTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) { 32 | val colors = if (darkTheme) { 33 | DarkColorPalette 34 | } else { 35 | LightColorPalette 36 | } 37 | 38 | MaterialTheme( 39 | colors = colors, 40 | typography = Typography, 41 | shapes = Shapes, 42 | content = content 43 | ) 44 | } -------------------------------------------------------------------------------- /app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/theme/Type.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android.ui.theme 2 | 3 | import androidx.compose.material.Typography 4 | import androidx.compose.ui.text.TextStyle 5 | import androidx.compose.ui.text.font.FontFamily 6 | import androidx.compose.ui.text.font.FontWeight 7 | import androidx.compose.ui.unit.sp 8 | 9 | // Set of Material typography styles to start with 10 | val Typography = Typography( 11 | body1 = TextStyle( 12 | fontFamily = FontFamily.Default, 13 | fontWeight = FontWeight.Normal, 14 | fontSize = 16.sp 15 | ) 16 | /* Other default text styles to override 17 | button = TextStyle( 18 | fontFamily = FontFamily.Default, 19 | fontWeight = FontWeight.W500, 20 | fontSize = 14.sp 21 | ), 22 | caption = TextStyle( 23 | fontFamily = FontFamily.Default, 24 | fontWeight = FontWeight.Normal, 25 | fontSize = 12.sp 26 | ) 27 | */ 28 | ) -------------------------------------------------------------------------------- /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/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/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF1565C0 7 | #FF03DAC5 8 | #FF018786 9 | #FF000000 10 | #FFFFFFFF 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | VisualFSM Sample Android 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /app/src/test/java/ru/kontur/mobile/visualfsm/sample_android/AuthFSMTests.kt: -------------------------------------------------------------------------------- 1 | package ru.kontur.mobile.visualfsm.sample_android 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.AuthFSMState 7 | import ru.kontur.mobile.visualfsm.sample_android.feature.auth.fsm.actions.AuthFSMAction 8 | import ru.kontur.mobile.visualfsm.tools.VisualFSM 9 | import ru.kontur.mobile.visualfsm.tools.graphviz.preset.DotAttributesDefaultPreset 10 | 11 | class AuthFSMTests { 12 | 13 | @Test 14 | fun generateDigraph() { 15 | println( 16 | VisualFSM.generateDigraph( 17 | baseAction = AuthFSMAction::class, 18 | baseState = AuthFSMState::class, 19 | initialState = AuthFSMState.Login::class, 20 | attributes = DotAttributesDefaultPreset(AuthFSMState.AsyncWorkState::class), 21 | ) 22 | ) 23 | assertTrue(true) 24 | } 25 | 26 | @Test 27 | fun allStatesReachableTest() { 28 | val notReachableStates = VisualFSM.getUnreachableStates( 29 | baseAction = AuthFSMAction::class, 30 | baseState = AuthFSMState::class, 31 | initialState = AuthFSMState.Login::class, 32 | ) 33 | 34 | assertTrue( 35 | "FSM have unreachable states: ${notReachableStates.joinToString(", ")}", 36 | notReachableStates.isEmpty() 37 | ) 38 | } 39 | 40 | @Test 41 | fun noFinalStateTest() { 42 | val finalStates = VisualFSM.getFinalStates( 43 | baseAction = AuthFSMAction::class, 44 | baseState = AuthFSMState::class, 45 | ) 46 | 47 | assertTrue( 48 | "FSM have not correct final states: ${finalStates.joinToString(", ")}", 49 | finalStates.isEmpty() 50 | ) 51 | } 52 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | ext { 3 | // Dependency versions 4 | compose_version = '1.2.1' 5 | compose_compiler_version = '1.3.2' 6 | kotlin_version = '1.7.20' 7 | ksp_version = '1.7.20-1.0.7' 8 | visualfsm_version = '1.4.0' 9 | lifecycle_runtime_ktx_version = '2.5.1' 10 | activity_compose_version = '1.6.0' 11 | core_ktx_version = '1.9.0' 12 | koin_version= '3.2.1' 13 | 14 | // Test dependency versions 15 | junit_version = '4.13.2' 16 | test_ext_junit_version= '1.1.3' 17 | espresso_core_version = '3.4.0' 18 | } 19 | }// Top-level build file where you can add configuration options common to all sub-projects/modules. 20 | plugins { 21 | id 'com.android.application' version '8.0.2' apply false 22 | id 'com.android.library' version '8.0.2' apply false 23 | id 'org.jetbrains.kotlin.android' version "$kotlin_version" apply false 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } -------------------------------------------------------------------------------- /docs/README-RU.md: -------------------------------------------------------------------------------- 1 | ## Пример использования VisualFSM в Android приложении - Kotlin Coroutines, Jetpack Compose 2 | 3 | [![Telegram](https://img.shields.io/static/v1?label=Telegram&message=Channel&color=0088CC)](https://t.me/visualfsm) 4 | [![Telegram](https://img.shields.io/static/v1?label=Telegram&message=Chat&color=0088CC)](https://t.me/visualfsm_support) 5 | 6 | [ENG](../README.md) | RUS 7 | 8 | [VisualFSM](https://github.com/Kontur-Mobile/VisualFSM) – это Kotlin-библиотека для реализации **MVI-архитектуры** 9 | (`Model-View-Intent`)[[1]](#что-такое-mvi) и набор инструментов для визуализации и анализа диаграммы 10 | состояний **конечного автомата** (`Finite-state machine`, далее FSM)[[2]](#что-такое-fsm). 11 | 12 | Визуализация происходит по исходному коду реализации FSM. Не требует написания отдельных 13 | конфигураторов для FSM, достаточно добавлять новые классы `State` и `Action` – они автоматически 14 | добавятся в граф состояний и переходов FSM. 15 | 16 | Анализ исходного кода и построение графа выполняется с помощью рефлексии и реализован отдельным 17 | модулем, что позволяет подключить его только к тестовой среде. 18 | 19 | ### Процесс авторизации и регистрации пользователя 20 | 21 | graph 22 | 23 | Feature: [AuthFeature.kt](../app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFeature.kt) 24 | 25 | States: [AuthFSMState.kt](../app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFSMState.kt) 26 | 27 | Actions: [actions](../app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/actions) 28 | 29 | AsyncWorker: [AuthFSMAsyncWorker.kt](../app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/feature/auth/fsm/AuthFSMAsyncWorker.kt) 30 | 31 | Маппер States в модель данных Ui: [ScreenDataMapper.kt](../app/src/main/java/ru/kontur/mobile/visualfsm/sample_android/ui/auth/ScreenDataMapper.kt) 32 | 33 | Генерация графа и пример тестов: [AuthFSMTests.kt](../app/src/test/java/ru/kontur/mobile/visualfsm/sample_android/AuthFSMTests.kt) 34 | 35 | Для визуализации на CI используйте утилиту [graphviz](https://graphviz.org/doc/info/command.html), для визуализации на компьютере разработчика используйте [webgraphviz](http://www.webgraphviz.com/). 36 | 37 | ### Скриншоты 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 |
LoginRegistrationConfirmationRequested
AsyncWorkState.RegisteringLogin with snackbarUserAuthorized
61 | 62 | ### Что такое MVI 63 | 64 | `MVI` расшифровывается как **Model-View-Intent**. Это архитектурный паттерн, который следует подходу 65 | _однонаправленный поток данных_ (_unidirectional data flow_). Данные передаются от `Model` 66 | к `View` только в одном направлении. 67 | 68 | [Подробнее на hannesdorfmann](http://hannesdorfmann.com/android/model-view-intent/) 69 | 70 | ### Что такое FSM 71 | 72 | `FSM` — это абстрактная сущность, которая может находиться только в одном из конечного количества 73 | состояний в определённый момент. Она может переходить из одного состояния в другой в ответ на 74 | входные данные. 75 | 76 | [Подробнее на wikipedia](https://en.wikipedia.org/wiki/Finite-state_machine) -------------------------------------------------------------------------------- /docs/confirm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/confirm.png -------------------------------------------------------------------------------- /docs/error.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/error.png -------------------------------------------------------------------------------- /docs/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/graph.png -------------------------------------------------------------------------------- /docs/login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/login.png -------------------------------------------------------------------------------- /docs/reg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/reg.png -------------------------------------------------------------------------------- /docs/reg_progress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/reg_progress.png -------------------------------------------------------------------------------- /docs/snack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/snack.png -------------------------------------------------------------------------------- /docs/welcome.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/docs/welcome.png -------------------------------------------------------------------------------- /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 | # Kotlin code style for this project: "official" or "obsolete": 19 | kotlin.code.style=official 20 | # Enables namespacing of each library's R class so that its R class includes only the 21 | # resources declared in the library itself and none from the library's dependencies, 22 | # thereby reducing the size of the R class for that library 23 | android.nonTransitiveRClass=true 24 | android.defaults.buildfeatures.buildconfig=true 25 | android.nonFinalResIds=false -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Kontur-Mobile/VisualFSM-Sample-Android/d2c9d379f522326d1e69e4ae1ac8bb4f2782e848/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jun 13 08:40:48 YEKT 2022 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-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 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | gradlePluginPortal() 4 | google() 5 | mavenCentral() 6 | } 7 | } 8 | dependencyResolutionManagement { 9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 10 | repositories { 11 | google() 12 | mavenCentral() 13 | } 14 | } 15 | rootProject.name = "VisualFSM Sample Android" 16 | include ':app' 17 | --------------------------------------------------------------------------------