Skip to content

Commit

Permalink
✨ GPS 상태 레포지토리 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yonghanJu committed Sep 21, 2023
1 parent 4027aa8 commit b81ef95
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.whyranoid.data.repository

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.location.LocationManager
import com.whyranoid.domain.repository.GpsRepository
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow

class GpsRepositoryImpl(private val context: Context) : GpsRepository {

private val _gpsEnabledState = MutableStateFlow(isGpsEnabled())
override fun getGpsEnabledState() = _gpsEnabledState.asStateFlow()

private val gpsStatusReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
checkGpsStatus()
}
}

override fun registerReceiver() {
context.registerReceiver(
gpsStatusReceiver,
IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION),
)
}

override fun unRegisterReceiver() = context.unregisterReceiver(gpsStatusReceiver)

private fun checkGpsStatus() {
_gpsEnabledState.value = isGpsEnabled()
}

private fun isGpsEnabled() = context.getSystemService(LocationManager::class.java)
.isProviderEnabled(LocationManager.GPS_PROVIDER)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.whyranoid.domain.repository

import kotlinx.coroutines.flow.StateFlow

interface GpsRepository {
fun getGpsEnabledState(): StateFlow<Boolean>
fun registerReceiver()
fun unRegisterReceiver()
}

0 comments on commit b81ef95

Please sign in to comment.