Skip to content

Commit

Permalink
#58 feat: 일정 작성 API 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunBaee committed Jun 18, 2023
1 parent 64a1381 commit f7eeed8
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.chunbae.narchive.data.remote.api

import com.chunbae.narchive.data.remote.request.RequestModifyTodoGroupData
import com.chunbae.narchive.data.remote.request.RequestTodoData
import com.chunbae.narchive.data.remote.request.RequestTodoGroupData
import com.chunbae.narchive.data.remote.response.ResponseCommonWithString
import com.chunbae.narchive.data.remote.response.ResponseTodoGroupData
Expand Down Expand Up @@ -48,4 +49,9 @@ interface TodoService {
@GET("/todo/default")
suspend fun getDefaultTodoGroup()
: Response<ResponseTodoGroupData>

@POST("/todo")
suspend fun postTodo(
@Body body : RequestTodoData
) : Response<ResponseCommonWithString>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.constraintlayout.widget.Group
import com.chunbae.narchive.data.data.GroupData
import com.chunbae.narchive.data.data.TodoData
import com.chunbae.narchive.data.remote.request.RequestModifyTodoGroupData
import com.chunbae.narchive.data.remote.request.RequestTodoData
import com.chunbae.narchive.data.remote.request.RequestTodoGroupData
import com.chunbae.narchive.domain.repository.TodoGroupRepository
import com.chunbae.narchive.domain.source.TodoGroupSource
Expand Down Expand Up @@ -38,5 +39,9 @@ class TodoGroupRepositoryImpl @Inject constructor(private val todoGroupSource: T
return todoGroupSource.getDefaultTodoGroup()
}

override suspend fun postTodo(body: RequestTodoData): Result<String> {
return todoGroupSource.postTodo(body)
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.chunbae.narchive.data.remote.request

data class RequestTodoData (
var todoGroupIdx : Int,
var todoTitle : String,
var startDate : String,
var startTime : String,
var endDate : String,
var endTime : String
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.chunbae.narchive.data.data.GroupData
import com.chunbae.narchive.data.data.TodoData
import com.chunbae.narchive.data.remote.api.TodoService
import com.chunbae.narchive.data.remote.request.RequestModifyTodoGroupData
import com.chunbae.narchive.data.remote.request.RequestTodoData
import com.chunbae.narchive.data.remote.request.RequestTodoGroupData
import com.chunbae.narchive.domain.source.TodoGroupSource
import javax.inject.Inject
Expand Down Expand Up @@ -66,4 +67,12 @@ class TodoGroupRemoteSource @Inject constructor(private val todoService : TodoSe
}
return Result.failure(IllegalArgumentException(res.message()))
}

override suspend fun postTodo(body: RequestTodoData): Result<String> {
val res = todoService.postTodo(body)
if(res.isSuccessful) {
return Result.success(res.body()!!.result)
}
return Result.failure(IllegalArgumentException(res.message()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.chunbae.narchive.domain.repository
import com.chunbae.narchive.data.data.GroupData
import com.chunbae.narchive.data.data.TodoData
import com.chunbae.narchive.data.remote.request.RequestModifyTodoGroupData
import com.chunbae.narchive.data.remote.request.RequestTodoData
import com.chunbae.narchive.data.remote.request.RequestTodoGroupData

interface TodoGroupRepository {
Expand All @@ -20,4 +21,6 @@ interface TodoGroupRepository {
suspend fun patchDefaultTodoGroup(past : Int, cur : Int) : Result<String>

suspend fun getDefaultTodoGroup() : Result<GroupData>

suspend fun postTodo(body : RequestTodoData) : Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.chunbae.narchive.domain.source
import com.chunbae.narchive.data.data.GroupData
import com.chunbae.narchive.data.data.TodoData
import com.chunbae.narchive.data.remote.request.RequestModifyTodoGroupData
import com.chunbae.narchive.data.remote.request.RequestTodoData
import com.chunbae.narchive.data.remote.request.RequestTodoGroupData

interface TodoGroupSource {
Expand All @@ -20,4 +21,6 @@ interface TodoGroupSource {
suspend fun patchDefaultTodoGroup(past : Int, cur : Int) : Result<String>

suspend fun getDefaultTodoGroup() : Result<GroupData>

suspend fun postTodo(body : RequestTodoData) : Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class WriteTodoFragment : Fragment() {
initPicker()
observeCalendarState()
openGroupSelectDialog()
observeTodoUploaded()

return binding.root
}
Expand Down Expand Up @@ -232,4 +233,11 @@ class WriteTodoFragment : Fragment() {
if(it) WriteTodoGroupDialog().show(requireActivity().supportFragmentManager, "GROUP")
}
}

private fun observeTodoUploaded() {
todoViewModel.updateFinished.observe(viewLifecycleOwner) {
requireActivity().supportFragmentManager.popBackStack()
requireActivity().supportFragmentManager.beginTransaction().commit()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.chunbae.narchive.data.data.GroupData
import com.chunbae.narchive.data.remote.request.RequestTodoData
import com.chunbae.narchive.data.remote.request.RequestTodoGroupData
import com.chunbae.narchive.domain.repository.TodoGroupRepository
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -52,9 +53,6 @@ class WriteTodoViewModel @Inject constructor(private val todoGroupRepository: To
private val _endMin = MutableLiveData<String>().apply { value = endHour.value }
val endMin: LiveData<String> = _endMin

private val _isAllday = MutableLiveData<Boolean>().apply { value = false }
val isAllday: LiveData<Boolean> = _isAllday

private var _isStartDay = MutableLiveData<Int>()
val isStartDay: LiveData<Int> = _isStartDay

Expand All @@ -67,6 +65,9 @@ class WriteTodoViewModel @Inject constructor(private val todoGroupRepository: To
private val _selectedGroup = MutableLiveData<GroupData>().apply { value = GroupData(0, "기본", "PINK", "N") }
val selectedGroup : LiveData<GroupData> = _selectedGroup

private val _updateFinished = MutableLiveData<Boolean>()
val updateFinished : LiveData<Boolean> = _updateFinished



fun manageCalendarState(state: Int) { // 0:all gone / 1 : start Cal / 2 : end Cal / 3 : start Time / 4 : end Time
Expand Down Expand Up @@ -122,10 +123,6 @@ class WriteTodoViewModel @Inject constructor(private val todoGroupRepository: To
_selectedGroup.value = userGroupList.value!![position]
}

fun setIsAllDayClicked() {
_isAllday.value = _isAllday.value!!.not()
}

fun getDefaultTodoGroup() {
viewModelScope.launch {
todoGroupRepository.getDefaultTodoGroup()
Expand All @@ -134,7 +131,28 @@ class WriteTodoViewModel @Inject constructor(private val todoGroupRepository: To
}

fun saveTodo() {
Log.d("----", "saveTOdo: ${todoTitle.value} / ${startDate.value}-${startTime.value} / ${endDate.value}-${endTime.value} / ${isAllday.value} / ${_selectedGroup.value}")
viewModelScope.launch {
todoGroupRepository.postTodo(mapToRequest())
.onSuccess { _updateFinished.value = true }
}
}

private fun String.convertDate() : String {
return this.replace("", "-").replace("", "-").substring(0, this.length - 1).replace(" ", "")
}

private fun String.convertTime() : String {
return this.replace(" ", "")
}

private fun mapToRequest() : RequestTodoData {
return RequestTodoData(
selectedGroup.value!!.todoGroupIdx!!,
todoTitle.value!!,
startDate.value!!.convertDate(),
startTime.value!!.convertTime(),
endDate.value!!.convertDate(),
endTime.value!!.convertTime()
)
}
}
32 changes: 4 additions & 28 deletions app/src/main/res/layout/fragment_write_todo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,49 +277,25 @@

</androidx.constraintlayout.widget.ConstraintLayout>

<CheckBox
android:id="@+id/fg_wrtie_todo_check_is_all_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_5"
android:layout_marginTop="@dimen/margin_10"
android:buttonTint="@color/text_black"
android:onClick="@{() -> todoViewModel.setIsAllDayClicked()}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/fg_wrtie_todo_layout_end_calendar_shell" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/pretendard_semibold"
android:includeFontPadding="false"
android:text="@string/write_todo_todo_tv_all_day"
android:textColor="@color/text_black"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/fg_wrtie_todo_check_is_all_day"
app:layout_constraintStart_toEndOf="@id/fg_wrtie_todo_check_is_all_day"
app:layout_constraintTop_toTopOf="@id/fg_wrtie_todo_check_is_all_day" />

<TextView
android:id="@+id/fg_write_todo_tv_todo_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_5"
android:layout_marginTop="@dimen/margin_20"
android:fontFamily="@font/pretendard_semibold"
android:includeFontPadding="false"
android:text="@string/write_todo_todo_group"
android:textColor="@color/text_black"
android:textSize="15sp"
app:layout_constraintStart_toStartOf="@id/fg_write_todo_tv_todo_time"
app:layout_constraintTop_toBottomOf="@id/fg_wrtie_todo_check_is_all_day" />
app:layout_constraintStart_toStartOf="@id/fg_write_todo_tv_todo_end"
app:layout_constraintTop_toBottomOf="@id/fg_write_todo_tv_todo_end" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_20"
android:onClick="@{() -> todoViewModel.groupDialogOpen()}"
app:layout_constraintBottom_toBottomOf="@+id/fg_write_todo_tv_todo_group"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toEndOf="@+id/fg_write_todo_btn_end_time"
app:layout_constraintTop_toTopOf="@+id/fg_write_todo_tv_todo_group">

<com.google.android.material.card.MaterialCardView
Expand Down

0 comments on commit f7eeed8

Please sign in to comment.