Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement functionality to skip login and data sync #3673

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import org.gradle.kotlin.dsl.extra
import java.io.File

import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.InputStreamReader
Expand All @@ -20,6 +19,7 @@ fun readProperties(file: String): Properties {
// Set required FHIR core properties
val requiredFhirProperties =
listOf(
"SKIP_AUTHENTICATION",
"URL",
"FHIR_BASE_URL",
"OAUTH_BASE_URL",
Expand All @@ -36,6 +36,7 @@ requiredFhirProperties.forEach { property ->
project.extra.set(property, localProperties.getProperty(property, when {
property.contains("URL") -> "https://sample.url/fhir/"
property == "OPENSRP_APP_ID" -> """"""""
property == "SKIP_AUTHENTICATION" -> "false"
else -> "sample_$property"
}
))
Expand Down
2 changes: 1 addition & 1 deletion android/quest/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ android {
versionName = BuildConfigs.versionName
multiDexEnabled = true

buildConfigField("boolean", "SKIP_AUTH_CHECK", "false")
buildConfigField("boolean", "SKIP_AUTHENTICATION", "${project.extra["SKIP_AUTHENTICATION"]}")
buildConfigField("String", "FHIR_BASE_URL", """"${project.extra["FHIR_BASE_URL"]}"""")
buildConfigField("String", "OAUTH_BASE_URL", """"${project.extra["OAUTH_BASE_URL"]}"""")
buildConfigField("String", "OAUTH_CLIENT_ID", """"${project.extra["OAUTH_CLIENT_ID"]}"""")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ import org.smartregister.fhircore.engine.util.SharedPreferencesHelper
import org.smartregister.fhircore.engine.util.extension.applyWindowInsetListener
import org.smartregister.fhircore.engine.util.extension.showToast
import org.smartregister.fhircore.quest.BuildConfig
import org.smartregister.fhircore.quest.ui.login.AccountAuthenticator

@AndroidEntryPoint
class AppSettingActivity : AppCompatActivity() {

@Inject lateinit var accountAuthenticator: AccountAuthenticator

@Inject lateinit var sharedPreferencesHelper: SharedPreferencesHelper

@Inject lateinit var dispatcherProvider: DispatcherProvider
Expand Down Expand Up @@ -70,7 +67,7 @@ class AppSettingActivity : AppCompatActivity() {
onApplicationIdChanged(existingAppId)
loadConfigurations(appSettingActivity)
}
} else if (!BuildConfig.OPENSRP_APP_ID.isNullOrEmpty()) {
} else if (BuildConfig.OPENSRP_APP_ID.isNotEmpty()) {
appSettingViewModel.onApplicationIdChanged(BuildConfig.OPENSRP_APP_ID)
appSettingViewModel.fetchConfigurations(appSettingActivity)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.smartregister.fhircore.quest.ui.appsetting

import android.content.Context
import android.content.Intent
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -51,6 +52,7 @@ import org.smartregister.fhircore.engine.util.extension.launchActivityWithNoBack
import org.smartregister.fhircore.engine.util.extension.retrieveCompositionSections
import org.smartregister.fhircore.engine.util.extension.retrieveImplementationGuideDefinitionResources
import org.smartregister.fhircore.quest.ui.login.LoginActivity
import org.smartregister.fhircore.quest.ui.main.AppMainActivity
import retrofit2.HttpException
import timber.log.Timber

Expand Down Expand Up @@ -229,7 +231,12 @@ constructor(
showProgressBar.postValue(false)
if (loadConfigSuccessful) {
sharedPreferencesHelper.write(SharedPreferenceKey.APP_ID.name, thisAppId)
context.getActivity()?.launchActivityWithNoBackStackHistory<LoginActivity>()
val activity = context.getActivity()
when {
org.smartregister.fhircore.quest.BuildConfig.SKIP_AUTHENTICATION ->
activity?.startActivity(Intent(context, AppMainActivity::class.java))
else -> activity?.launchActivityWithNoBackStackHistory<LoginActivity>()
}
} else {
_error.postValue(context.getString(R.string.application_not_supported, thisAppId))
}
Expand Down Expand Up @@ -288,9 +295,4 @@ constructor(
}

@VisibleForTesting fun isNonProxy(): Boolean = _isNonProxy

@VisibleForTesting
fun setNonProxy(nonProxy: Boolean) {
_isNonProxy = nonProxy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.annotation.VisibleForTesting
import androidx.compose.material.ExperimentalMaterialApi
import androidx.core.os.bundleOf
import androidx.lifecycle.viewModelScope
import androidx.work.WorkManager
Expand Down Expand Up @@ -114,7 +113,6 @@ open class LoginActivity : BaseMultiLanguageActivity() {

@VisibleForTesting open fun deviceOnline() = isDeviceOnline()

@OptIn(ExperimentalMaterialApi::class)
fun navigateToHome() {
startActivity(Intent(this, AppMainActivity::class.java))
// Initialize P2P after login only when username is provided then finish activity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.material.ExperimentalMaterialApi
import androidx.lifecycle.lifecycleScope
import androidx.navigation.findNavController
import androidx.navigation.fragment.NavHostFragment
Expand Down Expand Up @@ -73,7 +72,6 @@ import org.smartregister.fhircore.quest.ui.shared.models.QuestionnaireSubmission
import timber.log.Timber

@AndroidEntryPoint
@ExperimentalMaterialApi
open class AppMainActivity : BaseMultiLanguageActivity(), QuestionnaireHandler, OnSyncListener {

@Inject lateinit var syncListenerManager: SyncListenerManager
Expand Down Expand Up @@ -134,14 +132,14 @@ open class AppMainActivity : BaseMultiLanguageActivity(), QuestionnaireHandler,
lifecycleScope.launch(dispatcherProvider.main()) {
val navController =
(supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment).navController
val launcherType =
appMainViewModel.applicationConfiguration.navigationStartDestination.launcherType

val graph =
withContext(dispatcherProvider.io()) {
navController.navInflater.inflate(R.navigation.application_nav_graph).apply {
val startDestination =
when (
appMainViewModel.applicationConfiguration.navigationStartDestination.launcherType
) {
when (launcherType) {
LauncherType.MAP -> R.id.geoWidgetLauncherFragment
LauncherType.REGISTER -> R.id.registerFragment
}
Expand All @@ -152,7 +150,7 @@ open class AppMainActivity : BaseMultiLanguageActivity(), QuestionnaireHandler,
appMainViewModel.run {
navController.setGraph(graph, getStartDestinationArgs())
retrieveAppMainUiState()
withContext(dispatcherProvider.io()) { schedulePeriodicJobs(this@AppMainActivity) }
schedulePeriodicJobs(this@AppMainActivity)
}

setupLocationServices()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import org.smartregister.fhircore.engine.util.extension.retrieveRelatedEntitySyn
import org.smartregister.fhircore.engine.util.extension.setAppLocale
import org.smartregister.fhircore.engine.util.extension.showToast
import org.smartregister.fhircore.engine.util.extension.tryParse
import org.smartregister.fhircore.quest.BuildConfig
import org.smartregister.fhircore.quest.navigation.MainNavigationScreen
import org.smartregister.fhircore.quest.navigation.NavigationArg
import org.smartregister.fhircore.quest.ui.report.measure.worker.MeasureReportMonthPeriodWorker
Expand Down Expand Up @@ -319,12 +320,6 @@ constructor(
fun retrieveLastSyncTimestamp(): String? =
sharedPreferencesHelper.read(SharedPreferenceKey.LAST_SYNC_TIMESTAMP.name, null)

fun schedulePeriodicSync() {
viewModelScope.launch {
syncBroadcaster.schedulePeriodicSync(applicationConfiguration.syncInterval)
}
}

fun getStartDestinationArgs(): Bundle {
val startDestinationConfig = applicationConfiguration.navigationStartDestination

Expand Down Expand Up @@ -421,27 +416,30 @@ constructor(
private fun getSyncProgress(completed: Int, total: Int) =
completed * 100 / if (total > 0) total else 1

suspend fun schedulePeriodicJobs(context: Context) {
if (context.isDeviceOnline()) {
// Do not schedule sync until location selected when strategy is RelatedEntityLocation
// Use applicationConfiguration.usePractitionerAssignedLocationOnSync to identify
// if we need to trigger sync based on assigned locations or not
if (applicationConfiguration.syncStrategy.contains(SyncStrategy.RelatedEntityLocation)) {
if (
applicationConfiguration.usePractitionerAssignedLocationOnSync ||
context
.retrieveRelatedEntitySyncLocationState(MultiSelectViewAction.SYNC_DATA)
.isNotEmpty()
) {
schedulePeriodicSync()
}
} else {
schedulePeriodicSync()
}
} else {
with(context) {
withContext(dispatcherProvider.main()) {
showToast(getString(R.string.sync_failed), Toast.LENGTH_LONG)
fun schedulePeriodicJobs(context: Context) {
viewModelScope.launch {
if (!BuildConfig.SKIP_AUTHENTICATION) {
if (context.isDeviceOnline()) {
// Do not schedule sync until location selected when strategy is RelatedEntityLocation
// Use applicationConfiguration.usePractitionerAssignedLocationOnSync to identify
// if we need to trigger sync based on assigned locations or not
when {
applicationConfiguration.syncStrategy.contains(SyncStrategy.RelatedEntityLocation) -> {
if (
applicationConfiguration.usePractitionerAssignedLocationOnSync ||
context
.retrieveRelatedEntitySyncLocationState(MultiSelectViewAction.SYNC_DATA)
.isNotEmpty()
) {
syncBroadcaster.schedulePeriodicSync(applicationConfiguration.syncInterval)
}
}
else -> syncBroadcaster.schedulePeriodicSync(applicationConfiguration.syncInterval)
}
} else {
withContext(dispatcherProvider.main()) {
context.showToast(context.getString(R.string.sync_failed), Toast.LENGTH_LONG)
}
}
}
}
Expand All @@ -468,11 +466,13 @@ constructor(
initialDelay = INITIAL_DELAY,
)

schedulePeriodically<CustomSyncWorker>(
workId = CustomSyncWorker.WORK_ID,
repeatInterval = applicationConfiguration.syncInterval,
initialDelay = 0,
)
if (!BuildConfig.SKIP_AUTHENTICATION) {
schedulePeriodically<CustomSyncWorker>(
workId = CustomSyncWorker.WORK_ID,
repeatInterval = applicationConfiguration.syncInterval,
initialDelay = 0,
)
}

measureReportConfigurations.forEach { measureReportConfig ->
measureReportConfig.scheduledGenerationDuration?.let { scheduledGenerationDuration ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import org.smartregister.fhircore.engine.ui.theme.SyncBarBackgroundColor
import org.smartregister.fhircore.engine.ui.theme.WarningColor
import org.smartregister.fhircore.engine.util.annotation.PreviewWithBackgroundExcludeGenerated
import org.smartregister.fhircore.engine.util.extension.appVersion
import org.smartregister.fhircore.quest.BuildConfig
import org.smartregister.fhircore.quest.R
import org.smartregister.fhircore.quest.ui.main.AppMainEvent
import org.smartregister.fhircore.quest.ui.main.AppMainUiState
Expand Down Expand Up @@ -150,14 +151,16 @@ fun AppDrawer(
}
},
bottomBar = { // Display bottom section of the nav (sync)
NavBottomSection(
appUiState = appUiState,
appDrawerUIState = appDrawerUIState,
unSyncedResourceCount = unSyncedResourceCount,
onSideMenuClick = onSideMenuClick,
openDrawer = openDrawer,
decodeImage = decodeImage,
)
if (!BuildConfig.SKIP_AUTHENTICATION) {
NavBottomSection(
appUiState = appUiState,
appDrawerUIState = appDrawerUIState,
unSyncedResourceCount = unSyncedResourceCount,
onSideMenuClick = onSideMenuClick,
openDrawer = openDrawer,
decodeImage = decodeImage,
)
}
},
) { innerPadding ->
Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,8 @@ import android.net.Uri
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.material.ExperimentalMaterialApi
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.smartregister.fhircore.engine.p2p.dao.P2PReceiverTransferDao
import org.smartregister.fhircore.engine.p2p.dao.P2PSenderTransferDao
import org.smartregister.fhircore.engine.ui.base.BaseMultiLanguageActivity
Expand Down Expand Up @@ -81,26 +77,20 @@ class PinLoginActivity : BaseMultiLanguageActivity() {
setContent { AppTheme { PinLoginScreen(pinViewModel) } }
}

@OptIn(ExperimentalMaterialApi::class)
private fun navigateToHome() {
startActivity(Intent(this, AppMainActivity::class.java))

lifecycleScope.launch {
// Initialize P2P only when username is provided then launch main activity
val username = secureSharedPreference.retrieveSessionUsername()
if (!username.isNullOrEmpty()) {
withContext(dispatcherProvider.main()) {
P2PLibrary.init(
P2PLibrary.Options(
context = applicationContext,
dbPassphrase = username,
username = username,
senderTransferDao = p2pSenderTransferDao,
receiverTransferDao = p2pReceiverTransferDao,
),
)
}
}
// Initialize P2P only when username is provided then launch main activity
val username = secureSharedPreference.retrieveSessionUsername()
if (!username.isNullOrEmpty()) {
P2PLibrary.init(
P2PLibrary.Options(
context = applicationContext,
dbPassphrase = username,
username = username,
senderTransferDao = p2pSenderTransferDao,
receiverTransferDao = p2pReceiverTransferDao,
),
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import org.smartregister.fhircore.engine.ui.components.register.LoaderDialog
import org.smartregister.fhircore.engine.ui.components.register.RegisterHeader
import org.smartregister.fhircore.engine.ui.theme.AppTheme
import org.smartregister.fhircore.engine.util.annotation.PreviewWithBackgroundExcludeGenerated
import org.smartregister.fhircore.quest.BuildConfig
import org.smartregister.fhircore.quest.event.ToolbarClickEvent
import org.smartregister.fhircore.quest.ui.main.AppMainEvent
import org.smartregister.fhircore.quest.ui.main.components.TopScreenSection
Expand Down Expand Up @@ -170,7 +171,7 @@ fun RegisterScreen(
},
) { innerPadding ->
Box(modifier = modifier.padding(innerPadding)) {
if (registerUiState.isFirstTimeSync) {
if (!BuildConfig.SKIP_AUTHENTICATION && registerUiState.isFirstTimeSync) {
LoaderDialog(
modifier = modifier.testTag(FIRST_TIME_SYNC_DIALOG),
percentageProgressFlow = flowOf(appDrawerUIState.percentageProgress ?: 0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class UserSettingFragment : Fragment(), OnSyncListener {
showProgressIndicatorFlow = userSettingViewModel.showProgressIndicatorFlow,
dataMigrationVersion = userSettingViewModel.retrieveDataMigrationVersion(),
enableManualSync =
userSettingViewModel.enableMenuOption(SettingsOptions.MANUAL_SYNC),
!org.smartregister.fhircore.quest.BuildConfig.SKIP_AUTHENTICATION &&
userSettingViewModel.enableMenuOption(SettingsOptions.MANUAL_SYNC),
allowSwitchingLanguages = userSettingViewModel.allowSwitchingLanguages(),
showDatabaseResetConfirmation =
userSettingViewModel.enableMenuOption(SettingsOptions.RESET_DATA) &&
Expand Down
Loading