-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: CJE
Are you sure you want to change the base?
정렬 기능 추가 #59
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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. 이건 취향 차이일 수 있겠는데, 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() | ||
|
@@ -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) | ||
} | ||
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. 여기서 true로 가는 경우가 있나요? |
||
toDoAdapter.notifyDataSetChanged() | ||
recyclerView.apply { | ||
adapter = toDoAdapter | ||
|
@@ -48,8 +65,8 @@ class ToDoActivity : AppCompatActivity() { | |
|
||
override fun onRestart() { | ||
super.onRestart() | ||
viewModel.getAll() | ||
setRecyclerView() | ||
|
||
} | ||
|
||
override fun onDestroy() { | ||
|
@@ -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) | ||
} | ||
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. 여기도 세분화하면 좋을 것 같아요. when(item.itemId) {
R.id.sort_title -> abcd(titleIsSorted)
R.id.sort_date -> qwer(dateIsSorted)
} 처럼 표현하면 가독성이 좋아지지 않을까요? |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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
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. 삽질하기 좋은 개념인데 잘 이해가 되셨길ㅎㅎ 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. Cloneable |
||
} | ||
|
||
fun getAll(){ | ||
|
@@ -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) // 있어야 제목이 저장됨(왜?) | ||
|
@@ -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 | ||
} | ||
} |
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> |
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> |
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> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
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. 프로젝트 규모가 커진다면 color도 extract해야 수정할 때 번거롭지 않습니다. 또한 색상을 잘못고르는 실수가 없죠 저는 아래 링크 참고해서 프로젝트 진행했었어요~ |
||
android:textStyle="bold" | ||
android:textSize="13dp" /> | ||
|
||
</LinearLayout> | ||
|
||
</LinearLayout> | ||
|
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> |
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.
앱을 재실행하지 못하고 재설치를 해야되는 에러가 생기지는 않았나요?
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.
룸 마이그레이션
SQL문을 거쳐서 마이그레이션하는 방식도 있습니다.