forked from cobeisfresh/CleanArchitecture-Android-Showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject connectivity class in repository, if there is no network acces…
…s pull data from room
- Loading branch information
1 parent
713587c
commit 54f1052
Showing
11 changed files
with
59 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.data" /> | ||
package="com.example.data" > | ||
|
||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
</manifest> |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
package com.example.data.database | ||
|
||
const val WEATHER_TABLE_NAME = "weather" | ||
const val WEATHER_TABLE_NAME = "weather" | ||
const val DB_ENTRY_ERROR = "No entry found in database" |
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package com.example.data.di | ||
|
||
import com.example.data.repository.weather.WeatherRepositoryImpl | ||
import com.example.data.utils.Connectivity | ||
import com.example.domain.repository.WeatherRepository | ||
import org.koin.android.ext.koin.androidContext | ||
import org.koin.dsl.module | ||
|
||
val repositoryModule = module { | ||
factory<WeatherRepository> { WeatherRepositoryImpl(get(), get()) } | ||
factory<WeatherRepository> { WeatherRepositoryImpl(get(), get(), get()) } | ||
factory { Connectivity(androidContext()) } | ||
} |
28 changes: 19 additions & 9 deletions
28
data/src/main/java/com/example/data/repository/weather/WeatherRepositoryImpl.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 |
---|---|---|
@@ -1,23 +1,33 @@ | ||
package com.example.data.repository.weather | ||
|
||
import com.example.data.database.DB_ENTRY_ERROR | ||
import com.example.data.database.dao.WeatherDao | ||
import com.example.data.networking.GENERAL_NETWORK_ERROR | ||
import com.example.data.networking.WeatherApi | ||
import com.example.data.networking.base.onFailure | ||
import com.example.data.networking.base.onSuccess | ||
import com.example.data.utils.Connectivity | ||
import com.example.domain.model.* | ||
import com.example.domain.repository.WeatherRepository | ||
import kotlinx.coroutines.delay | ||
|
||
class WeatherRepositoryImpl(private val weatherApi: WeatherApi, private val weatherDao: WeatherDao) : WeatherRepository { | ||
class WeatherRepositoryImpl(private val weatherApi: WeatherApi, | ||
private val weatherDao: WeatherDao, | ||
private val connectivity: Connectivity) : WeatherRepository { | ||
|
||
override suspend fun getWeatherForLocation(location: String): Result<WeatherInfo> { | ||
weatherApi.getWeatherForLocation(location) | ||
.onSuccess { | ||
val weatherInfo = weatherDao.updateWeatherAndReturn(it.mapToRoomEntity()) | ||
return Success(weatherInfo.mapToDomainModel()) | ||
} | ||
.onFailure { return Failure(it) } | ||
return Failure(HttpError(Throwable(GENERAL_NETWORK_ERROR))) | ||
if (connectivity.hasNetworkAccess()) { | ||
weatherApi.getWeatherForLocation(location) | ||
.onSuccess { | ||
val weatherInfo = weatherDao.updateWeatherAndReturn(it.mapToRoomEntity()) | ||
return Success(weatherInfo.mapToDomainModel()) | ||
} | ||
.onFailure { return Failure(it) } | ||
return Failure(HttpError(Throwable(GENERAL_NETWORK_ERROR))) | ||
} else { | ||
val weatherInfo = weatherDao.getWeatherInfoForCity(location) | ||
@Suppress("SENSELESS_COMPARISON") // room can return null here even though it's not nullable type | ||
return if (weatherInfo != null) Success(weatherInfo.mapToDomainModel()) | ||
else Failure(HttpError(Throwable(DB_ENTRY_ERROR))) | ||
} | ||
} | ||
} |
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,13 @@ | ||
package com.example.data.utils | ||
|
||
import android.content.Context | ||
import android.net.ConnectivityManager | ||
|
||
class Connectivity(private val context: Context) { | ||
|
||
fun hasNetworkAccess(): Boolean { | ||
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | ||
val info = connectivityManager.activeNetworkInfo | ||
return info != null && info.isConnected | ||
} | ||
} |