Skip to content

Commit

Permalink
Merge pull request #374 from rfcx/feature/bourhan-improvement
Browse files Browse the repository at this point in the history
improvement from feedback
  • Loading branch information
Tooseriuz authored Aug 15, 2023
2 parents 2bdc814 + 430f5d6 commit 3510912
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ android {
defaultConfig {
applicationId 'org.rfcx.incidents'
manifestPlaceholders = [auth0Domain: "@string/auth0_domain", auth0Scheme: "@string/auth0_scheme"]
versionCode 56
versionName '1.1.1'
versionCode 57
versionName '1.1.2'
minSdkVersion 21
targetSdkVersion 33
multiDexEnabled true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class GuardianRegistrationRepositoryImpl(
emit(Result.Loading)
localDb.markSending(registration.guid)
if (env == "production") {
emit(Result.Success(productionEndpoint.register(registration)))
emit(Result.Success(productionEndpoint.registerSuspend(registration)))
localDb.markSent(registration.guid)
} else {
emit(Result.Success(stagingEndpoint.register(registration)))
emit(Result.Success(stagingEndpoint.registerSuspend(registration)))
localDb.markSent(registration.guid)
}
}.catch { e ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ class GuardianRegistrationDb(private val realm: Realm) {
return realm.where(GuardianRegistration::class.java).findAll().toFlow()
}

fun getAllUnsentForWorker(): List<GuardianRegistration> {
var unsent: List<GuardianRegistration> = listOf()
realm.executeTransaction {
val registrations = it.where(GuardianRegistration::class.java)
.notEqualTo(GuardianRegistration.FIELD_SYNC_STATE, SyncState.SENT.value)
.findAll().createSnapshot()
unsent = registrations
}
return unsent
}

fun markUnsent(guid: String) {
mark(guid, SyncState.UNSENT.value)
}
Expand All @@ -34,6 +45,20 @@ class GuardianRegistrationDb(private val realm: Realm) {
mark(guid, SyncState.SENDING.value)
}

fun unsentCount(): Long {
return realm.where(GuardianRegistration::class.java).notEqualTo(GuardianRegistration.FIELD_SYNC_STATE, SyncState.SENT.value).count()
}

fun unlockSending() {
realm.executeTransaction { it ->
val snapshot =
it.where(GuardianRegistration::class.java).equalTo(GuardianRegistration.FIELD_SYNC_STATE, SyncState.SENDING.value).findAll().createSnapshot()
snapshot.forEach {
it.syncState = SyncState.UNSENT.value
}
}
}

private fun mark(guid: String, syncState: Int) {
realm.executeTransaction {
val registration =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.rfcx.incidents.data.remote.guardian.registration

import org.rfcx.incidents.entity.guardian.registration.GuardianRegisterRequest
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface GuardianRegisterProductionEndpoint {
@POST("guardians")
suspend fun register(@Body registration: GuardianRegisterRequest): GuardianRegisterResponse
suspend fun registerSuspend(@Body registration: GuardianRegisterRequest): GuardianRegisterResponse

@POST("guardians")
fun register(@Body registration: GuardianRegisterRequest): Call<GuardianRegisterResponse>
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.rfcx.incidents.data.remote.guardian.registration

import org.rfcx.incidents.entity.guardian.registration.GuardianRegisterRequest
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface GuardianRegisterStagingEndpoint {
@POST("guardians")
suspend fun register(@Body registration: GuardianRegisterRequest): GuardianRegisterResponse
suspend fun registerSuspend(@Body registration: GuardianRegisterRequest): GuardianRegisterResponse

@POST("guardians")
fun register(@Body registration: GuardianRegisterRequest): Call<GuardianRegisterResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import org.rfcx.incidents.data.local.AssetDb
import org.rfcx.incidents.data.local.ResponseDb
import org.rfcx.incidents.data.local.deploy.DeploymentDb
import org.rfcx.incidents.data.local.deploy.DeploymentImageDb
import org.rfcx.incidents.data.local.guardian.GuardianRegistrationDb
import org.rfcx.incidents.data.local.realm.AppRealm
import org.rfcx.incidents.service.deploy.DeploymentSyncWorker
import org.rfcx.incidents.service.deploy.RegistrationSyncWorker
import java.util.concurrent.TimeUnit

/**
Expand Down Expand Up @@ -54,6 +56,13 @@ class ResponseCleanupWorker(context: Context, params: WorkerParameters) : Worker
DeploymentSyncWorker.enqueue()
}

val registrationDb = GuardianRegistrationDb(realm)
val registerUnsent = registrationDb.unsentCount()
registrationDb.unlockSending()
if (registerUnsent > 0) {
RegistrationSyncWorker.enqueue()
}

val imageDb = DeploymentImageDb(realm)
val imageUnsent = imageDb.unsentCount()
imageDb.unlockSending()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.rfcx.incidents.service.deploy

import android.content.Context
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.work.Constraints
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkInfo
import androidx.work.WorkManager
import androidx.work.Worker
import androidx.work.WorkerParameters
import io.realm.Realm
import org.rfcx.incidents.data.local.guardian.GuardianRegistrationDb
import org.rfcx.incidents.data.local.realm.AppRealm
import org.rfcx.incidents.data.remote.common.service.ServiceFactory
import org.rfcx.incidents.entity.guardian.registration.toRequest

class RegistrationSyncWorker(private val context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
val prodService = ServiceFactory.makeGuardianRegisterProductionService(context)
val stagingService = ServiceFactory.makeGuardianRegisterStagingService(context)
val db = GuardianRegistrationDb(Realm.getInstance(AppRealm.configuration()))
val unsent = db.getAllUnsentForWorker()

Log.d(TAG, "doWork: found ${unsent.size} unsent")
var someFailed = false
unsent.forEach {
when (it.env) {
"production" -> {
val result = prodService.register(it.toRequest()).execute()
if (result.isSuccessful) {
db.markSent(it.guid)
} else {
db.markUnsent(it.guid)
someFailed = true
}
}
"staging" -> {
val result = stagingService.register(it.toRequest()).execute()
if (result.isSuccessful) {
db.markSent(it.guid)
} else {
db.markUnsent(it.guid)
someFailed = true
}
}
}
}

return if (someFailed) Result.retry() else Result.success()
}

companion object {
private const val TAG = "RegistrationSyncWorker"
private const val UNIQUE_WORK_KEY = "RegistrationSyncWorkerUniqueKey"

fun enqueue() {
val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
val workRequest = OneTimeWorkRequestBuilder<RegistrationSyncWorker>().setConstraints(constraints).build()
WorkManager.getInstance().enqueueUniqueWork(UNIQUE_WORK_KEY, ExistingWorkPolicy.KEEP, workRequest)
}

fun workInfos(): LiveData<List<WorkInfo>> {
return WorkManager.getInstance().getWorkInfosForUniqueWorkLiveData(UNIQUE_WORK_KEY)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class CommunicationViewModel(
private var needCheckSha1 = false
private var isFirstTime = true
private var currentGuardianSha1 = ""
private var isManual = true

init {
getPrefSha1()
Expand Down Expand Up @@ -227,21 +228,29 @@ class CommunicationViewModel(
getProjectOffTimesUseCase.launch(GetProjectOffTimesParams(preferences.getString(Preferences.SELECTED_PROJECT, ""))).catch {
}.collectLatest { result ->
currentProjectOffTimes = result
if (currentProjectOffTimes.isEmpty()) {
_guardianSatOffTimeEmptyTextState.tryEmit(true)
} else {
_guardianSatOffTimeEmptyTextState.tryEmit(false)
}
determineEmptyProjectOffTimes()
}
}
}

private fun determineEmptyProjectOffTimes() {
if (currentProjectOffTimes.isEmpty() && !isManual) {
_guardianSatOffTimeEmptyTextState.tryEmit(true)
} else {
_guardianSatOffTimeEmptyTextState.tryEmit(false)
}
}

fun onManualClicked() {
isManual = true
_guardianSatTimeOffState.tryEmit(currentGuardianOffTimes)
determineEmptyProjectOffTimes()
}

fun onAutoClicked() {
isManual = false
_guardianSatTimeOffState.tryEmit(currentProjectOffTimes)
determineEmptyProjectOffTimes()
}

fun onNextClicked(plan: GuardianPlan, offTimes: List<TimeRange>? = null, isManual: Boolean = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.rfcx.incidents.entity.guardian.registration.GuardianRegistration
import org.rfcx.incidents.entity.guardian.registration.toSocketFormat
import org.rfcx.incidents.entity.guardian.socket.InstructionCommand
import org.rfcx.incidents.entity.guardian.socket.InstructionType
import org.rfcx.incidents.service.deploy.RegistrationSyncWorker
import org.rfcx.incidents.util.common.StringUtils
import org.rfcx.incidents.util.socket.PingUtils.getGuid
import org.rfcx.incidents.util.socket.PingUtils.isRegistered
Expand Down Expand Up @@ -68,6 +69,7 @@ class GuardianRegisterViewModel(
}
if (waitingForRegistration && it) {
waitingForRegistration = false
RegistrationSyncWorker.enqueue()
}
if (it && !waitingForRegistration) {
_registerTextState.tryEmit("Your Guardian is already registered")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@ package org.rfcx.incidents.view.report.create.image
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import android.view.LayoutInflater
import androidx.core.content.FileProvider
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.opensooq.supernova.gligar.GligarPicker
import org.rfcx.incidents.databinding.ButtomSheetAttachImageLayoutBinding
import org.rfcx.incidents.util.CameraPermissions
import org.rfcx.incidents.util.GalleryPermissions
import org.rfcx.incidents.util.ImageFileUtils
import org.rfcx.incidents.util.ImageUtils
import org.rfcx.incidents.util.ReportUtils
import org.rfcx.incidents.view.base.BaseFragment
import java.io.File

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DeploymentListFragment : Fragment(), DeploymentItemListener, ProjectOnClic

private lateinit var unsyncedAlert: AlertDialog
private var state = DeploymentListState.LIST
private var currentFilter = DeploymentListViewModel.FilterDeployment.ALL
private var currentFilter = DeploymentListViewModel.FilterDeployment.UNSYNCED

override fun onCreateView(
inflater: LayoutInflater,
Expand Down Expand Up @@ -320,8 +320,6 @@ class DeploymentListFragment : Fragment(), DeploymentItemListener, ProjectOnClic
viewModel.addFilter(currentFilter)
}
}
// Start with Map screen
binding.toolbarLayout.changePageButton.performClick()

binding.mapBoxView.onCreate(savedInstanceState)
binding.mapBoxView.setParam(canMove = true, fromDeploymentList = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ class DeploymentListViewModel(
private val _unsyncedAlertState: MutableStateFlow<Int> = MutableStateFlow(0)
val unsyncedAlertState = _unsyncedAlertState.asStateFlow()

private var currentFilter = FilterDeployment.ALL
private var currentFilter = FilterDeployment.UNSYNCED
private var currentAllStreams = listOf<Stream>()
private var currentAllRegistration = listOf<GuardianRegistration>()
private var selectedProjectId = ""

var isLoadingMore = false
private var isMapScreen = true
private var isMapScreen = false

init {
getLocationChanged()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_deployment_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
android:layout_marginStart="@dimen/margin_padding_small"
app:layout_constraintTop_toTopOf="parent"
app:singleSelection="true"
app:checkedChip="@id/allSelectChip">
app:checkedChip="@id/unSyncedSelectChip">

<com.google.android.material.chip.Chip
android:id="@+id/unSyncedSelectChip"
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/res/layout/toolbar_deployment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

<LinearLayout
android:id="@+id/projectTitleLayout"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal"
android:layout_marginEnd="@dimen/margin_padding_normal"
app:layout_constraintStart_toStartOf="parent">
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/projectTitleTextView"
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/res/layout/toolbar_project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

<LinearLayout
android:id="@+id/projectTitleLayout"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="@dimen/margin_padding_normal"
android:gravity="center"
android:orientation="horizontal">
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/projectTitleTextView"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@
<string name="registration">Registration</string>
<string name="deployments">Deployments</string>
<string name="map">Map View</string>
<string name="deploy_new_guardian">Deploy new Guardian</string>
<string name="deploy_new_guardian">Connect to Guardian</string>

<!-- Deployment Detail -->

Expand Down

0 comments on commit 3510912

Please sign in to comment.