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.

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

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.

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

9 changes: 9 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("org.jetbrains.kotlin.kapt")
}

android {
Expand All @@ -16,6 +17,10 @@ android {

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures {
dataBinding = true
viewBinding = true
}

buildTypes {
release {
Expand All @@ -37,6 +42,10 @@ android {

dependencies {

implementation(libs.androidx.room.runtime)
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.0")
implementation(libs.androidx.room.ktx)
kapt(libs.androidx.room.compiler)
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AddEditWordActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.bcsd_android_2025_1
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.bcsd_android_2025_1.databinding.ActivityAddEditWordBinding

class AddEditWordActivity : AppCompatActivity() {
private lateinit var binding: ActivityAddEditWordBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAddEditWordBinding.inflate(layoutInflater)
setContentView(binding.root)

val existingWord = intent.getSerializableExtra("word") as? Word

if (existingWord != null) {
binding.etWord.setText(existingWord.word)
binding.etMeaning.setText(existingWord.meaning)
}

binding.btnSave.setOnClickListener {
val word = binding.etWord.text.toString()
val meaning = binding.etMeaning.text.toString()
if (word.isNotBlank() && meaning.isNotBlank()) {
val result = Word(
id = existingWord?.id ?: 0,
word = word,
meaning = meaning
)
setResult(RESULT_OK, Intent().putExtra("result", result))
finish()
} else {
Toast.makeText(this, R.string.toast_msg, Toast.LENGTH_SHORT).show()
}
}
}
}
50 changes: 46 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,56 @@
package com.example.bcsd_android_2025_1

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

class MainActivity : AppCompatActivity() {
private lateinit var viewModel: WordViewModel
private lateinit var adapter: WordAdapter

private val launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val word = result.data?.getSerializableExtra("result") as? Word
word?.let {
if (it.id == 0) viewModel.insert(it) else viewModel.update(it)
}
}
}

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

viewModel = ViewModelProvider(this)[WordViewModel::class.java]
binding.viewModel = viewModel
binding.lifecycleOwner = this

adapter = WordAdapter(
onItemClick = { word -> viewModel.selectWord(word) },
onEditClick = { word ->
val intent = Intent(this, AddEditWordActivity::class.java)
intent.putExtra("word", word)
launcher.launch(intent)
},
onDeleteClick = { word -> viewModel.delete(word) }
)

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

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

binding.fab.setOnClickListener {
val intent = Intent(this, AddEditWordActivity::class.java)
launcher.launch(intent)
}
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/example/bcsd_android_2025_1/Word.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.bcsd_android_2025_1

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.io.Serializable

@Entity(tableName = "word_table")

data class Word(
@PrimaryKey(autoGenerate = true) val id : Int = 0,
val word: String,
val meaning: String
) : Serializable
41 changes: 41 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,41 @@
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 onItemClick: (Word) -> Unit,
private val onEditClick: (Word) -> Unit,
private val onDeleteClick: (Word) -> Unit
) : ListAdapter<Word, WordAdapter.WordViewHolder>(DiffCallback()){

inner class WordViewHolder(private val binding: ItemWordBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(word: Word) {
binding.word = word
binding.root.setOnClickListener { onItemClick(word) }
binding.btnEdit.setOnClickListener { onEditClick(word) }
binding.btnDelete.setOnClickListener { onDeleteClick(word) }
binding.executePendingBindings()
}
}

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

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

class DiffCallback : DiffUtil.ItemCallback<Word>() {
override fun areItemsTheSame(oldItem: Word, newItem: Word) = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Word, newItem: Word) = oldItem == newItem
}
}
19 changes: 19 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,19 @@
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<Word>>

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

@Update
suspend fun update(word: Word)

@Delete
suspend fun delete(word: Word)
}
28 changes: 28 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,28 @@
package com.example.bcsd_android_2025_1

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

@Database(entities = [Word::class], version = 1)
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
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.bcsd_android_2025_1

import androidx.lifecycle.LiveData

class WordRepository(private val dao: WordDao) {
val allWords : LiveData<List<Word>> = dao.getAllWords()

suspend fun insert(word: Word) = dao.insert(word)
suspend fun update(word: Word) = dao.update(word)
suspend fun delete(word: Word) = dao.delete(word)
}
Loading