diff --git a/app/src/main/java/com/abdurakhmanov/ridez/RidezApplication.kt b/app/src/main/java/com/abdurakhmanov/ridez/RidezApplication.kt index 5aa0240..4978b71 100644 --- a/app/src/main/java/com/abdurakhmanov/ridez/RidezApplication.kt +++ b/app/src/main/java/com/abdurakhmanov/ridez/RidezApplication.kt @@ -4,6 +4,4 @@ import android.app.Application import dagger.hilt.android.HiltAndroidApp @HiltAndroidApp -class RidezApplication : Application() { - -} \ No newline at end of file +class RidezApplication : Application() \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/data/repository/LocationRepository.kt b/app/src/main/java/com/abdurakhmanov/ridez/data/repository/LocationRepository.kt index 4e75468..fe5daa2 100644 --- a/app/src/main/java/com/abdurakhmanov/ridez/data/repository/LocationRepository.kt +++ b/app/src/main/java/com/abdurakhmanov/ridez/data/repository/LocationRepository.kt @@ -1,20 +1,16 @@ package com.abdurakhmanov.ridez.data.repository import com.abdurakhmanov.ridez.data.models.Location -import com.abdurakhmanov.ridez.data.response.CurrentAddress import com.abdurakhmanov.ridez.data.source.local.LocationUpdate import com.abdurakhmanov.ridez.data.source.local.LocationUpdateDao -import com.abdurakhmanov.ridez.data.source.remote.GeocoderApiService import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.* -import retrofit2.Response import javax.inject.Inject import javax.inject.Singleton @Singleton class LocationRepository @Inject constructor( - private val locationUpdateDao: LocationUpdateDao, - private val geocoderApiService: GeocoderApiService + private val locationUpdateDao: LocationUpdateDao ) { @OptIn(ExperimentalCoroutinesApi::class) @@ -31,8 +27,4 @@ class LocationRepository @Inject constructor( suspend fun insertLocationUpdate(locationUpdate: LocationUpdate) = locationUpdateDao.insert(locationUpdate) - - suspend fun getAddress(apiKey: String, lat: Double, lon: Double?): Response { - return geocoderApiService.getAddress(apiKey, lat, lon) - } } \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/data/response/CurrentAddress.kt b/app/src/main/java/com/abdurakhmanov/ridez/data/response/CurrentAddress.kt deleted file mode 100644 index 629c755..0000000 --- a/app/src/main/java/com/abdurakhmanov/ridez/data/response/CurrentAddress.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.abdurakhmanov.ridez.data.response - -data class CurrentAddress( - val lat: Double = 0.0, - val lon: Double = 0.0, - val postaladdress: String = "", - val zipcode: String = "", - val city: String = "", - val state: String = "", - val country: String = "" -) diff --git a/app/src/main/java/com/abdurakhmanov/ridez/data/source/remote/GeocoderApiService.kt b/app/src/main/java/com/abdurakhmanov/ridez/data/source/remote/GeocoderApiService.kt deleted file mode 100644 index 7332bc7..0000000 --- a/app/src/main/java/com/abdurakhmanov/ridez/data/source/remote/GeocoderApiService.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.abdurakhmanov.ridez.data.source.remote - -import com.abdurakhmanov.ridez.data.response.CurrentAddress -import retrofit2.Response -import retrofit2.http.GET -import retrofit2.http.Query - -interface GeocoderApiService { - @GET("getAddress") - suspend fun getAddress( - @Query("apikey") apiKey: String, - @Query("lat") lat: Double, - @Query("lon") lon: Double? - ): Response -} \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/di/NetworkModule.kt b/app/src/main/java/com/abdurakhmanov/ridez/di/NetworkModule.kt deleted file mode 100644 index 2b74e4d..0000000 --- a/app/src/main/java/com/abdurakhmanov/ridez/di/NetworkModule.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.abdurakhmanov.ridez.di - -import com.abdurakhmanov.ridez.data.source.remote.GeocoderApiService -import com.squareup.moshi.Moshi -import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory -import dagger.Module -import dagger.Provides -import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent -import retrofit2.Retrofit -import retrofit2.converter.moshi.MoshiConverterFactory - - -@Module -@InstallIn(SingletonComponent::class) -object NetworkModule { - - @Provides - fun provideRetrofit(): Retrofit { - val moshi = Moshi.Builder() - .add(KotlinJsonAdapterFactory()).build() - return Retrofit.Builder() - .baseUrl("https://geocod.xyz/api/public/") - .addConverterFactory(MoshiConverterFactory.create(moshi)) - .build() - } - - @Provides - fun provideGeocodApiService(retrofit: Retrofit): GeocoderApiService { - return retrofit.create(GeocoderApiService::class.java) - } -} \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/di/RepositoryModule.kt b/app/src/main/java/com/abdurakhmanov/ridez/di/RepositoryModule.kt index d59ec32..8c8cdfa 100644 --- a/app/src/main/java/com/abdurakhmanov/ridez/di/RepositoryModule.kt +++ b/app/src/main/java/com/abdurakhmanov/ridez/di/RepositoryModule.kt @@ -2,7 +2,6 @@ package com.abdurakhmanov.ridez.di import com.abdurakhmanov.ridez.data.repository.LocationRepository import com.abdurakhmanov.ridez.data.source.local.LocationUpdateDao -import com.abdurakhmanov.ridez.data.source.remote.GeocoderApiService import dagger.Module import dagger.Provides import dagger.hilt.InstallIn @@ -14,9 +13,8 @@ object RepositoryModule { @Provides fun provideLocationRepository( - locationUpdateDao: LocationUpdateDao, - geocoderApiService: GeocoderApiService + locationUpdateDao: LocationUpdateDao ): LocationRepository { - return LocationRepository(locationUpdateDao, geocoderApiService) + return LocationRepository(locationUpdateDao) } } \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/ui/MainActivity.kt b/app/src/main/java/com/abdurakhmanov/ridez/ui/MainActivity.kt index f8a38cb..2803181 100644 --- a/app/src/main/java/com/abdurakhmanov/ridez/ui/MainActivity.kt +++ b/app/src/main/java/com/abdurakhmanov/ridez/ui/MainActivity.kt @@ -5,6 +5,8 @@ import android.annotation.SuppressLint import android.content.Intent import android.content.res.ColorStateList import android.content.res.Configuration +import android.location.Address +import android.location.Geocoder import android.os.Build.VERSION import android.os.Build.VERSION_CODES import android.os.Bundle @@ -37,8 +39,9 @@ import com.mapbox.maps.plugin.locationcomponent.* import com.mapbox.maps.plugin.logo.logo import com.mapbox.maps.plugin.scalebar.scalebar import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.delay +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import java.util.* @AndroidEntryPoint class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback { @@ -195,28 +198,24 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback { } private fun showCurrentAddress() { + val geocoder = Geocoder(applicationContext, Locale("uz_UZ")) lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { viewModel.liveLocation.collect { location -> location?.let { - delay(5000) - viewModel.getCurrentAddress( - GEOCODER_API_KEY, + geocoder.getAddress( location.latitude, location.longitude - ) - } - } - } - } - lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.STARTED) { - viewModel.currentAddress.collect { currentAddress -> - if (currentAddress.isNotBlank()) { - binding.currentAddress.visibility = View.VISIBLE - binding.currentAddress.text = currentAddress - } else { - binding.currentAddress.visibility = View.INVISIBLE + ) { address: Address? -> + lifecycleScope.launch(Dispatchers.Main) { + if (address != null) { + binding.currentAddress.visibility = View.VISIBLE + binding.currentAddress.text = address.getAddressLine(0) + } else { + binding.currentAddress.visibility = View.INVISIBLE + } + } + } } } } diff --git a/app/src/main/java/com/abdurakhmanov/ridez/ui/MainViewModel.kt b/app/src/main/java/com/abdurakhmanov/ridez/ui/MainViewModel.kt index 100f849..aa40c30 100644 --- a/app/src/main/java/com/abdurakhmanov/ridez/ui/MainViewModel.kt +++ b/app/src/main/java/com/abdurakhmanov/ridez/ui/MainViewModel.kt @@ -5,7 +5,6 @@ import androidx.lifecycle.viewModelScope import com.abdurakhmanov.ridez.data.repository.LocationRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import javax.inject.Inject @@ -14,46 +13,15 @@ class MainViewModel @Inject constructor( private val locationRepository: LocationRepository ) : ViewModel() { - /** - * Set true if you want to start getting live location or false to stop - */ - private val _showLiveLocation = MutableStateFlow(true) - val showLiveLocation: StateFlow - get() = _showLiveLocation - - /** - * Use this flow to get live location (updates constantly) - */ - val liveLocation = locationRepository.getLiveLocation(_showLiveLocation) - - /** - * Geocoded address response - */ - private val _currentAddress = MutableStateFlow("") - val currentAddress: StateFlow - get() = _currentAddress + private val showLiveLocation = MutableStateFlow(true) + val liveLocation = locationRepository.getLiveLocation(showLiveLocation) init { - _showLiveLocation.value = true + showLiveLocation.value = true } - /** - * Set the show argument to true if you want to start getting live location or false to stop - */ fun showLiveLocation(show: Boolean) = viewModelScope.launch { - _showLiveLocation.value = show + showLiveLocation.value = show } - /** - * Use this function to geocode location (latitude and longtitude) - */ - suspend fun getCurrentAddress(apiKey: String, latitude: Double, longitude: Double) { - val request = locationRepository.getAddress(apiKey, latitude, longitude) - if (request.isSuccessful) { - val requestBody = request.body() - if (requestBody != null && request.code() == 200) { - _currentAddress.value = requestBody.postaladdress - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/utils/LocationExt.kt b/app/src/main/java/com/abdurakhmanov/ridez/utils/LocationExt.kt new file mode 100644 index 0000000..0f53aff --- /dev/null +++ b/app/src/main/java/com/abdurakhmanov/ridez/utils/LocationExt.kt @@ -0,0 +1,41 @@ +package com.abdurakhmanov.ridez.utils + +import android.content.Context +import android.location.Geocoder +import android.location.LocationManager +import android.os.Build +import androidx.core.location.LocationManagerCompat + +/** + * Checks whether user enabled the location or not. + * + * @return true if location is enabled or false if location is disabled + */ +fun Context.isLocationEnabled(): Boolean { + val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager + return LocationManagerCompat.isLocationEnabled(locationManager) +} + +/** + * Gets current address of the user using latitude and longitude. + * + * @return user's current address + */ +@Suppress("DEPRECATION") +fun Geocoder.getAddress( + latitude: Double, + longitude: Double, + address: (android.location.Address?) -> Unit +) { + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + getFromLocation(latitude, longitude, 1) { address(it.firstOrNull()) } + return + } + + try { + address(getFromLocation(latitude, longitude, 1)!!.firstOrNull()) + } catch (e: Exception) { + address(null) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/abdurakhmanov/ridez/utils/PermissionExt.kt b/app/src/main/java/com/abdurakhmanov/ridez/utils/PermissionExt.kt index 70072d4..6ce7351 100644 --- a/app/src/main/java/com/abdurakhmanov/ridez/utils/PermissionExt.kt +++ b/app/src/main/java/com/abdurakhmanov/ridez/utils/PermissionExt.kt @@ -4,13 +4,11 @@ import android.Manifest import android.content.Context import android.content.Intent import android.content.pm.PackageManager -import android.location.LocationManager import android.net.Uri import android.os.Build import android.provider.Settings import androidx.annotation.RequiresApi import androidx.core.content.ContextCompat -import androidx.core.location.LocationManagerCompat import com.google.android.material.dialog.MaterialAlertDialogBuilder /** @@ -56,16 +54,6 @@ fun String.isPermissionGranted( return false } -/** - * Checks whether user enabled the location or not. - * - * @return true if location is enabled or false if location is disabled - */ -fun Context.isLocationEnabled(): Boolean { - val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager - return LocationManagerCompat.isLocationEnabled(locationManager) -} - /** * Shows explanation dialog about location usage. */