Skip to content
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

정렬 기능 추가 #59

Open
wants to merge 3 commits into
base: CJE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion app/src/main/java/com/gdsc/todo/model/db/ToDoDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.gdsc.todo.model.dao.ToDoDao
// 반드시 RoomDatabase를 상속받은 추상클래스를 만들어야 한다.
// 추상 클래스로 만든 인스턴스는 주로 싱글톤으로 만든다.
// 같은 시간에 여러 개의 인스턴스에서 데이터베이스에 접근하는 것을 막기 위함이다.
@Database(entities = [MyToDoList::class], version = 6)
@Database(entities = [MyToDoList::class], version = 9)
Copy link
Member

Choose a reason for hiding this comment

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

앱을 재실행하지 못하고 재설치를 해야되는 에러가 생기지는 않았나요?

Copy link
Member

Choose a reason for hiding this comment

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

룸 마이그레이션
SQL문을 거쳐서 마이그레이션하는 방식도 있습니다.

abstract class ToDoDatabase: RoomDatabase() {
abstract fun getToDoDao(): ToDoDao

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/gdsc/todo/model/entity/MyToDoList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ import androidx.room.PrimaryKey
data class MyToDoList(
@PrimaryKey(autoGenerate = true) val id: Long = 0,
@ColumnInfo(name="title") val title: String,
@ColumnInfo(name="content") val content: String
@ColumnInfo(name="content") val content: String,
@ColumnInfo(name="date") val date: String
)
66 changes: 64 additions & 2 deletions app/src/main/java/com/gdsc/todo/ui/ToDo/ToDoActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,41 @@ package com.gdsc.todo.ui.ToDo
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.widget.Toast
import android.widget.Toolbar
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import com.gdsc.todo.R
import com.gdsc.todo.ui.ToDoViewModel
import com.gdsc.todo.databinding.ActivityToDoBinding
import com.gdsc.todo.model.db.ToDoDatabase
import com.gdsc.todo.model.entity.MyToDoList
import com.gdsc.todo.ui.AddToDo.AddToDoActivity

const val TAG2 = "ToDoActivity"
const val ISSORTED = "isSorted"

class ToDoActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var toDoAdapter: ToDoAdapter
private lateinit var viewModel: ToDoViewModel
private lateinit var toolbar: androidx.appcompat.widget.Toolbar
Copy link
Member

Choose a reason for hiding this comment

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

이건 취향 차이일 수 있겠는데, toolbar를 굳이 변수로 둬야하나 싶네요.

private var titleIsSorted: Boolean = false
private var dateIsSorted: Boolean = false

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

recyclerView = binding.todoRecyclerView
toolbar = binding.todoToolbar
setSupportActionBar(toolbar)

viewModel = ViewModelProvider(this , ViewModelProvider.AndroidViewModelFactory(getApplication())).get(
ToDoViewModel::class.java)
setRecyclerView()
Expand All @@ -34,7 +48,10 @@ class ToDoActivity : AppCompatActivity() {
}

fun setRecyclerView() {
toDoAdapter = ToDoAdapter(viewModel.myToDoSet, viewModel)
when(titleIsSorted || dateIsSorted){
true -> toDoAdapter = ToDoAdapter(viewModel.sortMyToDoSet, viewModel)
false -> toDoAdapter = ToDoAdapter(viewModel.myToDoSet, viewModel)
}
Copy link
Member

Choose a reason for hiding this comment

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

여기서 true로 가는 경우가 있나요?

toDoAdapter.notifyDataSetChanged()
recyclerView.apply {
adapter = toDoAdapter
Expand All @@ -48,8 +65,8 @@ class ToDoActivity : AppCompatActivity() {

override fun onRestart() {
super.onRestart()
viewModel.getAll()
setRecyclerView()

}

override fun onDestroy() {
Expand All @@ -66,4 +83,49 @@ class ToDoActivity : AppCompatActivity() {
private fun checkEvent(toDo: MyToDoList){
viewModel.deleteToDo(toDo)
}

// toolbar에 menu.xml 추가
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu, menu)
return true
}

// toolbar에 추가된 항목 클릭 시 이벤트 처리
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.sort_title -> {
when(titleIsSorted) {
true -> {
Log.d(ISSORTED, titleIsSorted.toString())
titleIsSorted = false
setRecyclerView()
}
false -> {
Log.d(ISSORTED, titleIsSorted.toString())
viewModel.sortTitle()
titleIsSorted = true
dateIsSorted = false
setRecyclerView()
}
}
return true
}
R.id.sort_date -> {
when(dateIsSorted){
true -> {
dateIsSorted = false
setRecyclerView()
}
false -> {
viewModel.sortDate()
dateIsSorted = true
titleIsSorted = false
setRecyclerView()
}
}
return true
}
}
return super.onOptionsItemSelected(item)
}
Copy link
Member

Choose a reason for hiding this comment

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

여기도 세분화하면 좋을 것 같아요.

when(item.itemId) {
    R.id.sort_title -> abcd(titleIsSorted)
    R.id.sort_date -> qwer(dateIsSorted)
}

처럼 표현하면 가독성이 좋아지지 않을까요?

}
18 changes: 16 additions & 2 deletions app/src/main/java/com/gdsc/todo/ui/ToDoViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import com.gdsc.todo.model.entity.MyToDoList
class ToDoViewModel(application: Application) : AndroidViewModel(application) {

private var _myToDoSet = ArrayList<MyToDoList>()
private var _sortMyToDoSet = ArrayList<MyToDoList>()

val myToDoSet: List<MyToDoList>
get() = _myToDoSet
val sortMyToDoSet: List<MyToDoList>
get() = _sortMyToDoSet
var title = ""
var content = ""
var date = ""

private val repository: ToDoRepository by lazy{
ToDoRepository(application)
Expand All @@ -23,6 +27,8 @@ class ToDoViewModel(application: Application) : AndroidViewModel(application) {
init {
Log.d(TAG, "init")
getAll()
// _sortMyToDoSet = _myToDoSet 얕은 복사(기존 객체에 영향 O)
_sortMyToDoSet.addAll(_myToDoSet) // 깊은 복사(기존 객체에 영향 X)
Comment on lines +30 to +31
Copy link
Member

Choose a reason for hiding this comment

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

삽질하기 좋은 개념인데 잘 이해가 되셨길ㅎㅎ

Copy link
Member

Choose a reason for hiding this comment

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

Cloneable

}

fun getAll(){
Expand All @@ -34,7 +40,7 @@ class ToDoViewModel(application: Application) : AndroidViewModel(application) {

fun addButtonClick(){
Thread{
val newToDo = MyToDoList(title = title.toString(), content = content.toString())
val newToDo = MyToDoList(title = title, content = content, date = "~" + date)
repository.insert(newToDo)
}.start()
Thread.sleep(TIME) // 있어야 제목이 저장됨(왜?)
Expand All @@ -48,8 +54,16 @@ class ToDoViewModel(application: Application) : AndroidViewModel(application) {

fun checkEmpty() = title!="" && content!=""

fun sortTitle(){
_sortMyToDoSet.sortBy{it.title}
}

fun sortDate(){
_sortMyToDoSet.sortBy{it.date}
}

companion object{
const val TAG = "ToDoViewModel"
const val TIME: Long = 1000
const val TIME: Long = 500
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/sort.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M3,18h6v-2L3,16v2zM3,6v2h18L21,6L3,6zM3,13h12v-2L3,11v2z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/sort_date.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M9,11L7,11v2h2v-2zM13,11h-2v2h2v-2zM17,11h-2v2h2v-2zM19,4h-1L18,2h-2v2L8,4L8,2L6,2v2L5,4c-1.11,0 -1.99,0.9 -1.99,2L3,20c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM19,20L5,20L5,9h14v11z"/>
</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/sort_title.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M21,11h-1.5v-0.5h-2v3h2V13H21v1c0,0.55 -0.45,1 -1,1h-3c-0.55,0 -1,-0.45 -1,-1v-4c0,-0.55 0.45,-1 1,-1h3c0.55,0 1,0.45 1,1V11zM8,10v5H6.5v-1.5h-2V15H3v-5c0,-0.55 0.45,-1 1,-1h3C7.55,9 8,9.45 8,10zM6.5,10.5h-2V12h2V10.5zM13.5,12c0.55,0 1,0.45 1,1v1c0,0.55 -0.45,1 -1,1h-4V9h4c0.55,0 1,0.45 1,1v1C14.5,11.55 14.05,12 13.5,12zM11,10.5v0.75h2V10.5H11zM13,12.75h-2v0.75h2V12.75z"/>
</vector>
15 changes: 14 additions & 1 deletion app/src/main/res/layout/activity_add_to_do.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,23 @@
android:text="@={addToDoViewModel.title}"/>

<EditText
android:id="@+id/addTodo_content"
android:id="@+id/addTodo_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/addTodo_title"
android:layout_marginStart="13dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="13dp"
android:layout_marginBottom="13dp"
android:background="@android:color/transparent"
android:hint="Deadline(mmdd)"
android:text="@={addToDoViewModel.date}" />

<EditText
android:id="@+id/addTodo_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/addTodo_date"
android:layout_marginLeft="13dp"
android:layout_marginRight="13dp"
android:background="@android:color/transparent"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/layout/item_todo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
android:textSize="13dp"
android:text="@{item.content}"/>

<TextView
android:id="@+id/date"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@{item.date}"
android:textColor="#C67C5252"
Copy link
Member

Choose a reason for hiding this comment

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

프로젝트 규모가 커진다면 color도 extract해야 수정할 때 번거롭지 않습니다. 또한 색상을 잘못고르는 실수가 없죠
하지만 extract하는 과정 자체가 번거롭기 때문에 조삼모사 같다는 생각도 해요.
리소스 파일에 대한 관리도 가볍게 고려해보셔도 좋을 것 같습니다.

저는 아래 링크 참고해서 프로젝트 진행했었어요~
헤이딜러 컨벤션

android:textStyle="bold"
android:textSize="13dp" />

</LinearLayout>

</LinearLayout>
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/res/menu/menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/sort_title"
android:icon="@drawable/sort"
app:showAsAction="always" />

<item
android:id="@+id/sort_date"
android:icon="@drawable/sort_date"
app:showAsAction="always" />

</menu>