Skip to content

Commit 06db43a

Browse files
committed
feat : 페이크 데이터로 알람 정보 저장 로직 구현
1 parent d2cfb39 commit 06db43a

File tree

5 files changed

+76
-37
lines changed

5 files changed

+76
-37
lines changed

data/src/main/java/com/stop/data/local/source/alarm/AlarmLocalDataSourceImpl.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ internal class AlarmLocalDataSourceImpl @Inject constructor(
3535
override suspend fun getAlarm(): Flow<AlarmRepositoryItem?> {
3636
return context.datastore.data.map { preferences ->
3737
val jsonString = preferences[ALARM] ?: ""
38-
val elements = adapter.fromJson(jsonString)?.toRepositoryModel()
39-
elements
38+
if (jsonString.isNotBlank()) {
39+
adapter.fromJson(jsonString)?.toRepositoryModel()
40+
} else {
41+
null
42+
}
4043
}
4144
}
4245

presentation/src/main/java/com/stop/ui/alarmsetting/AlarmSettingFragment.kt

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ import android.view.View
99
import android.view.ViewGroup
1010
import androidx.fragment.app.Fragment
1111
import androidx.fragment.app.viewModels
12-
import androidx.lifecycle.lifecycleScope
1312
import com.stop.R
1413
import com.stop.databinding.FragmentAlarmSettingBinding
15-
import com.stop.domain.model.alarm.AlarmUseCaseItem
1614
import dagger.hilt.android.AndroidEntryPoint
17-
import kotlinx.coroutines.flow.collectLatest
18-
import kotlinx.coroutines.launch
1915

2016
@AndroidEntryPoint
2117
class AlarmSettingFragment : Fragment() {
@@ -38,48 +34,33 @@ class AlarmSettingFragment : Fragment() {
3834

3935
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
4036
super.onViewCreated(view, savedInstanceState)
41-
val test = AlarmUseCaseItem(
42-
"abc",
43-
"abc",
44-
listOf("ABC"),
45-
"abc","abc",true,true
46-
)
47-
48-
lifecycleScope.launch{
49-
alarmSettingViewModel.save(test)
50-
}
51-
52-
/* lifecycleScope.launch{
53-
alarmSettingViewModel.get().asLiveData().observe(viewLifecycleOwner){
54-
Log.e("ABC", it.toString())
55-
}
56-
}*/
57-
58-
lifecycleScope.launch{
59-
alarmSettingViewModel.get().collectLatest {
60-
Log.e("ABC", it.toString())
61-
}
62-
}
63-
6437

6538
initView()
6639
setButtonListener()
40+
setToggleListener()
41+
alarmSettingViewModel.getAlarm()
42+
alarmSettingViewModel.alarmUseCaseItem.observe(viewLifecycleOwner) {
43+
Log.e("ABC", it.toString())
44+
}
6745
}
6846

69-
private fun initBinding(){
47+
private fun initBinding() {
7048
binding.apply {
7149
lifecycleOwner = viewLifecycleOwner
7250
viewModel = alarmSettingViewModel
7351
}
7452
}
7553

7654
private fun initView() {
77-
with(binding){
55+
with(binding) {
7856
textViewLastTime.text = getString(R.string.last_transport_arrival_time, 23, 30)
7957
textViewWalk.text = getString(R.string.last_transport_walking_time, 10)
8058

8159
numberPickerAlarmTime.minValue = 0
8260
numberPickerAlarmTime.maxValue = 60
61+
62+
buttonSound.isCheckable = true
63+
buttonMissionOn.isCheckable = true
8364
}
8465
}
8566

@@ -113,6 +94,28 @@ class AlarmSettingFragment : Fragment() {
11394
}
11495
}
11596

97+
private fun setToggleListener() {
98+
with(binding) {
99+
toggleGroupAlarm.addOnButtonCheckedListener { group, checkedId, isChecked ->
100+
if (isChecked) {
101+
when (checkedId) {
102+
R.id.button_sound -> alarmSettingViewModel.alarmMethod = true
103+
else -> alarmSettingViewModel.alarmMethod = false
104+
}
105+
}
106+
}
107+
108+
toggleGroupMission.addOnButtonCheckedListener { group, checkedId, isChecked ->
109+
if (isChecked) {
110+
when (checkedId) {
111+
R.id.button_mission_on -> alarmSettingViewModel.isMission = true
112+
else -> alarmSettingViewModel.isMission = false
113+
}
114+
}
115+
}
116+
}
117+
}
118+
116119
override fun onDestroyView() {
117120
_binding = null
118121
super.onDestroyView()
Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.stop.ui.alarmsetting
22

3+
import androidx.lifecycle.LiveData
34
import androidx.lifecycle.MutableLiveData
45
import androidx.lifecycle.ViewModel
6+
import androidx.lifecycle.viewModelScope
57
import com.stop.domain.model.alarm.AlarmUseCaseItem
68
import com.stop.domain.usecase.alarm.GetAlarmUseCase
79
import com.stop.domain.usecase.alarm.SaveAlarmUseCase
810
import dagger.hilt.android.lifecycle.HiltViewModel
11+
import kotlinx.coroutines.Dispatchers
12+
import kotlinx.coroutines.flow.collectLatest
13+
import kotlinx.coroutines.launch
914
import javax.inject.Inject
1015

1116
@HiltViewModel
@@ -16,11 +21,36 @@ class AlarmSettingViewModel @Inject constructor(
1621

1722
val alarmTime = MutableLiveData(0)
1823

19-
suspend fun save(alarmUseCaseItem: AlarmUseCaseItem){
20-
saveAlarmUseCase.saveAlarm(alarmUseCaseItem)
24+
//TODO TEST 코드
25+
private val _alarmUseCaseItem = MutableLiveData<AlarmUseCaseItem?>()
26+
val alarmUseCaseItem: LiveData<AlarmUseCaseItem?> = _alarmUseCaseItem
27+
28+
var alarmMethod = true
29+
var isMission = true
30+
31+
fun saveAlarm() {
32+
val alarmUseCaseItem = AlarmUseCaseItem(
33+
"쑥고개로 2다길 1",
34+
"현대 아남타워",
35+
listOf("도보", "버스5517", "신림역", "선릉역", "도보"),
36+
"23:30",
37+
alarmTime.value.toString(),
38+
alarmMethod,
39+
isMission
40+
)
41+
42+
viewModelScope.launch(Dispatchers.IO) {
43+
saveAlarmUseCase.saveAlarm(alarmUseCaseItem)
44+
}
2145
}
2246

23-
suspend fun get() =
24-
getAlarmUseCase.getAlarm()
47+
// TODO TEST 코드
48+
fun getAlarm() {
49+
viewModelScope.launch(Dispatchers.IO) {
50+
getAlarmUseCase.getAlarm().collectLatest {
51+
_alarmUseCaseItem.postValue(it)
52+
}
53+
}
54+
}
2555

2656
}

presentation/src/main/res/layout/fragment_alarm_setting.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
android:layout_height="wrap_content"
175175
android:layout_marginStart="12dp"
176176
android:layout_marginBottom="9dp"
177+
app:checkedButton="@id/button_sound"
177178
app:layout_constraintBottom_toTopOf="@id/toggle_group_mission"
178179
app:layout_constraintEnd_toEndOf="parent"
179180
app:layout_constraintStart_toEndOf="@id/text_view_alarm_method"
@@ -217,6 +218,7 @@
217218
android:layout_height="wrap_content"
218219
android:layout_marginStart="12dp"
219220
android:layout_marginTop="9dp"
221+
app:checkedButton="@id/button_mission_on"
220222
app:layout_constraintBottom_toTopOf="@id/text_view_register_alarm"
221223
app:layout_constraintEnd_toEndOf="parent"
222224
app:layout_constraintStart_toEndOf="@id/text_view_mission_toggle"
@@ -246,10 +248,11 @@
246248
android:layout_marginBottom="24dp"
247249
android:background="@drawable/background_gray_d9_8"
248250
android:gravity="center"
249-
android:textSize="16sp"
251+
android:onClick="@{() -> viewModel.saveAlarm()}"
250252
android:paddingVertical="8dp"
251253
android:text="@string/register_alarm_text"
252254
android:textColor="@color/black"
255+
android:textSize="16sp"
253256
android:textStyle="bold"
254257
app:layout_constraintBottom_toBottomOf="parent"
255258
app:layout_constraintEnd_toEndOf="@id/guide_line_end"

presentation/src/main/res/values/themes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<resources xmlns:tools="http://schemas.android.com/tools">
22
<!-- Base application theme. -->
3-
<style name="Theme.PlzStop" parent="Theme.Material3.DayNight.NoActionBar">
3+
<style name="Theme.PlzStop" parent="Theme.Material3.Light.NoActionBar">
44
<!-- Primary brand color. -->
55
<item name="colorPrimary">@color/purple_500</item>
66
<item name="colorPrimaryVariant">@color/purple_700</item>

0 commit comments

Comments
 (0)