diff --git a/app/src/main/java/com/sidharth/mosam/data/local/LocalDataSource.kt b/app/src/main/java/com/sidharth/mosam/data/local/LocalDataSource.kt index 42d7add..569a18d 100644 --- a/app/src/main/java/com/sidharth/mosam/data/local/LocalDataSource.kt +++ b/app/src/main/java/com/sidharth/mosam/data/local/LocalDataSource.kt @@ -1,6 +1,7 @@ package com.sidharth.mosam.data.local import com.sidharth.mosam.data.mapper.WeatherEntityMapper +import com.sidharth.mosam.domain.model.EmptyWeatherData import com.sidharth.mosam.domain.model.WeatherData import javax.inject.Inject @@ -14,8 +15,8 @@ class LocalDataSource @Inject constructor( } suspend fun getWeatherData(): WeatherData { - return WeatherEntityMapper.mapWeatherEntityToWeatherData( - weatherDao.getWeatherData() - ) + return weatherDao.getWeatherData()?.let { + WeatherEntityMapper.mapWeatherEntityToWeatherData(it) + } ?: EmptyWeatherData.instance } } \ No newline at end of file diff --git a/app/src/main/java/com/sidharth/mosam/data/local/WeatherDao.kt b/app/src/main/java/com/sidharth/mosam/data/local/WeatherDao.kt index 9bda68a..b0ffae2 100644 --- a/app/src/main/java/com/sidharth/mosam/data/local/WeatherDao.kt +++ b/app/src/main/java/com/sidharth/mosam/data/local/WeatherDao.kt @@ -10,5 +10,5 @@ interface WeatherDao { suspend fun upsertWeatherData(weatherEntity: WeatherEntity) @Query("select * from weather_data") - suspend fun getWeatherData(): WeatherEntity + suspend fun getWeatherData(): WeatherEntity? } \ No newline at end of file diff --git a/app/src/main/java/com/sidharth/mosam/ui/MainActivity.kt b/app/src/main/java/com/sidharth/mosam/ui/MainActivity.kt index 5eb7b70..9051153 100644 --- a/app/src/main/java/com/sidharth/mosam/ui/MainActivity.kt +++ b/app/src/main/java/com/sidharth/mosam/ui/MainActivity.kt @@ -43,19 +43,36 @@ class MainActivity : AppCompatActivity() { permissions[Manifest.permission.ACCESS_COARSE_LOCATION] ?: false if (fineLocationGranted || coarseLocationGranted) { - bindData() + observeBindWeatherData() } else { finish() } } + override fun onPause() { + super.onPause() + NetworkUtils.stopNetworkCallback(applicationContext) + } + + override fun onDestroy() { + super.onDestroy() + NetworkUtils.stopNetworkCallback(applicationContext) + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) WindowCompat.setDecorFitsSystemWindows(window, false) installSplashScreen() setContentView(activityMainBinding.root) - BaseApplication.instance.appComponent.inject(this) + initDependencyInjection() + getWeatherData() + setupNetworkCallback() + setupTransitionGenerator() + observeBindWeatherData() + } + + private fun getWeatherData() { if (ActivityCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION @@ -65,8 +82,8 @@ class MainActivity : AppCompatActivity() { Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) { - LocationUtils.getCurrentLocation(this)?.let { - weatherViewModel.getWeatherData(this, it.latitude, it.longitude) + LocationUtils.getCurrentLocation(applicationContext)?.let { + weatherViewModel.getWeatherData(applicationContext, it.latitude, it.longitude) } } else { locationPermissionLauncher.launch( @@ -76,42 +93,22 @@ class MainActivity : AppCompatActivity() { ) ) } - - setupNetworkCallback() - setupTransitionGenerator() - bindData() } - private fun setupNetworkCallback() { - NetworkUtils.startNetworkCallback(this, onConnectionAvailable = { - if (ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_COARSE_LOCATION - ) == PackageManager.PERMISSION_GRANTED || - ActivityCompat.checkSelfPermission( - this, - Manifest.permission.ACCESS_COARSE_LOCATION - ) == PackageManager.PERMISSION_GRANTED - ) { - LocationUtils.getCurrentLocation(this)?.let { - weatherViewModel.getWeatherData(this, it.latitude, it.longitude) - } - } - }, onConnectionLost = {}) - } - - override fun onPause() { - super.onPause() - NetworkUtils.stopNetworkCallback(this) + private fun initDependencyInjection() { + BaseApplication.instance.appComponent.inject(this) } - override fun onDestroy() { - super.onDestroy() - NetworkUtils.stopNetworkCallback(this) + private fun setupNetworkCallback() { + NetworkUtils.startNetworkCallback( + context = applicationContext, + onConnectionAvailable = { getWeatherData() }, + onConnectionLost = {} + ) } @SuppressLint("SetTextI18n") - private fun bindData() { + private fun observeBindWeatherData() { weatherViewModel.weatherData.observe(this) { activityMainBinding.bgImage.setImageDrawable( AppCompatResources.getDrawable( diff --git a/app/src/main/java/com/sidharth/mosam/ui/viewmodel/WeatherViewModel.kt b/app/src/main/java/com/sidharth/mosam/ui/viewmodel/WeatherViewModel.kt index abe9dde..d99b10c 100644 --- a/app/src/main/java/com/sidharth/mosam/ui/viewmodel/WeatherViewModel.kt +++ b/app/src/main/java/com/sidharth/mosam/ui/viewmodel/WeatherViewModel.kt @@ -21,12 +21,11 @@ class WeatherViewModel @Inject constructor( context: Context, latitude: Double, longitude: Double - ): LiveData { + ) { viewModelScope.launch { _weatherData.postValue( getWeatherDataUseCase.execute(context, latitude, longitude) ) } - return _weatherData } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8659f04..53aa0ce 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,7 +10,6 @@ android:id="@+id/bg_image" android:layout_width="match_parent" android:layout_height="match_parent" - android:src="@drawable/bg_morning" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" @@ -529,7 +528,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" - android:text="@string/degree" + android:text="@string/speed" android:textColor="@color/mosam_white_trans" android:textFontWeight="500" android:textSize="@dimen/text_size_label" /> @@ -556,7 +555,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:includeFontPadding="false" - android:text="@string/speed" + android:text="@string/degree" android:textColor="@color/mosam_white_trans" android:textFontWeight="500" android:textSize="@dimen/text_size_label" />