Skip to content

Commit

Permalink
handle app crashes due to no data in database
Browse files Browse the repository at this point in the history
  • Loading branch information
SidharthMudgil committed Aug 9, 2023
1 parent 63bb387 commit 2130678
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface WeatherDao {
suspend fun upsertWeatherData(weatherEntity: WeatherEntity)

@Query("select * from weather_data")
suspend fun getWeatherData(): WeatherEntity
suspend fun getWeatherData(): WeatherEntity?
}
63 changes: 30 additions & 33 deletions app/src/main/java/com/sidharth/mosam/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ class WeatherViewModel @Inject constructor(
context: Context,
latitude: Double,
longitude: Double
): LiveData<WeatherData> {
) {
viewModelScope.launch {
_weatherData.postValue(
getWeatherDataUseCase.execute(context, latitude, longitude)
)
}
return _weatherData
}
}
5 changes: 2 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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" />
Expand All @@ -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" />
Expand Down

0 comments on commit 2130678

Please sign in to comment.