Skip to content

Commit

Permalink
๐Ÿ’ก ์„ค์ • ํŽ˜์ด์ง€ | ์•Œ๋ฆผ ์ˆ˜์‹  ์—ฌ๋ถ€ ์กฐํšŒ ๋ฐ ์ˆ˜์ • ๊ธฐ๋Šฅ (#24)
Browse files Browse the repository at this point in the history
* Add Alarm Status APIs

* Setting Fragment | Alarm Status
  • Loading branch information
moondev03 authored Sep 12, 2024
1 parent ebe78cd commit f8313aa
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.neverland.data.datasource
import com.neverland.data.remote.model.BaseResponse
import com.neverland.data.remote.model.alarm.AlarmDTO
import retrofit2.Response
import retrofit2.http.PATCH
import retrofit2.http.Query

interface AlarmDataSource {
suspend fun readAlarm(
Expand All @@ -22,4 +24,13 @@ interface AlarmDataSource {
suspend fun checkAllAlarm(
ssaId: String
): Response<BaseResponse<Boolean>>

suspend fun getAlarmStatus(
ssaId: String
): Response<BaseResponse<Boolean>>

@PATCH("/api/alarm/alarm-toggle")
suspend fun patchAlarmStatus(
ssaId: String
): Response<BaseResponse<Boolean>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ class AlarmDataSourceImpl @Inject constructor(
override suspend fun checkAllAlarm(ssaId: String): Response<BaseResponse<Boolean>> {
return service.checkAllAlarm(ssaId)
}

override suspend fun getAlarmStatus(ssaId: String): Response<BaseResponse<Boolean>> {
return service.getAlarmStatus(ssaId)
}

override suspend fun patchAlarmStatus(ssaId: String): Response<BaseResponse<Boolean>> {
return service.patchAlarmStatus(ssaId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.neverland.data.remote.model.BaseResponse
import com.neverland.data.remote.model.alarm.AlarmDTO
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.PATCH
import retrofit2.http.Query

interface AlarmService {
Expand All @@ -28,4 +29,14 @@ interface AlarmService {
suspend fun checkAllAlarm(
@Query("SSAID") ssaId: String
): Response<BaseResponse<Boolean>>

@GET("/api/alarm/alarm-status")
suspend fun getAlarmStatus(
@Query("SSAID") ssaId: String
): Response<BaseResponse<Boolean>>

@PATCH("/api/alarm/alarm-toggle")
suspend fun patchAlarmStatus(
@Query("SSAID") ssaId: String
): Response<BaseResponse<Boolean>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,18 @@ class AlarmRepositoryImpl @Inject constructor(
onSuccess = { it!! }
)
}

override suspend fun getAlarmStatus(ssaId: String): Result<Boolean> {
return handleResponse(
call = { datasource.getAlarmStatus(ssaId) },
onSuccess = { it!! }
)
}

override suspend fun patchAlarmStatus(ssaId: String): Result<Boolean> {
return handleResponse(
call = { datasource.patchAlarmStatus(ssaId) },
onSuccess = { true }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ interface AlarmRepository {

suspend fun checkAllAlarm(ssaId: String): Result<Boolean>

suspend fun getAlarmStatus(ssaId: String): Result<Boolean>

suspend fun patchAlarmStatus(ssaId: String): Result<Boolean>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.neverland.domain.usecase.alarm

import com.neverland.domain.repository.AlarmRepository
import javax.inject.Inject


class GetAlarmStatusUseCase @Inject constructor(
private val repository: AlarmRepository
) {
suspend operator fun invoke(ssaId: String): Result<Boolean> {
return repository.getAlarmStatus(ssaId)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.neverland.domain.usecase.alarm

import com.neverland.domain.repository.AlarmRepository
import javax.inject.Inject


class PatchAlarmStatusUseCase @Inject constructor(
private val repository: AlarmRepository
) {
suspend operator fun invoke(ssaId: String): Result<Boolean> {
return repository.patchAlarmStatus(ssaId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class SettingFragment: BaseFragment<FragmentSettingBinding>() {
}

setVersionName()
viewModel.fetchAlarmStatus()
viewModel.fetchKeyword()
}

Expand All @@ -46,6 +47,30 @@ class SettingFragment: BaseFragment<FragmentSettingBinding>() {
}
}
}

viewModel.uiState.observe(viewLifecycleOwner){
when(it){
is UiState.Loading -> {}
is UiState.Empty -> {}
is UiState.Error -> {}
is UiState.Success -> {
binding.switchAlarm.isChecked = it.data

binding.switchAlarm.setOnCheckedChangeListener { _, _ -> viewModel.patchAlarmStatus() }
}
}
}

viewModel.alarmStatus.observe(viewLifecycleOwner){
when(it){
is UiState.Loading -> {}
is UiState.Empty -> {}
is UiState.Error -> {}
is UiState.Success -> {
binding.switchAlarm.isChecked = !binding.switchAlarm.isChecked
}
}
}
}

private fun setupKeywordRecyclerView(list: List<Keyword>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.neverland.domain.model.keyword.Keyword
import com.neverland.domain.usecase.alarm.GetAlarmStatusUseCase
import com.neverland.domain.usecase.alarm.PatchAlarmStatusUseCase
import com.neverland.domain.usecase.keyword.GetKeywordUseCase
import com.neverland.thinkerbell.base.ThinkerBellApplication.Companion.application
import com.neverland.thinkerbell.utils.UiState
Expand All @@ -15,11 +17,19 @@ import javax.inject.Inject
@HiltViewModel
class SettingViewModel @Inject constructor(
private val getKeywordUseCase: GetKeywordUseCase,
private val getAlarmStatusUseCase: GetAlarmStatusUseCase,
private val patchAlarmStatusUseCase: PatchAlarmStatusUseCase
) : ViewModel() {

private val _keyword = MutableLiveData<UiState<List<Keyword>>>(UiState.Loading)
val keyword: LiveData<UiState<List<Keyword>>> get() = _keyword

private val _uiState = MutableLiveData<UiState<Boolean>>(UiState.Loading)
val uiState: LiveData<UiState<Boolean>> get() = _uiState

private val _alarmStatus = MutableLiveData<UiState<Unit>>(UiState.Loading)
val alarmStatus: LiveData<UiState<Unit>> get() = _alarmStatus

fun fetchKeyword() {
_keyword.value = UiState.Loading
viewModelScope.launch {
Expand All @@ -32,4 +42,32 @@ class SettingViewModel @Inject constructor(
}
}
}

fun fetchAlarmStatus(){
_uiState.value = UiState.Loading

viewModelScope.launch {
getAlarmStatusUseCase(application.getAndroidId())
.onSuccess {
_uiState.value = UiState.Success(it)
}
.onFailure {
_uiState.value = UiState.Error(it)
}
}
}

fun patchAlarmStatus(){
_alarmStatus.value = UiState.Loading

viewModelScope.launch {
patchAlarmStatusUseCase(application.getAndroidId())
.onSuccess {
_alarmStatus.value = UiState.Success(Unit)
}
.onFailure {
_alarmStatus.value = UiState.Error(it)
}
}
}
}

0 comments on commit f8313aa

Please sign in to comment.