-
Notifications
You must be signed in to change notification settings - Fork 1
[정현수_Android] 9-1주차 과제 제출 #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
96cbb52
364800c
e4994fa
eb5e1e2
5b7f4b8
525861d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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) | ||
| } | ||
| } |
| 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") | ||
|
|
||
| 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" | ||
| } | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 추후 clean architecture 설계를 할 때는 domain, data 계층을 분리하시면 더 좋을 것 같습니다(현재 과제에서는 선택사항이라 문제 없습니다). |
| 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 | ||
| } | ||
| } |
| 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 | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
여기 있는 키들도 companion object에 상수로 선언하는 것이 좋아보입니다.