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
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.

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.

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

7 changes: 7 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.

14 changes: 14 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
id("kotlin-kapt")
}

android {
Expand All @@ -17,6 +18,14 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
dataBinding = true
}

kapt {
correctErrorTypes = true
}

buildTypes {
release {
isMinifyEnabled = false
Expand All @@ -42,6 +51,11 @@ dependencies {
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
implementation(libs.androidx.lifecycle.viewmodel.ktx)
implementation(libs.kotlinx.coroutines.android)
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx)
kapt(libs.androidx.room.compiler)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
android:supportsRtl="true"
android:theme="@style/Theme.BCSD_Android_20251"
tools:targetApi="31">
<activity
android:name=".AddEditActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.bcsd_android_2025_1

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.example.bcsd_android_2025_1.databinding.ActivityAddEditBinding


class AddEditActivity : AppCompatActivity(){
private lateinit var binding: ActivityAddEditBinding
private val viewModel:WordViewModel by viewModels()

private var wordId: Int?= null

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
binding = ActivityAddEditBinding.inflate(layoutInflater)
setContentView(binding.root)

val editWordText = intent.getStringExtra("word_text")
Copy link

Choose a reason for hiding this comment

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

companion object 로 관리하면 좋을 거 같습니다.
다른 intent key 값들도 마찬가지입니다.

val editMeaning = intent.getStringExtra("word_meaning")
wordId = intent.getIntExtra("word_id", -1).takeIf { it != -1 }

binding.wordEdittext.setText(editWordText)
binding.meaningEdittext.setText(editMeaning)

binding.addButton.setOnClickListener {
val wordText = binding.wordEdittext.text.toString()
val meaning = binding.meaningEdittext.text.toString()
if (wordText.isNotBlank() && meaning.isNotBlank()) {
val word = WordListData(wordId ?: 0, wordText, meaning)
if (wordId != null) {
viewModel.update(word)
binding.wordEdittext.setText("")
binding.meaningEdittext.setText("")
val resultIntent = Intent()
resultIntent.putExtra("edited_word", word.word)
resultIntent.putExtra("edited_meaning", word.meaning)
setResult(Activity.RESULT_OK, resultIntent)
finish()

} else {
viewModel.insert(word)
}
finish()
}
}
}
}
72 changes: 68 additions & 4 deletions app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,78 @@
package com.example.bcsd_android_2025_1

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.ActivityResultLauncher
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.recyclerview.widget.LinearLayoutManager
import com.example.bcsd_android_2025_1.databinding.ActivityMainBinding


class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private val viewModel: WordViewModel by viewModels()
private lateinit var editActivityLauncher: ActivityResultLauncher<Intent>


private val adapter by lazy {
WordAdapter(onTopClick = { viewModel.setTopWord(it) })
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

editActivityLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data = result.data
val editedWord = data?.getStringExtra("edited_word") ?: ""
val editedMeaning = data?.getStringExtra("edited_meaning") ?: ""

// 상단 TextView 업데이트
binding.wordTextview.text = editedWord
binding.meaningTextview.text = editedMeaning
}
}

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

binding.editButton.setOnClickListener {
viewModel.topWord.value?.let {
val intent = Intent(this, AddEditActivity::class.java).apply {
putExtra("word_id", it.id)
putExtra("word_text", it.word)
putExtra("word_meaning", it.meaning)
}
editActivityLauncher.launch(intent)
}
}

binding.deleteButton.setOnClickListener {
viewModel.topWord.value?.let {
viewModel.delete(it)
binding.wordTextview.text = ""
binding.meaningTextview.text = ""
}
}

viewModel.allWords.observe(this) {
adapter.submitList(it)
}

viewModel.topWord.observe(this) {
binding.wordTextview.text = it?.word ?: "단어 선택 필요"
Copy link

Choose a reason for hiding this comment

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

strings.xml

binding.meaningTextview.text = it?.meaning ?: ""
}

binding.floatingButton.setOnClickListener {
startActivity(Intent(this, AddEditActivity::class.java))
}
}
}
35 changes: 35 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/WordAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.bcsd_android_2025_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.bcsd_android_2025_1.databinding.ItemWordBinding

class WordAdapter (
private val onTopClick: (WordListData) ->Unit
) : ListAdapter<WordListData, WordAdapter.WordViewHolder>(DiffCallback()){

inner class WordViewHolder(private val binding: ItemWordBinding): RecyclerView.ViewHolder(binding.root) {
fun bind(word: WordListData) {
binding.word = word
binding.root.setOnClickListener { onTopClick(word) }
binding.executePendingBindings()
}
}

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

override fun onBindViewHolder(holder: WordViewHolder, position:Int){
holder.bind(getItem(position))
}

class DiffCallback: DiffUtil.ItemCallback<WordListData>(){
override fun areItemsTheSame(oldItem:WordListData, newItem:WordListData):Boolean = oldItem.id ==newItem.id
override fun areContentsTheSame(oldItem: WordListData, newItem: WordListData): Boolean = oldItem == newItem
}
}
20 changes: 20 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/WordDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.bcsd_android_2025_1

import androidx.lifecycle.LiveData
import androidx.room.*

@Dao
interface WordDao {
@Query("SELECT * FROM word_table ORDER BY id DESC")
fun getAllwords(): LiveData<List<WordListData>>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(word: WordListData)

@Update
suspend fun update(word: WordListData)

@Delete
suspend fun delete(wordListData: WordListData)

}
26 changes: 26 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/WordDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.bcsd_android_2025_1

import android.content.Context
import androidx.room.Room
import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [WordListData::class], version = 1, exportSchema = false)
abstract class WordDatabase : RoomDatabase() {
abstract fun wordDao(): WordDao

companion object{
@Volatile private var INSTANCE: WordDatabase?=null

fun getDatabase(context: Context): WordDatabase{
return INSTANCE ?: synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext, WordDatabase::class.java,
"word_database"
).build()
INSTANCE = instance
instance
}
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/WordListData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.bcsd_android_2025_1

import androidx.room.PrimaryKey
import androidx.room.Entity

@Entity(tableName = "word_table")
data class WordListData (
@PrimaryKey(autoGenerate = true)
val id: Int = 0,
var word: String,
var meaning: String
)
Loading