Skip to content

Commit

Permalink
added #17 added to check location before to call getWeather
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierGonzal committed Jan 13, 2020
1 parent 13c51c4 commit 8eb0429
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 31 deletions.
27 changes: 27 additions & 0 deletions app/src/main/java/com/architectcoders/openweather/CheckLocation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.architectcoders.openweather

import android.location.LocationManager

class CheckLocation(var locationManager: LocationManager) {

fun checkLocation( continuation: (Boolean) -> Unit) {

var gpsEnabled = false
var networkEnabled = false

try {
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
} catch (ex: Exception) {
}

try {
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
} catch (ex: Exception) {
}
if (!gpsEnabled && !networkEnabled) {
continuation(false)
} else {
continuation(true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.lifecycle.Observer
import com.architectcoders.data.repository.RegionRepository
import com.architectcoders.data.repository.WeatherRepository
import com.architectcoders.domain.Weather
import com.architectcoders.openweather.CheckLocation
import com.architectcoders.openweather.PermissionRequester
import com.architectcoders.openweather.R
import com.architectcoders.openweather.model.AndroidPermissionChecker
Expand All @@ -36,6 +37,9 @@ class MainActivity : AppCompatActivity() {
Manifest.permission.ACCESS_COARSE_LOCATION
)

private lateinit var checkLocation : CheckLocation


//private val adapter = CitiesAdapter()

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -60,9 +64,10 @@ class MainActivity : AppCompatActivity() {
}

//recycler.adapter = adapter

checkLocation = CheckLocation(
getSystemService(Context.LOCATION_SERVICE) as LocationManager)
location.setOnClickListener {
viewModel.checkLocation(getSystemService(Context.LOCATION_SERVICE) as LocationManager)
viewModel.checkLocation()
}
viewModel.model.observe(this, Observer(::updateUi))

Expand All @@ -87,13 +92,8 @@ class MainActivity : AppCompatActivity() {
is MainViewModel.UiModel.Content -> updateData(model.weather)
is MainViewModel.UiModel.ShowTurnOnLocation -> showTurnOnLocation()
is MainViewModel.UiModel.ShowTurnOnPermission -> showTurnOnPermission()
MainViewModel.UiModel.RequestLocationPermission -> coarsePermissionRequester.request {
if (it) {
viewModel.onCoarsePermissionRequested()
} else {
viewModel.showTurnOnPermission()
}
}
is MainViewModel.UiModel.CheckLocation -> checkLocation()
MainViewModel.UiModel.RequestLocationPermission -> checkLocation()
}
}

Expand Down Expand Up @@ -131,4 +131,24 @@ class MainActivity : AppCompatActivity() {
snackbar.show()
}

private fun checkPermission() {
coarsePermissionRequester.request {
if (it) {
viewModel.onCoarsePermissionRequested()
} else {
viewModel.showTurnOnPermission()
}
}
}

private fun checkLocation() {
checkLocation.checkLocation {
if (it) {
checkPermission()
} else {
viewModel.showTurnOnLocation()
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.architectcoders.openweather.ui.main

import android.location.LocationManager
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -29,6 +28,7 @@ class MainViewModel(var getWeather: GetWeather)
object ShowTurnOnLocation : UiModel()
object Loading : UiModel()
class Content(val weather: Weather) : UiModel()
class CheckLocation :UiModel()
object RequestLocationPermission : UiModel()
}

Expand All @@ -40,34 +40,17 @@ class MainViewModel(var getWeather: GetWeather)
_model.value = UiModel.RequestLocationPermission
}

fun checkLocation() {
_model.value = UiModel.CheckLocation()
}

fun onCoarsePermissionRequested() {
launch {
_model.value = UiModel.Loading
_model.value = UiModel.Content(getWeather.invoke())
}
}

fun checkLocation(locationManager: LocationManager) {

var gpsEnabled = false
var networkEnabled = false

try {
gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
} catch (ex: Exception) {
}

try {
networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
} catch (ex: Exception) {
}
if (!gpsEnabled && !networkEnabled) {
_model.value = UiModel.ShowTurnOnLocation
} else {
refresh()
}
}

fun onWeatherClicked(weather: Weather) {
_navigation.value = Event(weather)
}
Expand All @@ -76,4 +59,8 @@ class MainViewModel(var getWeather: GetWeather)
_model.value = UiModel.ShowTurnOnPermission
}

fun showTurnOnLocation(){
_model.value = UiModel.ShowTurnOnLocation
}

}

0 comments on commit 8eb0429

Please sign in to comment.