From 2652027314016a93a5dd22f03ee86c39959bdafb Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Thu, 3 Oct 2024 21:28:43 +0900 Subject: [PATCH 01/19] design: Apply design --- app/src/main/res/layout/activity_main.xml | 32 ++++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 86a5d97..3097e15 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,13 +7,37 @@ android:layout_height="match_parent" tools:context=".MainActivity"> + + + + + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintTop_toBottomOf="@id/et_memo" + app:layout_constraintBottom_toBottomOf="parent" + /> + \ No newline at end of file From 5769652cbbc2ca549de3c6aa0bd878d839f6c7c4 Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Thu, 3 Oct 2024 21:34:36 +0900 Subject: [PATCH 02/19] feat: Apply viewBinding --- .../java/com/alom/androidstudy1/MainActivity.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt index c1bcbbf..1e14f9e 100644 --- a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt +++ b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt @@ -5,16 +5,17 @@ import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import com.alom.androidstudy1.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { + + private lateinit var binding : ActivityMainBinding + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - enableEdgeToEdge() - setContentView(R.layout.activity_main) - 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 - } + + binding = ActivityMainBinding.inflate(layoutInflater) + setContentView(binding.root) + } } \ No newline at end of file From e1e8c86d0cce74bc01506e4b829d46df962a704e Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 4 Oct 2024 00:50:32 +0900 Subject: [PATCH 03/19] setting: Set ViewBinding --- app/build.gradle.kts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e86faf7..891fc07 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -17,6 +17,9 @@ android { testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" } + buildFeatures{ + viewBinding = true + } buildTypes { release { isMinifyEnabled = false From 8b90bb964f9b0dabe763dd1e3c8d32aa64cf22ab Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 4 Oct 2024 01:01:12 +0900 Subject: [PATCH 04/19] feat: Implement ViewModel Class --- .../androidstudy1/viewModel/MemoViewModel.kt | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt diff --git a/app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt b/app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt new file mode 100644 index 0000000..5d55ec1 --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt @@ -0,0 +1,28 @@ +package com.alom.androidstudy1.viewModel + +import android.content.Context +import android.content.Context.MODE_PRIVATE +import android.content.SharedPreferences +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel + +class MemoViewModel: ViewModel() { + + private val _currentValue = MutableLiveData() + val currentValue : LiveData + get() = _currentValue + + + + fun saveMemo(context: Context, memo: String) { + _currentValue.value = memo + val sharedPreferences = context.getSharedPreferences("memo", MODE_PRIVATE) + val editor = sharedPreferences.edit() + editor.putString("memo", memo) + editor.apply() + + + } + +} \ No newline at end of file From c2363f68820778f47bb6cfcc50f599c33d5bdfa4 Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 4 Oct 2024 01:03:21 +0900 Subject: [PATCH 05/19] feat: Implement save logic by SharedPreference,Design MVVM pattern (by LiveData) --- .../com/alom/androidstudy1/MainActivity.kt | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt index 1e14f9e..841f442 100644 --- a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt +++ b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt @@ -1,21 +1,41 @@ package com.alom.androidstudy1 import android.os.Bundle +import android.util.Log import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat +import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider import com.alom.androidstudy1.databinding.ActivityMainBinding +import com.alom.androidstudy1.viewModel.MemoViewModel class MainActivity : AppCompatActivity() { - + private lateinit var memoViewModel: MemoViewModel private lateinit var binding : ActivityMainBinding + private val TAG = "MainActivity" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) + memoViewModel = ViewModelProvider(this).get(MemoViewModel::class.java) + + val sharedPreferences = getSharedPreferences("memo", MODE_PRIVATE) + + Log.d(TAG, sharedPreferences.getString("memo","").toString()) + val initData = sharedPreferences.getString("memo","").toString() + binding.etMemo.setText(initData) + Log.d(TAG+"textview", binding.etMemo.text.toString()) + + memoViewModel.currentValue.observe(this, Observer { + binding.etMemo.setText(it.toString()) + }) + val btnSave = binding.btnSave + btnSave.setOnClickListener { + memoViewModel.saveMemo(this,binding.etMemo.text.toString()) + } } } \ No newline at end of file From fad0a22856ffd5e19543c8c2921505aaf1790ac8 Mon Sep 17 00:00:00 2001 From: KunHee Jung Date: Mon, 7 Oct 2024 00:10:12 +0900 Subject: [PATCH 06/19] Create PULL_REQUEST_TEMPLATE.md --- .github/PULL_REQUEST_TEMPLATE.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..69851c0 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,9 @@ +## 📋 설명 + + +## 🔍 상세 작업 내용 +- [ ] 상세 +- [ ] 상세 + +## ⏳ 기간 +`2024.10. - 2024.10.` From 44e3af09e6ab6852712595bc0d9aaaa9cfd62d38 Mon Sep 17 00:00:00 2001 From: KunHee Jung Date: Mon, 7 Oct 2024 00:30:38 +0900 Subject: [PATCH 07/19] Delete .github directory --- .github/PULL_REQUEST_TEMPLATE.md | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index 69851c0..0000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,9 +0,0 @@ -## 📋 설명 - - -## 🔍 상세 작업 내용 -- [ ] 상세 -- [ ] 상세 - -## ⏳ 기간 -`2024.10. - 2024.10.` From fa8e0f30f2fff70b18674063ff1c1acaf1c0c2aa Mon Sep 17 00:00:00 2001 From: KunHee Jung Date: Mon, 7 Oct 2024 00:31:36 +0900 Subject: [PATCH 08/19] Create pull_request_template.md --- pull_request_template.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 pull_request_template.md diff --git a/pull_request_template.md b/pull_request_template.md new file mode 100644 index 0000000..a11a17f --- /dev/null +++ b/pull_request_template.md @@ -0,0 +1,19 @@ +### 📝 Description + +ex) OO 기능 구현 + +### 💻 How To Test + + +### 💽 Commits + +ex) +구현 사항 요약 +1. OO 구조 설계 +2. OO 클래스 분리 +3. 메서드 구현 + + +### 🖼 결과 + +ex) 실행, 테스트 결과 또는 사진 첨부 From 376d528a8748be05ea5a6592aebc989fb7e44246 Mon Sep 17 00:00:00 2001 From: KunHee Jung Date: Mon, 7 Oct 2024 00:33:03 +0900 Subject: [PATCH 09/19] Delete pull_request_template.md --- pull_request_template.md | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 pull_request_template.md diff --git a/pull_request_template.md b/pull_request_template.md deleted file mode 100644 index a11a17f..0000000 --- a/pull_request_template.md +++ /dev/null @@ -1,19 +0,0 @@ -### 📝 Description - -ex) OO 기능 구현 - -### 💻 How To Test - - -### 💽 Commits - -ex) -구현 사항 요약 -1. OO 구조 설계 -2. OO 클래스 분리 -3. 메서드 구현 - - -### 🖼 결과 - -ex) 실행, 테스트 결과 또는 사진 첨부 From 1c1a89b8c38c35471c5bc1d7cb637d6ec13050ef Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Thu, 10 Oct 2024 23:58:21 +0900 Subject: [PATCH 10/19] refactor: Replace LiveData with StateFlow --- .../com/alom/androidstudy1/MemoViewModel.kt | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt diff --git a/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt b/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt new file mode 100644 index 0000000..d8d72e2 --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt @@ -0,0 +1,19 @@ +package com.alom.androidstudy1 +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.launch + +class MemoViewModel: ViewModel() { + + private val _currentValue = MutableStateFlow("") + val currentValue : StateFlow + get() = _currentValue + + fun saveMemo(memo: String) { + viewModelScope.launch { + _currentValue.emit(memo) + } + } +} \ No newline at end of file From dfa425e7dc92afbf1f0d2beac36988f2d19f5a3e Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Thu, 10 Oct 2024 23:58:44 +0900 Subject: [PATCH 11/19] refactor: Replace LiveData with StateFlow --- .../com/alom/androidstudy1/MainActivity.kt | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt index 841f442..1b8cfca 100644 --- a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt +++ b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt @@ -2,14 +2,13 @@ package com.alom.androidstudy1 import android.os.Bundle import android.util.Log -import androidx.activity.enableEdgeToEdge import androidx.appcompat.app.AppCompatActivity -import androidx.core.view.ViewCompat -import androidx.core.view.WindowInsetsCompat -import androidx.lifecycle.Observer +import androidx.lifecycle.Lifecycle import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope +import androidx.lifecycle.repeatOnLifecycle import com.alom.androidstudy1.databinding.ActivityMainBinding -import com.alom.androidstudy1.viewModel.MemoViewModel +import kotlinx.coroutines.launch class MainActivity : AppCompatActivity() { private lateinit var memoViewModel: MemoViewModel @@ -24,18 +23,26 @@ class MainActivity : AppCompatActivity() { memoViewModel = ViewModelProvider(this).get(MemoViewModel::class.java) val sharedPreferences = getSharedPreferences("memo", MODE_PRIVATE) - + val editor = sharedPreferences.edit() Log.d(TAG, sharedPreferences.getString("memo","").toString()) val initData = sharedPreferences.getString("memo","").toString() + binding.etMemo.setText(initData) Log.d(TAG+"textview", binding.etMemo.text.toString()) - memoViewModel.currentValue.observe(this, Observer { - binding.etMemo.setText(it.toString()) - }) - val btnSave = binding.btnSave - btnSave.setOnClickListener { - memoViewModel.saveMemo(this,binding.etMemo.text.toString()) + lifecycleScope.launch{ + lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){ + launch { + memoViewModel.currentValue.collect{ + editor.putString("memo",it) + editor.apply() + } + } + } + } + + binding.btnSave.setOnClickListener { + memoViewModel.saveMemo(binding.etMemo.text.toString()) } } } \ No newline at end of file From 81c998c0e3ee7f1c60acbe5bc967e7c9b39e5f2d Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 00:00:00 +0900 Subject: [PATCH 12/19] chore: Add lifecycle implementation --- app/build.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 891fc07..61879cb 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -45,4 +45,6 @@ dependencies { testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) + + implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.6") } \ No newline at end of file From 6980adaeb6dc6a6956bee8aac3bc740ae99c77de Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 00:04:04 +0900 Subject: [PATCH 13/19] remove: file delete --- .../androidstudy1/viewModel/MemoViewModel.kt | 28 ------------------- 1 file changed, 28 deletions(-) delete mode 100644 app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt diff --git a/app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt b/app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt deleted file mode 100644 index 5d55ec1..0000000 --- a/app/src/main/java/com/alom/androidstudy1/viewModel/MemoViewModel.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.alom.androidstudy1.viewModel - -import android.content.Context -import android.content.Context.MODE_PRIVATE -import android.content.SharedPreferences -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.ViewModel - -class MemoViewModel: ViewModel() { - - private val _currentValue = MutableLiveData() - val currentValue : LiveData - get() = _currentValue - - - - fun saveMemo(context: Context, memo: String) { - _currentValue.value = memo - val sharedPreferences = context.getSharedPreferences("memo", MODE_PRIVATE) - val editor = sharedPreferences.edit() - editor.putString("memo", memo) - editor.apply() - - - } - -} \ No newline at end of file From 32d0d2f4f7b4444374937ae357f4d3a0acd7e79f Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 16:20:47 +0900 Subject: [PATCH 14/19] setting: Add LifeCycleScope implementation --- app/build.gradle.kts | 4 +++- gradle/libs.versions.toml | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 61879cb..26c4f89 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -46,5 +46,7 @@ dependencies { androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.espresso.core) - implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.6") + implementation(libs.androidx.lifecycle.viewmodel.ktx) + implementation(libs.androidx.lifecycle.runtime.ktx) + } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e0e8c2c..b4d3171 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -6,12 +6,15 @@ junit = "4.13.2" junitVersion = "1.2.1" espressoCore = "3.6.1" appcompat = "1.7.0" +lifecycleViewmodelKtx = "2.8.6" material = "1.12.0" activity = "1.9.2" constraintlayout = "2.1.4" [libraries] androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" } +androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version.ref = "lifecycleViewmodelKtx" } +androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" } junit = { group = "junit", name = "junit", version.ref = "junit" } androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" } androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" } From 098510c29be18d3f8cbed21048418ea14f9ee45e Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 16:23:33 +0900 Subject: [PATCH 15/19] feat: Define repository interface --- app/src/main/java/com/alom/androidstudy1/MemoRepository.kt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 app/src/main/java/com/alom/androidstudy1/MemoRepository.kt diff --git a/app/src/main/java/com/alom/androidstudy1/MemoRepository.kt b/app/src/main/java/com/alom/androidstudy1/MemoRepository.kt new file mode 100644 index 0000000..346495c --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/MemoRepository.kt @@ -0,0 +1,7 @@ +package com.alom.androidstudy1 + +interface MemoRepository { + //메모를 가져오는 추상함수와 메모를 설정하는 추상함수 설정 + fun getMemo():String + fun setMemo(input:String):Unit +} From c4608507d14647d662c7fd316ea00c8b7ac08cf0 Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 16:24:56 +0900 Subject: [PATCH 16/19] feat: Implement repository pattern --- .../com/alom/androidstudy1/MemoRepositoryImpl.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 app/src/main/java/com/alom/androidstudy1/MemoRepositoryImpl.kt diff --git a/app/src/main/java/com/alom/androidstudy1/MemoRepositoryImpl.kt b/app/src/main/java/com/alom/androidstudy1/MemoRepositoryImpl.kt new file mode 100644 index 0000000..50f6f83 --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/MemoRepositoryImpl.kt @@ -0,0 +1,14 @@ +package com.alom.androidstudy1 + +import android.content.SharedPreferences + +class MemoRepositoryImpl(private val sharedPreferences: SharedPreferences) : MemoRepository { + override fun getMemo(): String { + return sharedPreferences.getString("memo", "").toString() + } + override fun setMemo(input: String) { + val editor = sharedPreferences.edit() + editor.putString("memo", input) + editor.apply() + } +} \ No newline at end of file From bda98cddab853239301258e2253b8894b0ad17dd Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 16:30:40 +0900 Subject: [PATCH 17/19] refactor: Apply repository pattern, Add init block to ViewModel --- .../main/java/com/alom/androidstudy1/MemoViewModel.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt b/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt index d8d72e2..c41b712 100644 --- a/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt +++ b/app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt @@ -5,15 +5,22 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch -class MemoViewModel: ViewModel() { +class MemoViewModel(private val repository: MemoRepository) : ViewModel() { private val _currentValue = MutableStateFlow("") - val currentValue : StateFlow + val currentValue: StateFlow get() = _currentValue + init { //초기값 설정 + viewModelScope.launch { + _currentValue.emit(repository.getMemo()) + } + } + fun saveMemo(memo: String) { viewModelScope.launch { _currentValue.emit(memo) + repository.setMemo(memo) } } } \ No newline at end of file From 4253215097c6f2f1e8268359f637ea566d6176c5 Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 16:31:53 +0900 Subject: [PATCH 18/19] feat: Add ViewModelFactory --- .../alom/androidstudy1/MemoViewModelFactory.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 app/src/main/java/com/alom/androidstudy1/MemoViewModelFactory.kt diff --git a/app/src/main/java/com/alom/androidstudy1/MemoViewModelFactory.kt b/app/src/main/java/com/alom/androidstudy1/MemoViewModelFactory.kt new file mode 100644 index 0000000..cbecd91 --- /dev/null +++ b/app/src/main/java/com/alom/androidstudy1/MemoViewModelFactory.kt @@ -0,0 +1,15 @@ +package com.alom.androidstudy1 + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.ViewModelProvider + + +class MemoViewModelFactory(private val memoRepository: MemoRepository) : ViewModelProvider.Factory { + //ViewModel에 생성자가 있을 경우 factory를 만들어야함 + override fun create(modelClass: Class): T { + if(modelClass.isAssignableFrom(MemoViewModel::class.java)){ + return MemoViewModel(memoRepository) as T + } + throw IllegalArgumentException("Unknown ViewModel Class") + } +} \ No newline at end of file From 05b4812e7d44cbb1f4428e060f55a80261089cb8 Mon Sep 17 00:00:00 2001 From: MisterJerry Date: Fri, 11 Oct 2024 16:35:35 +0900 Subject: [PATCH 19/19] =?UTF-8?q?refactor:=20Refactor=20business=20logic?= =?UTF-8?q?=20(=EA=B8=B0=EC=A1=B4=20MainActivity=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=97=90=EC=84=9C=20RepositoryImpl=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alom/androidstudy1/MainActivity.kt | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt index 1b8cfca..53c0dbc 100644 --- a/app/src/main/java/com/alom/androidstudy1/MainActivity.kt +++ b/app/src/main/java/com/alom/androidstudy1/MainActivity.kt @@ -19,23 +19,18 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) - - memoViewModel = ViewModelProvider(this).get(MemoViewModel::class.java) - val sharedPreferences = getSharedPreferences("memo", MODE_PRIVATE) - val editor = sharedPreferences.edit() - Log.d(TAG, sharedPreferences.getString("memo","").toString()) - val initData = sharedPreferences.getString("memo","").toString() + val memoRepository = MemoRepositoryImpl(sharedPreferences) + + val memoViewModelFactory = MemoViewModelFactory(memoRepository) - binding.etMemo.setText(initData) - Log.d(TAG+"textview", binding.etMemo.text.toString()) + memoViewModel = ViewModelProvider(this,memoViewModelFactory).get(MemoViewModel::class.java) lifecycleScope.launch{ lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED){ launch { memoViewModel.currentValue.collect{ - editor.putString("memo",it) - editor.apply() + binding.etMemo.setText(it) } } }