Skip to content

Commit

Permalink
Inject connectivity class in repository, if there is no network acces…
Browse files Browse the repository at this point in the history
…s pull data from room
  • Loading branch information
LukaKordic committed Jul 30, 2019
1 parent 713587c commit 54f1052
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 27 deletions.
8 changes: 5 additions & 3 deletions app/src/main/java/com/cobeisfresh/template/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ import org.koin.core.logger.Level

class App : Application() {

lateinit var INSTANCE: Application
private set
companion object {
lateinit var instance: Application
private set
}

override fun onCreate() {
super.onCreate()
INSTANCE = this
instance = this

startKoin {
androidContext(this@App)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@ inline fun View.onClick(crossinline onClick: () -> Unit) {
}

fun FragmentActivity.showFragment(fragment: Fragment, @IdRes container: Int, addToBackStack: Boolean = false) {
supportFragmentManager.beginTransaction()
.apply {
if (addToBackStack) {
addToBackStack(fragment.tag)
}
}
supportFragmentManager.beginTransaction().apply {
if (addToBackStack) {
addToBackStack(fragment.tag)
}
}
.replace(container, fragment)
.commitAllowingStateLoss()
}

fun FragmentActivity.goBack(){
fun FragmentActivity.goBack() {
supportFragmentManager.popBackStack()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import com.cobeisfresh.template.R
import com.cobeisfresh.template.common.extensions.showFragment
import com.cobeisfresh.template.ui.weather.view.fragments.WeatherDetailsFragment

/**
* Every activity that holds fragments should name its container "fragmentContainer"
*/

class AppFragmentNavigator : FragmentNavigator {

override fun showWeatherDetails(fragmentActivity: FragmentActivity) =
fragmentActivity.showFragment(WeatherDetailsFragment.newInstance(), R.id.fragmentContainer, true)

}

Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ import androidx.fragment.app.FragmentActivity
interface FragmentNavigator {

fun showWeatherDetails(fragmentActivity: FragmentActivity)

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import com.cobeisfresh.template.ui.base.BaseFragment

class WeatherDetailsFragment : BaseFragment() {

override fun viewReady() {}

override fun getLayout() = R.layout.fragment_weather_details

override fun viewReady() {}

companion object {
fun newInstance() = WeatherDetailsFragment()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.cobeisfresh.template.common.extensions.onClick
import com.cobeisfresh.template.common.extensions.subscribe
import com.cobeisfresh.template.ui.base.BaseFragment
import com.cobeisfresh.template.ui.base.ViewState
import com.cobeisfresh.template.ui.base.ViewState.Status
import com.cobeisfresh.template.ui.base.ViewState.Status.*
import com.cobeisfresh.template.ui.weather.presentation.WeatherViewModel
import com.example.domain.model.WeatherInfo
Expand Down
5 changes: 4 additions & 1 deletion data/src/main/AndroidManifest.xml
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>
3 changes: 2 additions & 1 deletion data/src/main/java/com/example/data/database/DbConstants.kt
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"
5 changes: 4 additions & 1 deletion data/src/main/java/com/example/data/di/RepositoryModule.kt
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()) }
}
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)))
}
}
}
13 changes: 13 additions & 0 deletions data/src/main/java/com/example/data/utils/Connectivity.kt
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
}
}

0 comments on commit 54f1052

Please sign in to comment.