Skip to content

Commit c5d1318

Browse files
authored
Merge pull request #243 from boostcampwm-2022/feat/user_location_history
사용 위치 기록
2 parents ea5b79f + 93abc7a commit c5d1318

File tree

15 files changed

+111
-21
lines changed

15 files changed

+111
-21
lines changed

data/src/main/java/com/lighthouse/database/entity/UsageHistoryEntity.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import java.util.Date
2121
data class UsageHistoryEntity(
2222
@ColumnInfo(name = "gifticon_id") val gifticonId: String,
2323
@ColumnInfo(name = "date") val date: Date,
24-
@ColumnInfo(name = "address") val address: String,
24+
@ColumnInfo(name = "longitude") val longitude: Double,
25+
@ColumnInfo(name = "latitude") val latitude: Double,
2526
@ColumnInfo(name = "amount") val amount: Int
2627
) {
2728
companion object {
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.lighthouse.database.mapper
22

33
import com.lighthouse.database.entity.UsageHistoryEntity
4+
import com.lighthouse.domain.VertexLocation
45
import com.lighthouse.domain.model.UsageHistory
56

67
fun UsageHistoryEntity.toUsageHistory(): UsageHistory {
78
return UsageHistory(
89
date = date,
9-
address = address,
10+
location = VertexLocation(longitude, latitude),
1011
amount = amount
1112
)
1213
}
@@ -15,7 +16,8 @@ fun UsageHistory.toUsageHistoryEntity(gifticonId: String): UsageHistoryEntity {
1516
return UsageHistoryEntity(
1617
gifticonId = gifticonId,
1718
date = date,
18-
address = address,
19+
longitude = location?.longitude ?: 0f.toDouble(),
20+
latitude = location?.latitude ?: 0f.toDouble(),
1921
amount = amount
2022
)
2123
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.lighthouse.domain.model
22

3+
import com.lighthouse.domain.VertexLocation
34
import java.util.Date
45

56
data class UsageHistory(
67
val date: Date,
7-
val address: String,
8+
val location: VertexLocation?,
89
val amount: Int
910
)

domain/src/main/java/com/lighthouse/domain/usecase/UseCashCardGifticonUseCase.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ package com.lighthouse.domain.usecase
33
import com.lighthouse.domain.model.UsageHistory
44
import com.lighthouse.domain.repository.GifticonRepository
55
import com.lighthouse.domain.util.currentTime
6+
import kotlinx.coroutines.flow.first
67
import javax.inject.Inject
78

89
class UseCashCardGifticonUseCase @Inject constructor(
9-
private val gifticonRepository: GifticonRepository
10+
private val gifticonRepository: GifticonRepository,
11+
private val getUserLocationUseCase: GetUserLocationUseCase
1012
) {
1113

12-
suspend operator fun invoke(gifticonId: String, amount: Int) {
13-
val usageHistory = UsageHistory(currentTime, "광주 광산구 일곡동", amount) // TODO 위치 얻어오기
14+
suspend operator fun invoke(gifticonId: String, amount: Int, hasLocationPermission: Boolean) {
15+
val userLocation = if (hasLocationPermission) getUserLocationUseCase().first() else null
16+
val usageHistory = UsageHistory(currentTime, userLocation, amount)
17+
1418
gifticonRepository.useCashCardGifticon(gifticonId, amount, usageHistory)
1519
}
1620
}

domain/src/main/java/com/lighthouse/domain/usecase/UseGifticonUseCase.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package com.lighthouse.domain.usecase
33
import com.lighthouse.domain.model.UsageHistory
44
import com.lighthouse.domain.repository.GifticonRepository
55
import com.lighthouse.domain.util.currentTime
6+
import kotlinx.coroutines.flow.first
67
import javax.inject.Inject
78

89
class UseGifticonUseCase @Inject constructor(
9-
private val gifticonRepository: GifticonRepository
10+
private val gifticonRepository: GifticonRepository,
11+
private val getUserLocationUseCase: GetUserLocationUseCase
1012
) {
11-
suspend operator fun invoke(gifticonId: String) {
12-
val usageHistory = UsageHistory(currentTime, "광주 광산구 일곡동", 0) // TODO 위치 얻어오기
13+
suspend operator fun invoke(gifticonId: String, hasLocationPermission: Boolean) {
14+
val userLocation = if (hasLocationPermission) getUserLocationUseCase().first() else null
15+
val usageHistory = UsageHistory(currentTime, userLocation, 0)
1316

1417
gifticonRepository.useGifticon(gifticonId, usageHistory)
1518
}

presentation/src/main/java/com/lighthouse/presentation/ui/detailgifticon/CashCardGifticonInfoFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.fragment.app.activityViewModels
77
import com.lighthouse.presentation.R
88
import com.lighthouse.presentation.databinding.FragmentCashCardGifticonInfoBinding
99
import com.lighthouse.presentation.ui.common.viewBindings
10+
import com.lighthouse.presentation.util.Geography
1011

1112
class CashCardGifticonInfoFragment : Fragment(R.layout.fragment_cash_card_gifticon_info) {
1213
val binding: FragmentCashCardGifticonInfoBinding by viewBindings()
@@ -16,6 +17,7 @@ class CashCardGifticonInfoFragment : Fragment(R.layout.fragment_cash_card_giftic
1617
super.onViewCreated(view, savedInstanceState)
1718

1819
binding.vm = viewModel
20+
binding.geo = Geography(requireContext())
1921
binding.lifecycleOwner = viewLifecycleOwner
2022
binding.ctfBalance.addOnValueListener {
2123
viewModel.editBalance(it)

presentation/src/main/java/com/lighthouse/presentation/ui/detailgifticon/GifticonDetailActivity.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ import com.lighthouse.presentation.ui.detailgifticon.dialog.UseGifticonDialog
3232
import com.lighthouse.presentation.ui.edit.modifygifticon.ModifyGifticonActivity
3333
import com.lighthouse.presentation.ui.security.AuthCallback
3434
import com.lighthouse.presentation.ui.security.AuthManager
35+
import com.lighthouse.presentation.util.permission.LocationPermissionManager
36+
import com.lighthouse.presentation.util.permission.core.permissions
3537
import dagger.hilt.android.AndroidEntryPoint
3638
import kotlinx.coroutines.cancel
3739
import kotlinx.coroutines.delay
40+
import kotlinx.coroutines.flow.collectLatest
3841
import kotlinx.coroutines.launch
3942
import javax.inject.Inject
4043

@@ -63,6 +66,8 @@ class GifticonDetailActivity : AppCompatActivity() {
6366
private val chip by lazy { binding.chipScrollDownForUseButton }
6467
private val spinnerDatePicker = SpinnerDatePicker()
6568

69+
private val locationPermission: LocationPermissionManager by permissions()
70+
6671
@Inject
6772
lateinit var authManager: AuthManager
6873
private val biometricLauncher: ActivityResultLauncher<Intent> =
@@ -104,6 +109,11 @@ class GifticonDetailActivity : AppCompatActivity() {
104109
chip.isVisible = btnMaster.isOnScreen().not()
105110
}
106111
}
112+
repeatOnStarted {
113+
locationPermission.permissionFlow.collectLatest {
114+
viewModel.updateLocationPermission(it)
115+
}
116+
}
107117
repeatOnStarted {
108118
viewModel.event.collect { event ->
109119
handleEvent(event)

presentation/src/main/java/com/lighthouse/presentation/ui/detailgifticon/GifticonDetailViewModel.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ class GifticonDetailViewModel @Inject constructor(
106106
private val _masterButtonLabel = MutableStateFlow<UIText>(UIText.Empty)
107107
val masterButtonLabel = _masterButtonLabel.asStateFlow()
108108

109+
var hasLocationPermission = MutableStateFlow(false)
110+
private set
111+
109112
fun scrollDownForUseButtonClicked() {
110113
event(GifticonDetailEvent.ScrollDownForUseButtonClicked)
111114
}
@@ -150,11 +153,11 @@ class GifticonDetailViewModel @Inject constructor(
150153
viewModelScope.launch {
151154
if (gifticon.value?.isCashCard == true) {
152155
assert((gifticon.value?.balance ?: 0) >= amountToBeUsed.value)
153-
useCashCardGifticonUseCase(gifticonId, amountToBeUsed.value)
156+
useCashCardGifticonUseCase(gifticonId, amountToBeUsed.value, hasLocationPermission.value)
154157
amountToBeUsed.value = 0
155158
event(GifticonDetailEvent.UseGifticonComplete)
156159
} else {
157-
useGifticonUseCase(gifticonId)
160+
useGifticonUseCase(gifticonId, hasLocationPermission.value)
158161
event(GifticonDetailEvent.UseGifticonComplete)
159162
}
160163
}
@@ -190,6 +193,10 @@ class GifticonDetailViewModel @Inject constructor(
190193
}
191194
}
192195

196+
fun updateLocationPermission(isLocationPermission: Boolean) {
197+
hasLocationPermission.value = isLocationPermission
198+
}
199+
193200
private fun switchMode(mode: GifticonDetailMode) {
194201
_mode.value = mode
195202
_scrollDownChipLabel.value = when (_mode.value) {
@@ -199,8 +206,8 @@ class GifticonDetailViewModel @Inject constructor(
199206
}
200207
_masterButtonLabel.value = when (_mode.value) {
201208
GifticonDetailMode.UNUSED -> UIText.StringResource(R.string.gifticon_detail_unused_mode_button_text)
202-
GifticonDetailMode.EDIT -> UIText.StringResource(R.string.gifticon_detail_used_mode_button_text)
203-
GifticonDetailMode.USED -> UIText.StringResource(R.string.gifticon_detail_edit_mode_button_text)
209+
GifticonDetailMode.EDIT -> UIText.StringResource(R.string.gifticon_detail_edit_mode_button_text)
210+
GifticonDetailMode.USED -> UIText.StringResource(R.string.gifticon_detail_used_mode_button_text)
204211
}
205212
}
206213

presentation/src/main/java/com/lighthouse/presentation/ui/detailgifticon/StandardGifticonInfoFragment.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.fragment.app.activityViewModels
77
import com.lighthouse.presentation.R
88
import com.lighthouse.presentation.databinding.FragmentStandardGifticonInfoBinding
99
import com.lighthouse.presentation.ui.common.viewBindings
10+
import com.lighthouse.presentation.util.Geography
1011

1112
class StandardGifticonInfoFragment : Fragment(R.layout.fragment_standard_gifticon_info) {
1213
private val binding: FragmentStandardGifticonInfoBinding by viewBindings()
@@ -15,6 +16,7 @@ class StandardGifticonInfoFragment : Fragment(R.layout.fragment_standard_giftico
1516
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
1617
super.onViewCreated(view, savedInstanceState)
1718
binding.vm = viewModel
19+
binding.geo = Geography(requireContext())
1820
binding.lifecycleOwner = viewLifecycleOwner
1921
}
2022
}

presentation/src/main/java/com/lighthouse/presentation/ui/detailgifticon/dialog/UsageHistoryAdapter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import com.lighthouse.domain.model.UsageHistory
99
import com.lighthouse.presentation.R
1010
import com.lighthouse.presentation.adapter.BindableListAdapter
1111
import com.lighthouse.presentation.databinding.ItemUsageHistoryBinding
12+
import com.lighthouse.presentation.util.Geography
1213

1314
class UsageHistoryAdapter : BindableListAdapter<UsageHistory, UsageHistoryAdapter.UsageHistoryViewHolder>(diffUtil) {
1415

1516
class UsageHistoryViewHolder(private val binding: ItemUsageHistoryBinding) :
1617
RecyclerView.ViewHolder(binding.root) {
1718
fun bind(usageHistory: UsageHistory) {
19+
binding.geo = Geography(binding.root.context)
1820
binding.usage = usageHistory
1921
}
2022
}

0 commit comments

Comments
 (0)