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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
# android_study_14_1
아롬 안드로이드 14기 스터디 1번

# ToDo
|내용|완료 여부|
|---|:---:|
|MVVM 학습하기|✅|
|ㄴMVVM 코드 구현|✅|
|StateFlow 학습하기|✅|
|ㄴStateFlow 코드 구현|✅|
|SharedPreferences 이용한 저장 기능 구현|✅|
|Repository 학습하기||


# 과제 설명
### 간단하게 메모하고 저장, 불러오는 앱을 만들어봅시다.

단) mvvm + stateflow를 이용해서!
Expand Down
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ android {
namespace = "com.alom.androidstudy1"
compileSdk = 34

viewBinding.isEnabled = true

defaultConfig {
applicationId = "com.alom.androidstudy1"
minSdk = 26
Expand Down Expand Up @@ -42,4 +44,5 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation(libs.androidx.lifecycle.runtime.ktx)
}
47 changes: 45 additions & 2 deletions app/src/main/java/com/alom/androidstudy1/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
package com.alom.androidstudy1

import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Toast
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 com.alom.androidstudy1.databinding.ActivityMainBinding
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(),OnClickListener {
private lateinit var memoViewModel : MemoViewModel
private lateinit var binding:ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)

//뷰바인딩 적용
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

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
}

//ViewModel
memoViewModel = ViewModelProvider(this).get(MemoViewModel::class.java)

//버튼 리스너 설정
binding.button.setOnClickListener(this)

lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
launch {
memoViewModel.currentText.collect{
//EditText Text를 SharedPreferences에서 불러온 글자로 변경
binding.editTextTextMultiLine.setText(it)
}
}
}
}
}

override fun onResume() {
super.onResume()

memoViewModel.returnMemo(applicationContext)
}

override fun onClick(view: View?) {
//SharedPreference 저장 함수 호출
memoViewModel.saveMemo(binding.editTextTextMultiLine.text.toString(),applicationContext)
}
}
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.content.Context
import android.content.SharedPreferences
import android.widget.Toast
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

class MemoViewModel : ViewModel(){
val _currentText = MutableStateFlow<String>("")
val currentText:StateFlow<String> = _currentText

fun returnMemo(context: Context){
val pref:SharedPreferences = context.getSharedPreferences("pref",Context.MODE_PRIVATE)
_currentText.value = pref.getString("content","")!!
}

fun saveMemo(content:String,context: Context){
_currentText.value = content

val pref:SharedPreferences = context.getSharedPreferences("pref",Context.MODE_PRIVATE)
pref.edit().putString("content",content).apply()

Toast.makeText(context,"메모가 저장되었습니다",Toast.LENGTH_LONG).show()
}
}
39 changes: 31 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,36 @@
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
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" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:text="메모장" />

<EditText
android:id="@+id/editTextTextMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_weight="1"
android:ems="10"
android:gravity="center|start"
android:inputType="textMultiLine"
android:text="edit text save" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:text="저장" />
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
lifecycleRuntimeKtx = "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 = "lifecycleRuntimeKtx" }
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" }
Expand Down