Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ android {
namespace = "com.alom.androidstudy1"
compileSdk = 34

buildFeatures{
viewBinding = true
}

defaultConfig {
applicationId = "com.alom.androidstudy1"
minSdk = 26
Expand Down
26 changes: 20 additions & 6 deletions app/src/main/java/com/alom/androidstudy1/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.alom.androidstudy1.databinding.ActivityMainBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var memoViewModel: MemoViewModel
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)

memoViewModel = ViewModelProvider(this)[MemoViewModel::class.java]

binding.btnSave.setOnClickListener{
memoViewModel.updateMemo(binding.etText.text.toString())
}

lifecycleScope.launch {
memoViewModel.currentValue.collect{
binding.etText.setText(it)
}
}
}
}
27 changes: 27 additions & 0 deletions app/src/main/java/com/alom/androidstudy1/MemoViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.alom.androidstudy1

import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

class MemoViewModel(application: Application) : AndroidViewModel(application) {

private val preferenceUtil = PreferenceUtil(getApplication<Application>().applicationContext)
private val _currentValue = MutableStateFlow<String>("")

val currentValue: StateFlow<String> = _currentValue

init{
_currentValue.value = preferenceUtil.getString("memo", "")
}

fun updateMemo(newMemo: String) {
_currentValue.value = newMemo
preferenceUtil.setString("memo", newMemo)
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/alom/androidstudy1/PreferenceUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.alom.androidstudy1

import android.content.Context
import android.content.SharedPreferences

class PreferenceUtil(context: Context) {
private val prefs: SharedPreferences =
context.getSharedPreferences("MemoApplication", Context.MODE_PRIVATE)

fun getString(key: String, defValue: String): String {
return prefs.getString(key, defValue).toString()
}

fun setString(key: String, str: String) {
prefs.edit().putString(key, str).apply()
}
}
20 changes: 13 additions & 7 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
<EditText
android:id="@+id/etText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnSave"
tools:text="22"/>

<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Sep 25 21:41:25 KST 2024
#Mon Sep 30 10:06:39 KST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists