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

[feat] ToDo 생성 서버통신 #39

Merged
merged 1 commit into from
Dec 4, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.howdroid.data.datasource.remote

import com.example.howdroid.data.model.request.RequestAddToDoDto
import com.example.howdroid.data.service.ToDoService
import javax.inject.Inject

Expand All @@ -8,4 +9,7 @@ class ToDoDataSource @Inject constructor(
) {
suspend fun checkToDo(toDoId: Long) =
toDoService.checkToDo(toDoId)

suspend fun addToDo(requestAddToDoDto: RequestAddToDoDto) =
toDoService.addToDo(requestAddToDoDto)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.howdroid.data.model.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class RequestAddToDoDto(
@SerialName("priority")
val priority: String,
@SerialName("selectedDate")
val selectedDate: String,
@SerialName("todo")
val todo: String,
@SerialName("todoCategoryId")
val todoCategoryId: Int,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.howdroid.data.repository

import com.example.howdroid.data.datasource.remote.ToDoDataSource
import com.example.howdroid.data.model.request.RequestAddToDoDto
import com.example.howdroid.domain.repository.ToDoRepository
import javax.inject.Inject

Expand All @@ -9,4 +10,7 @@ class ToDoRepositoryImpl @Inject constructor(
) : ToDoRepository {
override suspend fun checkToDo(toDoId: Long): Result<Unit> =
runCatching { toDoDataSource.checkToDo(toDoId) }

override suspend fun addToDo(requestAddToDoDto: RequestAddToDoDto): Result<Unit> =
runCatching { toDoDataSource.addToDo(requestAddToDoDto) }
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package com.example.howdroid.data.service

import com.example.howdroid.data.model.request.RequestAddToDoDto
import retrofit2.http.Body
import retrofit2.http.PATCH
import retrofit2.http.POST
import retrofit2.http.Path

interface ToDoService {
@PATCH("todo/check/{todoId}")
suspend fun checkToDo(
@Path("todoId") todoId: Long,
)

@POST("todo/assign")
suspend fun addToDo(
@Body requestAddToDoDto: RequestAddToDoDto,
)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.example.howdroid.domain.repository

import com.example.howdroid.data.model.request.RequestAddToDoDto

interface ToDoRepository {

suspend fun checkToDo(toDoId: Long): Result<Unit>

suspend fun addToDo(requestAddToDoDto: RequestAddToDoDto): Result<Unit>
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.example.howdroid.R
import com.example.howdroid.databinding.ActivityAddTodoBinding
import com.example.howdroid.presentation.home.HomeFragment.Companion.CATEGORY_ID
import com.example.howdroid.presentation.home.HomeFragment.Companion.SELECTED_DATE
import com.example.howdroid.presentation.type.PriorityType
import com.example.howdroid.util.UiState
import com.example.howdroid.util.binding.BindingActivity
import com.example.howdroid.util.extension.setOnSingleClickListener
import com.example.howdroid.util.extension.setVisible
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlin.properties.Delegates

@AndroidEntryPoint
class AddToDoActivity : BindingActivity<ActivityAddTodoBinding>(R.layout.activity_add_todo) {

private val addToDoViewModel: AddToDoViewModel by viewModels()

private val errorMessageTextView by lazy { binding.tvAddTodoErrorMessage }
lateinit var selectedDate: String
var categoryId by Delegates.notNull<Int>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -29,6 +36,20 @@ class AddToDoActivity : BindingActivity<ActivityAddTodoBinding>(R.layout.activit
setTextChangeListeners()
setPriorityType()
observePriorityType()
postAddToDo()
}

private fun postAddToDo() {
selectedDate = intent.extras?.getString(SELECTED_DATE).toString()
categoryId = intent.extras?.getInt(CATEGORY_ID) ?: 0

binding.btnAddTodoDone.setOnSingleClickListener {
addToDoViewModel.addTodo(
selectedDate,
binding.etAddTodo.text.toString(),
categoryId,
)
}
}

private fun observePriorityType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ package com.example.howdroid.presentation
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.howdroid.data.model.request.RequestAddToDoDto
import com.example.howdroid.domain.repository.ToDoRepository
import com.example.howdroid.presentation.signup.SignUpViewModel
import com.example.howdroid.presentation.type.PriorityType
import com.example.howdroid.util.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

class AddToDoViewModel : ViewModel() {
@HiltViewModel
class AddToDoViewModel @Inject constructor(
private val toDoRepository: ToDoRepository,
) : ViewModel() {
private val _priorityType = MutableStateFlow<PriorityType?>(null)
val priorityType get() = _priorityType.asStateFlow()

Expand All @@ -19,6 +28,19 @@ class AddToDoViewModel : ViewModel() {
private val _isButtonEnabled = MutableLiveData<Boolean>()
val isButtonEnabled: LiveData<Boolean> = _isButtonEnabled

fun addTodo(selectedDate: String, todo: String, todoCategoryId: Int) {
viewModelScope.launch {
toDoRepository.addToDo(
RequestAddToDoDto(
_priorityType.value?.typeString.toString(),
selectedDate,
todo,
todoCategoryId,
),
)
}
}

fun setSelectedPriorityType(priorityType: PriorityType) {
_priorityType.value = priorityType
checkButtonEnabled()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ class HomeFragment :
private val homeViewModel by viewModels<HomeViewModel>()
private val outerAdapter by lazy {
HomeTodoOuterAdapter(
moveToAddToDo = {
findNavController().navigate(R.id.navigation_addToDo)
moveToAddToDo = { categoryId ->
val bundle = putDataToBundle()
bundle.putInt(CATEGORY_ID, categoryId)
findNavController().navigate(R.id.navigation_addToDo, bundle)
},
onInnerItemClick = this,
onInnerToDoCheck = this,
Expand Down Expand Up @@ -91,15 +93,18 @@ class HomeFragment :

private fun showPutFailTagBottomFragment() {
val bottomSheetFragment = PutFailTagBottomSheetFragment()
.apply {
val bundle = Bundle()
bundle.putString(SELECTED_DATE, homeViewModel.selectedDate.value)
arguments = bundle
}
.apply { putDataToBundle() }
bottomSheetFragment.show(childFragmentManager, bottomSheetFragment.tag)
binding.clHomeAddCategory.setVisible(GONE)
}

private fun putDataToBundle(): Bundle {
Copy link
Member

Choose a reason for hiding this comment

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

날짜 보내는것만 따로 뺏구만

val bundle = Bundle()
bundle.putString(SELECTED_DATE, homeViewModel.selectedDate.value)
arguments = bundle
return bundle
}

private fun showAddCategory() {
with(binding) {
tvHomeAddCategoty.setOnSingleClickListener {
Expand Down Expand Up @@ -176,5 +181,6 @@ class HomeFragment :
companion object {
const val TAG = "homeBottomSheetFragmentTag"
const val SELECTED_DATE = "selectedDate"
const val CATEGORY_ID = "categoryId"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.example.howdroid.util.extension.setOnSingleClickListener
class HomeTodoOuterAdapter(
private val onInnerItemClick: TodoOptionClickListener,
private val onInnerToDoCheck: TodoCheckClickListener,
private val moveToAddToDo: (Unit) -> Unit,
private val moveToAddToDo: (Int) -> Unit,
) :
ListAdapter<Home.TodoCategoryData, HomeTodoOuterAdapter.HomeTodoOuterViewHolder>(
ItemDiffCallback<Home.TodoCategoryData>(
Expand Down Expand Up @@ -51,7 +51,7 @@ class HomeTodoOuterAdapter(
home.todoData,
)
binding.tvHomeCategoryAdd.setOnSingleClickListener {
moveToAddToDo(Unit)
moveToAddToDo(home.todoCategoryId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package com.example.howdroid.presentation.type

enum class PriorityType {
MOST_IMPORTANT, IMPORTANT, NOT_IMPORTANT
enum class PriorityType(
val typeString: String,
) {
MOST_IMPORTANT(
"매우중요",
),
IMPORTANT(
"중요",
),
NOT_IMPORTANT(
"중요하지 않음",
),
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<string name="add_todo_hint">15자 이하로 입력해 주세요</string>
<string name="add_todo_priority_title">우선 순위를 정해볼까요?</string>
<string name="add_todo_done">완료</string>
<string name="add_todo_most_important">매우중요</string>
<string name="add_todo_important">중요</string>
<string name="add_todo_not_important">중요하지 않음</string>

<!-- chart -->
<string name="chart_statistic_title">통계</string>
Expand Down