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.

11 changes: 11 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,7 +42,13 @@ 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.glide)
kapt(libs.glide.compiler)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
android:theme="@style/Theme.BCSD_Android_20251"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:name=".app.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".app.AddEditWordActivity" />
</application>

</manifest>
14 changes: 0 additions & 14 deletions app/src/main/java/com/example/bcsd_android_2025_1/MainActivity.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.example.bcsd_android_2025_1.app
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.example.bcsd_android_2025_1.R
import com.example.bcsd_android_2025_1.domain.Word
import com.example.bcsd_android_2025_1.databinding.ActivityAddEditWordBinding
import com.bumptech.glide.Glide

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

private var selectedImageUri: Uri? = null

private val selectImageLauncher = registerForActivityResult(
ActivityResultContracts.GetContent()
) { uri: Uri? ->
if (uri != null) {
selectedImageUri = uri
Glide.with(this)
.load(uri)
.into(binding.ivPreview)
}
}

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)

if (!existingWord.imageUri.isNullOrEmpty()) {
selectedImageUri = Uri.parse(existingWord.imageUri)
Glide.with(this)
.load(selectedImageUri)
.into(binding.ivPreview)
}
}

binding.btnImage.setOnClickListener {
selectImageLauncher.launch("image/*")
}

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,
imageUri = selectedImageUri?.toString()
)
setResult(RESULT_OK, Intent().putExtra("result", result))
finish()
} else {
Toast.makeText(this, R.string.toast_msg, Toast.LENGTH_SHORT).show()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.bcsd_android_2025_1.app

import android.content.Intent
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.bcsd_android_2025_1.domain.Word
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)
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.bcsd_android_2025_1.app

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.domain.Word
import com.example.bcsd_android_2025_1.databinding.ItemWordBinding
import android.net.Uri
import android.view.View
import com.bumptech.glide.Glide

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

if (!word.imageUri.isNullOrEmpty()) {
binding.ivWordImage.visibility = View.VISIBLE
Glide.with(binding.root.context)
.load(Uri.parse(word.imageUri))
.centerCrop()
.into(binding.ivWordImage)
} else {
binding.ivWordImage.visibility = View.GONE
}

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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.example.bcsd_android_2025_1.app

import android.app.Application
import androidx.lifecycle.*
import com.example.bcsd_android_2025_1.domain.Word
import com.example.bcsd_android_2025_1.data.WordDatabase
import com.example.bcsd_android_2025_1.data.WordRepository
import kotlinx.coroutines.launch

class WordViewModel(application: Application) : AndroidViewModel(application) {
private val repository: WordRepository
val allWords: LiveData<List<Word>>
private val _selectedWord = MutableLiveData<Word?>()
val selectedWord: LiveData<Word?> get() = _selectedWord

init {
val dao = WordDatabase.getDatabase(application).wordDao()
repository = WordRepository(dao)
allWords = repository.allWords
}
fun insert(word: Word) = viewModelScope.launch { repository.insert(word) }
fun update(word: Word) = viewModelScope.launch { repository.update(word) }
fun delete(word: Word) = viewModelScope.launch { repository.delete(word) }
fun selectWord(word: Word) {
_selectedWord.value = word
}
}
Loading