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
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/AndroidProjectSystem.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/deviceManager.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/markdown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
plugins {
alias(libs.plugins.android.application)
id("kotlin-parcelize")
alias(libs.plugins.android.legacy.kapt)
}

android {
namespace = "com.example.android_2026_1"
compileSdk = 36
compileSdk = 37

defaultConfig {
applicationId = "com.example.android_2026_1"
minSdk = 26
targetSdk = 36
targetSdk = 37
versionCode = 1
versionName = "1.0"

Expand All @@ -29,6 +31,10 @@ android {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

buildFeatures {
dataBinding = true
}
}

dependencies {
Expand All @@ -37,7 +43,9 @@ dependencies {
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.recyclerview)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation(libs.coil)
}
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WordEditActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>


Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/example/android_2026_1/BindingAdapters.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.android_2026_1

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.dispose
import coil.load

@BindingAdapter("imageUri")
fun ImageView.setImageUri(uri: String?) {
if (uri.isNullOrEmpty()) {
dispose()
setImageDrawable(null)
} else {
load(uri)
}
}
71 changes: 69 additions & 2 deletions app/src/main/java/com/example/android_2026_1/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,87 @@
package com.example.android_2026_1

import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.android_2026_1.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private val viewModel: MainViewModel by viewModels()
private lateinit var adapter: WordAdapter
private lateinit var binding: ActivityMainBinding

private val editLauncher = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { result ->
val data = result.data
if (data != null) {
val mode = data.getStringExtra("mode")
val word = data.getStringExtra("word").orEmpty()
val meaning = data.getStringExtra("meaning").orEmpty()
val uri = data.getStringExtra("imageUri")
Comment on lines +25 to +29

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기 있는 키들도 companion object에 상수로 선언하는 것이 좋아보입니다.


when (mode) {
MODE_ADD -> viewModel.addWord(word, meaning, uri)
MODE_EDIT -> {
val id = data.getIntExtra("id", -1)
viewModel.editWord(id, word, meaning, uri)
}
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.viewModel = viewModel
binding.lifecycleOwner = this

ViewCompat.setOnApplyWindowInsetsListener(binding.main) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

adapter = WordAdapter { clickedItem ->
viewModel.selectItem(clickedItem)
}

binding.recyclerView.layoutManager = LinearLayoutManager(this)
binding.recyclerView.adapter = adapter

binding.btnAdd.setOnClickListener {
val intent = Intent(this, WordEditActivity::class.java).apply {
putExtra("mode", MODE_ADD)
}
editLauncher.launch(intent)
}

binding.btnEdit.setOnClickListener {
viewModel.selectedWord.value?.let { item ->
val intent = Intent(this, WordEditActivity::class.java).apply {
putExtra("mode", MODE_EDIT)
putExtra("item", item)
}
editLauncher.launch(intent)
}
}

viewModel.wordList.observe(this) { newList ->
adapter.submitList(newList)
}
}

companion object {
const val MODE_ADD = "ADD"
const val MODE_EDIT = "EDIT"
}
}
59 changes: 59 additions & 0 deletions app/src/main/java/com/example/android_2026_1/MainViewModel.kt

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후 clean architecture 설계를 할 때는 domain, data 계층을 분리하시면 더 좋을 것 같습니다(현재 과제에서는 선택사항이라 문제 없습니다).
예를 들어 실제 데이터를 저장하는 기능을 구현한다면 직접적으로 로컬db나 api로 데이터를 조회하는 로직은 data 계층, 이것에 대한 추상 인터페이스를 domain 계층에 두고 viewmodel이 domain 계층에 의존성을 가지도록 구현 가능합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.example.android_2026_1

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class MainViewModel : ViewModel() {

private val _wordList = MutableLiveData<List<WordItem>>(emptyList())
val wordList: LiveData<List<WordItem>> = _wordList

private val _selectedWord = MutableLiveData<WordItem?>(null)
val selectedWord: LiveData<WordItem?> = _selectedWord

private var nextId = 0

private fun getCurrentList(): MutableList<WordItem> {
return _wordList.value.orEmpty().toMutableList()
}

fun addWord(word: String, meaning: String, uri: String?) {
val currentList = getCurrentList()
val newItem = WordItem(nextId++, word, meaning, uri)
currentList.add(newItem)
_wordList.value = currentList
}

fun editWord(id: Int, word: String, meaning: String, uri: String?) {
val currentList = getCurrentList()
val index = currentList.indexOfFirst { it.id == id }
if (index != -1) {
val updated = WordItem(id, word, meaning, uri)
currentList[index] = updated
_wordList.value = currentList
if (_selectedWord.value?.id == id) {
_selectedWord.value = updated
}
}
}

fun deleteWord() {
val item = _selectedWord.value ?: return
val currentList = getCurrentList()
val index = currentList.indexOfFirst { it.id == item.id }
if (index != -1) {
currentList.removeAt(index)
_wordList.value = currentList
clearSelection()
}
}

fun selectItem(item: WordItem) {
_selectedWord.value = item
}

fun clearSelection() {
_selectedWord.value = null
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/com/example/android_2026_1/WordAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.example.android_2026_1

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.android_2026_1.databinding.ItemWordBinding

class WordAdapter(
private val onItemClick: (WordItem) -> Unit
) : ListAdapter<WordItem, WordAdapter.ViewHolder>(WordDiffCallback) {

class ViewHolder(val binding: ItemWordBinding) : RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val binding = ItemWordBinding.inflate(layoutInflater, parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.binding.item = item
holder.binding.root.setOnClickListener { onItemClick(item) }
holder.binding.executePendingBindings()
}

companion object WordDiffCallback : DiffUtil.ItemCallback<WordItem>() {
override fun areItemsTheSame(oldItem: WordItem, newItem: WordItem): Boolean {
return oldItem.id == newItem.id
}

override fun areContentsTheSame(oldItem: WordItem, newItem: WordItem): Boolean {
return oldItem == newItem
}
}
}
Loading