From b331b0076172831192ab9c98dcab04b1acc2dab1 Mon Sep 17 00:00:00 2001 From: daye200 Date: Mon, 7 Oct 2024 00:16:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat(mvvm):mvvm=EA=B5=AC=EC=A1=B0=EB=A1=9C?= =?UTF-8?q?=20sharedpreference?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/alom/androidstudy1/MainActivity.kt | 37 ++++++++++++++++++- .../com/alom/androidstudy1/TextRepository.kt | 20 ++++++++++ .../com/alom/androidstudy1/TextViewModel.kt | 36 ++++++++++++++++++ .../androidstudy1/TextViewModelFactory.kt | 20 ++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/alom/androidstudy1/TextRepository.kt create mode 100644 app/src/main/java/com/alom/androidstudy1/TextViewModel.kt create mode 100644 app/src/main/java/com/alom/androidstudy1/TextViewModelFactory.kt diff --git a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt index c1bcbbf..99e3848 100644 --- a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt +++ b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt @@ -1,20 +1,55 @@ package com.alom.androidstudy1 import android.os.Bundle +import android.view.View import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle +import androidx.lifecycle.viewmodel.viewModelFactory +import com.alom.androidstudy1.databinding.ActivityMainBinding +import kotlinx.coroutines.launch class MainActivity : AppCompatActivity() { + lateinit var textViewModel: TextViewModel + private lateinit var binding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityMainBinding.inflate(layoutInflater) enableEdgeToEdge() - setContentView(R.layout.activity_main) + setContentView(binding.root) + val viewModelFactory =TextViewModelFactory(TextRepository(this)) + textViewModel = ViewModelProvider(this, viewModelFactory).get(TextViewModel::class.java) +// val sharedPreferences = getSharedPreferences("alom", MODE_PRIVATE) +// val saveText = sharedPreferences.getString("text","") + + lifecycleScope.launch{ + lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){ + + textViewModel.currentValue.collect{value-> + binding.editText.setText(value) + } + + } + } + + binding.button.setOnClickListener{ + handleButtonClick()} + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) insets } } + private fun handleButtonClick(){ + val userInputString = binding.editText.text.toString() + textViewModel.updateValue(userInputString) + + } } \ No newline at end of file diff --git a/app/src/main/java/com/alom/androidstudy1/TextRepository.kt b/app/src/main/java/com/alom/androidstudy1/TextRepository.kt new file mode 100644 index 0000000..92fcbbc --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/TextRepository.kt @@ -0,0 +1,20 @@ +package com.alom.androidstudy1 + +import android.content.Context +import android.content.Context.MODE_PRIVATE +import android.renderscript.ScriptGroup.Input +import androidx.datastore.core.DataStore +import java.util.prefs.Preferences + + +class TextRepository(context: Context) { + val sharedPreferences = context.getSharedPreferences("alom", MODE_PRIVATE) + fun saveValue(input: String) + { val editor = sharedPreferences.edit() + editor.putString("text",input) + editor.apply()} + fun getValue():String{ + return sharedPreferences.getString("text","")?:"" + } +// private val dataStore: DataStore = context.createDataStore() +} \ No newline at end of file diff --git a/app/src/main/java/com/alom/androidstudy1/TextViewModel.kt b/app/src/main/java/com/alom/androidstudy1/TextViewModel.kt new file mode 100644 index 0000000..0baf088 --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/TextViewModel.kt @@ -0,0 +1,36 @@ +package com.alom.androidstudy1 + +import androidx.appcompat.app.AppCompatActivity.MODE_PRIVATE +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.cancel +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch + +class TextViewModel( + private val textRepository: TextRepository +): ViewModel() { + + +// private val _currentUserInput = MutableStateFlow("") +// val currentUserInput: StateFlow = _currentUserInput + private val _currentValue = MutableStateFlow("") + val currentValue: StateFlow = _currentValue + init{ + val savedText = textRepository.getValue() + _currentValue.value =savedText.toString() + } + + fun updateValue(input: String){ + viewModelScope.launch { + textRepository.saveValue(input) + _currentValue.value = input + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/com/alom/androidstudy1/TextViewModelFactory.kt b/app/src/main/java/com/alom/androidstudy1/TextViewModelFactory.kt new file mode 100644 index 0000000..1a845cf --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/TextViewModelFactory.kt @@ -0,0 +1,20 @@ +package com.alom.androidstudy1 + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlin.coroutines.CoroutineContext + +class TextViewModelFactory( + private val repository: TextRepository):ViewModelProvider.Factory { + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(TextViewModel::class.java)) { + return TextViewModel(repository) as T + } else { + throw IllegalArgumentException() + } + + } +} \ No newline at end of file From e87263b87d3a3e47baf72233d7df700995f23e9f Mon Sep 17 00:00:00 2001 From: daye200 Date: Mon, 7 Oct 2024 00:18:51 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat[activity]:=20=ED=99=94=EB=A9=B4?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 11 ++++++++++ app/src/main/res/layout/activity_main.xml | 26 +++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e86faf7..bb096d4 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -16,6 +16,9 @@ android { testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } + buildFeatures { + viewBinding = true + } buildTypes { release { @@ -33,7 +36,15 @@ android { } dependencies { + val lifecycle_version = "2.8.5" + val arch_version = "2.2.0" + // ViewModel + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version") + // Lifecycles only (without ViewModel or LiveData) + implementation("androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9") + implementation("androidx.datastore:datastore-preferences:1.1.1") implementation(libs.androidx.core.ktx) implementation(libs.androidx.appcompat) implementation(libs.material) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 86a5d97..85a2070 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -8,12 +8,34 @@ tools:context=".MainActivity"> + + +