├── .gitignore ├── .idea ├── .gitignore ├── codeStyles │ ├── Project.xml │ └── codeStyleConfig.xml ├── compiler.xml ├── gradle.xml ├── jarRepositories.xml ├── misc.xml └── sonarlint │ └── issuestore │ ├── 0 │ └── 7 │ │ └── 078bbc8c79e5f6c93d82fb933a120170044904e1 │ ├── 5 │ └── e │ │ └── 5e516db28ed6a3281539813ab18e2f2d24d97b3d │ ├── f │ └── 4 │ │ └── f4a01d6a4fcb971362ec00a83903fd3902f52164 │ └── index.pb ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── plcoding │ │ └── stateflow │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── plcoding │ │ │ └── stateflow │ │ │ ├── MainActivity.kt │ │ │ └── MainViewModel.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values-night │ │ └── themes.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ └── test │ └── java │ └── com │ └── plcoding │ └── stateflow │ └── ExampleUnitTest.kt ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | .cxx 15 | local.properties 16 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 20 | 22 | 23 | 135 | 136 | 138 | 139 | -------------------------------------------------------------------------------- /.idea/codeStyles/codeStyleConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/0/7/078bbc8c79e5f6c93d82fb933a120170044904e1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/StateFlow/c48e565abd87e379008a7f8f01ed2507a4b9dd3b/.idea/sonarlint/issuestore/0/7/078bbc8c79e5f6c93d82fb933a120170044904e1 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/5/e/5e516db28ed6a3281539813ab18e2f2d24d97b3d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/StateFlow/c48e565abd87e379008a7f8f01ed2507a4b9dd3b/.idea/sonarlint/issuestore/5/e/5e516db28ed6a3281539813ab18e2f2d24d97b3d -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/f/4/f4a01d6a4fcb971362ec00a83903fd3902f52164: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/philipplackner/StateFlow/c48e565abd87e379008a7f8f01ed2507a4b9dd3b/.idea/sonarlint/issuestore/f/4/f4a01d6a4fcb971362ec00a83903fd3902f52164 -------------------------------------------------------------------------------- /.idea/sonarlint/issuestore/index.pb: -------------------------------------------------------------------------------- 1 | 2 | @ 3 | app/build.gradle,f\4\f4a01d6a4fcb971362ec00a83903fd3902f52164 4 | i 5 | 9app/src/main/java/com/plcoding/stateflow/MainViewModel.kt,5\e\5e516db28ed6a3281539813ab18e2f2d24d97b3d 6 | h 7 | 8app/src/main/java/com/plcoding/stateflow/MainActivity.kt,0\7\078bbc8c79e5f6c93d82fb933a120170044904e1 -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | } 5 | 6 | android { 7 | compileSdkVersion 30 8 | buildToolsVersion "30.0.2" 9 | 10 | defaultConfig { 11 | applicationId "com.plcoding.stateflow" 12 | minSdkVersion 21 13 | targetSdkVersion 30 14 | versionCode 1 15 | versionName "1.0" 16 | 17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | buildFeatures { 20 | viewBinding true 21 | } 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = '1.8' 34 | } 35 | } 36 | 37 | dependencies { 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 39 | implementation 'androidx.core:core-ktx:1.3.2' 40 | implementation 'androidx.appcompat:appcompat:1.2.0' 41 | implementation 'com.google.android.material:material:1.2.1' 42 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 43 | testImplementation 'junit:junit:4.+' 44 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 45 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 46 | 47 | // Activity KTX for viewModels() 48 | implementation "androidx.activity:activity-ktx:1.1.0" 49 | 50 | // Architectural Components 51 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" 52 | 53 | // Coroutines 54 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9' 55 | implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9' 56 | 57 | // Coroutine Lifecycle Scopes 58 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0" 59 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0" 60 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/plcoding/stateflow/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.stateflow 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.plcoding.stateflow", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/stateflow/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.stateflow 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.activity.viewModels 6 | import androidx.core.view.isVisible 7 | import androidx.lifecycle.lifecycleScope 8 | import com.google.android.material.snackbar.Snackbar 9 | import com.plcoding.stateflow.databinding.ActivityMainBinding 10 | import kotlinx.coroutines.ExperimentalCoroutinesApi 11 | import kotlinx.coroutines.flow.collect 12 | import kotlinx.coroutines.launch 13 | 14 | @ExperimentalCoroutinesApi 15 | class MainActivity : AppCompatActivity() { 16 | 17 | private var _binding: ActivityMainBinding? = null 18 | private val binding get() = _binding!! 19 | 20 | private val viewModel: MainViewModel by viewModels() 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | _binding = ActivityMainBinding.inflate(layoutInflater) 25 | setContentView(binding.root) 26 | 27 | binding.btnLogin.setOnClickListener { 28 | viewModel.login( 29 | binding.etUsername.text.toString(), 30 | binding.etPassword.text.toString() 31 | ) 32 | } 33 | 34 | lifecycleScope.launchWhenStarted { 35 | viewModel.loginUiState.collect { 36 | when (it) { 37 | is MainViewModel.LoginUiState.Success -> { 38 | Snackbar.make( 39 | binding.root, 40 | "Successfully logged in", 41 | Snackbar.LENGTH_LONG 42 | ).show() 43 | binding.progressBar.isVisible = false 44 | } 45 | is MainViewModel.LoginUiState.Error -> { 46 | Snackbar.make( 47 | binding.root, 48 | it.message, 49 | Snackbar.LENGTH_LONG 50 | ).show() 51 | binding.progressBar.isVisible = false 52 | } 53 | is MainViewModel.LoginUiState.Loading -> { 54 | binding.progressBar.isVisible = true 55 | } 56 | else -> Unit 57 | } 58 | } 59 | } 60 | } 61 | 62 | override fun onDestroy() { 63 | super.onDestroy() 64 | _binding = null 65 | } 66 | } -------------------------------------------------------------------------------- /app/src/main/java/com/plcoding/stateflow/MainViewModel.kt: -------------------------------------------------------------------------------- 1 | package com.plcoding.stateflow 2 | 3 | import androidx.lifecycle.ViewModel 4 | import androidx.lifecycle.viewModelScope 5 | import kotlinx.coroutines.ExperimentalCoroutinesApi 6 | import kotlinx.coroutines.delay 7 | import kotlinx.coroutines.flow.MutableStateFlow 8 | import kotlinx.coroutines.flow.StateFlow 9 | import kotlinx.coroutines.launch 10 | 11 | @ExperimentalCoroutinesApi 12 | class MainViewModel : ViewModel() { 13 | 14 | private val _loginUiState = MutableStateFlow(LoginUiState.Empty) 15 | val loginUiState: StateFlow = _loginUiState 16 | 17 | fun login(username: String, password: String) = viewModelScope.launch { 18 | _loginUiState.value = LoginUiState.Loading 19 | delay(2000L) 20 | if(username == "android" && password == "topsecret") { 21 | _loginUiState.value = LoginUiState.Success 22 | } else { 23 | _loginUiState.value = LoginUiState.Error("Wrong credentials") 24 | } 25 | } 26 | 27 | sealed class LoginUiState { 28 | object Success : LoginUiState() 29 | data class Error(val message: String) : LoginUiState() 30 | object Loading : LoginUiState() 31 | object Empty : LoginUiState() 32 | } 33 | 34 | 35 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 20 | 21 | 26 | 27 | 28 | 29 | 40 | 41 | 47 | 48 | 49 | 50 |