-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
data/src/main/java/com/whyranoid/data/repository/GpsRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
9 changes: 9 additions & 0 deletions
9
domain/src/main/java/com/whyranoid/domain/repository/GpsRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |