Skip to content

Commit

Permalink
Merge pull request #336 from rfcx/bug/fix-cached-endpoint
Browse files Browse the repository at this point in the history
Add internet connection even cached endpoint is reached timeout
  • Loading branch information
Tooseriuz authored Apr 20, 2023
2 parents 09c359a + f049ea8 commit 3004b74
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ android {
defaultConfig {
applicationId 'org.rfcx.incidents'
manifestPlaceholders = [auth0Domain: "@string/auth0_domain", auth0Scheme: "@string/auth0_scheme"]
versionCode 51
versionName '1.0.13'
versionCode 52
versionName '1.0.14'
minSdkVersion 19
targetSdkVersion 31
multiDexEnabled true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package org.rfcx.incidents.data

import android.os.Looper
import android.util.Log
import io.reactivex.Single
import org.rfcx.incidents.data.interfaces.ProjectsRepository
import org.rfcx.incidents.data.local.CachedEndpointDb
import org.rfcx.incidents.data.local.ProjectDb
import org.rfcx.incidents.data.remote.project.ProjectsEndpoint
import org.rfcx.incidents.domain.executor.PostExecutionThread
import org.rfcx.incidents.entity.stream.Project
import org.rfcx.incidents.util.ConnectivityUtils

class ProjectsRepositoryImp(
private val endpoint: ProjectsEndpoint,
private val projectDb: ProjectDb,
private val cachedEndpointDb: CachedEndpointDb,
private val connectivityUtils: ConnectivityUtils,
private val postExecutionThread: PostExecutionThread
) : ProjectsRepository {

override fun getProjects(forceRefresh: Boolean): Single<List<Project>> {
if (forceRefresh || !cachedEndpointDb.hasCachedEndpoint("GetProjects")) {
Log.d("ProjectsRepo", "API")
if (forceRefresh || !cachedEndpointDb.hasCachedEndpoint("GetProjects") && connectivityUtils.isNetworkAvailable()) {
return refreshFromAPI()
}
Log.d("ProjectsRepo", "DB")
return getFromLocalDB()
}

Expand All @@ -31,9 +29,7 @@ class ProjectsRepositoryImp(
}

private fun refreshFromAPI(): Single<List<Project>> {
Log.d("ProjectsRepo", "OUTSIDE: " + if (Looper.myLooper() == Looper.getMainLooper()) "MAIN THREAD" else "NOT MAIN!")
return endpoint.getProjects().observeOn(postExecutionThread.scheduler).flatMap { rawProjects ->
Log.d("ProjectsRepo", "INSIDE: " + if (Looper.myLooper() == Looper.getMainLooper()) "MAIN THREAD" else "NOT MAIN!")
rawProjects.forEach {
projectDb.insertOrUpdate(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ import org.rfcx.incidents.data.remote.streams.toStream
import org.rfcx.incidents.domain.GetStreamsParams
import org.rfcx.incidents.domain.executor.PostExecutionThread
import org.rfcx.incidents.entity.stream.Stream
import org.rfcx.incidents.util.ConnectivityUtils

class StreamsRepositoryImp(
private val endpoint: Endpoint,
private val streamDb: StreamDb,
private val eventDb: EventDb,
private val cachedEndpointDb: CachedEndpointDb,
private val connectivityUtils: ConnectivityUtils,
private val postExecutionThread: PostExecutionThread
) : StreamsRepository {
override fun get(params: GetStreamsParams): Single<List<Stream>> {
if (params.streamRefresh) {
if (params.streamRefresh && connectivityUtils.isNetworkAvailable()) {
return refreshFromAPI(params.projectId, params.offset, true)
}
if (params.forceRefresh || !cachedEndpointDb.hasCachedEndpoint(cacheKey(params.projectId))) {
if (params.forceRefresh || !cachedEndpointDb.hasCachedEndpoint(cacheKey(params.projectId)) && connectivityUtils.isNetworkAvailable()) {
return refreshFromAPI(params.projectId, params.offset)
}
return getFromLocalDB(params.projectId)
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/org/rfcx/incidents/domain/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.rfcx.incidents.data.remote.common.CredentialVerifier
import org.rfcx.incidents.data.remote.common.service.ServiceFactory
import org.rfcx.incidents.domain.executor.PostExecutionThread
import org.rfcx.incidents.domain.executor.ThreadExecutor
import org.rfcx.incidents.util.ConnectivityUtils
import org.rfcx.incidents.view.UiThread

object DataModule {
Expand All @@ -50,10 +51,10 @@ object DataModule {
factory { JobExecutor() } bind ThreadExecutor::class
factory { UiThread() } bind PostExecutionThread::class

single { ProjectsRepositoryImp(get(), get(), get(), get()) } bind ProjectsRepository::class
single { ProjectsRepositoryImp(get(), get(), get(), get(), get()) } bind ProjectsRepository::class
single { GetProjectsUseCase(get(), get(), get()) }

single { StreamsRepositoryImp(get(), get(), get(), get(), get()) } bind StreamsRepository::class
single { StreamsRepositoryImp(get(), get(), get(), get(), get(), get()) } bind StreamsRepository::class
single { GetStreamsUseCase(get(), get(), get()) }

single { EventsRepositoryImpl(get()) } bind EventsRepository::class
Expand Down Expand Up @@ -82,6 +83,8 @@ object DataModule {

single { MediaRepositoryImp(get()) } bind MediaRepository::class
single { MediaUseCase(get(), get(), get()) }

single { ConnectivityUtils(androidContext()) }
}

val remoteModule = module {
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/org/rfcx/incidents/util/ConnectivityUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.rfcx.incidents.util

import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build

class ConnectivityUtils(private val context: Context) {

fun isNetworkAvailable(): Boolean {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val capabilities = cm.getNetworkCapabilities(cm.activeNetwork)
capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
} else {
val activeNetworkInfo = cm.activeNetworkInfo
activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting
}
}
}
6 changes: 1 addition & 5 deletions releaseDetail/whatsnew-en-US
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
- Bug fixes
- firebase messaging crashed fixed (newer version lib)
- app crashed when open notification from bad connectivity
- app crashed when preference is not initialized
- app crashed when event start time is bigger than end time
- sites are disappear when refresh with poor connection
- Cannot load project and alerts when offline

0 comments on commit 3004b74

Please sign in to comment.