Skip to content

Implement Clear Data Activity #2797

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

Draft
wants to merge 17 commits 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
7 changes: 7 additions & 0 deletions android/quest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
android:allowBackup="false"
android:enableOnBackInvokedCallback="true"
android:hardwareAccelerated="true"
android:hasFragileUserData="true"
android:icon="@drawable/ic_launcher"
android:installLocation="internalOnly"
android:label="@string/app_name"
android:largeHeap="true"
android:manageSpaceActivity="org.smartregister.fhircore.quest.ui.cleardata.ClearDataActivity"
android:supportsRtl="true"
android:theme="@style/AppTheme.NoActionBar"
android:usesCleartextTraffic="false"
Expand All @@ -28,6 +30,7 @@
android:dataExtractionRules="@xml/data_extraction_rules"
tools:ignore="UnusedAttribute,LockedOrientationActivity"
tools:replace="android:allowBackup,android:theme">

<profileable
android:shell="true"
android:enabled="true"
Expand Down Expand Up @@ -74,6 +77,10 @@
android:theme="@style/AppTheme"
tools:replace="exported" />

<activity
android:name="org.smartregister.fhircore.quest.ui.cleardata.ClearDataActivity"
android:exported="false" />

<service
android:name=".ui.login.AuthAndroidService"
android:enabled="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.ui.cleardata

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import org.smartregister.fhircore.engine.ui.base.BaseMultiLanguageActivity
import org.smartregister.fhircore.engine.ui.theme.AppTheme
import org.smartregister.fhircore.quest.BuildConfig

@AndroidEntryPoint
class ClearDataActivity : BaseMultiLanguageActivity() {

Check warning on line 30 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L30

Added line #L30 was not covered by tests

private val viewModel by viewModels<ClearDataViewModel>()

Check warning on line 32 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L32

Added line #L32 was not covered by tests

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Check warning on line 35 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L35

Added line #L35 was not covered by tests

lifecycleScope.launch {
val unsyncedResources = viewModel.getUnsyncedResourceCount()
val appName = viewModel.getAppName()

Check warning on line 39 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L37-L39

Added lines #L37 - L39 were not covered by tests

setContent {

Check warning on line 41 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L41

Added line #L41 was not covered by tests
AppTheme {
ClearDataScreen(
viewModel = viewModel,
unsyncedResourceCount = unsyncedResources,
appName = appName,

Check warning on line 46 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L44-L46

Added lines #L44 - L46 were not covered by tests
isDebug = BuildConfig.DEBUG,
// onSyncData = { viewModel.onEvent(ClearDataEvent.SyncData) },
onDeleteData = { viewModel.clearAppData(this@ClearDataActivity) },
)

Check warning on line 50 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataActivity.kt#L49-L50

Added lines #L49 - L50 were not covered by tests
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.ui.cleardata

import android.content.Context

sealed class ClearDataEvent {
data class SyncData(val context: Context) : ClearDataEvent()

data class ClearAppData(val context: Context) : ClearDataEvent()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
* Copyright 2021-2024 Ona Systems, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.smartregister.fhircore.quest.ui.cleardata

import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.smartregister.fhircore.engine.util.annotation.PreviewWithBackgroundExcludeGenerated
import org.smartregister.fhircore.quest.R
import org.smartregister.fhircore.quest.ui.login.APP_LOGO_TAG

@Composable
fun ClearDataScreen(
viewModel: ClearDataViewModel,
unsyncedResourceCount: Int = 0,
appName: String,
isDebug: Boolean = false,
onSyncData: () -> Unit = {},
onDeleteData: () -> Unit = {},
) {
val dataCleared = viewModel.dataCleared.collectAsState()
val isClearing by remember { mutableStateOf(false) }
val context = LocalContext.current

Check warning on line 68 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L66-L68

Added lines #L66 - L68 were not covered by tests

LaunchedEffect(dataCleared.value) {
if (dataCleared.value) {
Toast.makeText(context, "$appName's data has been cleared.", Toast.LENGTH_SHORT).show()

Check warning on line 72 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L72

Added line #L72 was not covered by tests
}
}

ClearData(
unsyncedResourceCount,
appName,

Check warning on line 78 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L77-L78

Added lines #L77 - L78 were not covered by tests
isDebug,
onSyncData,
onDeleteData,
isClearing,

Check warning on line 82 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L81-L82

Added lines #L81 - L82 were not covered by tests
)
}

@Composable
fun ClearData(
unsyncedResourceCount: Int,
appName: String,
isDebug: Boolean,
onSyncData: () -> Unit,
onDeleteData: () -> Unit,
isClearing: Boolean = false,
) {
val hasUnsyncedData = unsyncedResourceCount >= 1

Scaffold(
topBar = {

Check warning on line 98 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L98

Added line #L98 was not covered by tests
TopAppBar(
title = { Text(text = stringResource(R.string.clear_data_title, appName)) },
)
},
) { paddingValues ->

Check warning on line 103 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L101-L103

Added lines #L101 - L103 were not covered by tests
Column(
modifier = Modifier.fillMaxSize().padding(paddingValues).padding(16.dp),
verticalArrangement = Arrangement.SpaceBetween,

Check warning on line 106 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L105-L106

Added lines #L105 - L106 were not covered by tests
) {
Row(
modifier = Modifier.fillMaxWidth().padding(bottom = 16.dp),
horizontalArrangement = Arrangement.Center,

Check warning on line 110 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L108-L110

Added lines #L108 - L110 were not covered by tests
// verticalAlignment = Alignment.Top,
) {
Image(
painter = painterResource(R.drawable.ic_app_logo),

Check warning on line 114 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L113-L114

Added lines #L113 - L114 were not covered by tests
contentDescription =
stringResource(id = org.smartregister.fhircore.engine.R.string.app_logo),
modifier = Modifier.requiredHeight(120.dp).requiredWidth(140.dp).testTag(APP_LOGO_TAG),

Check warning on line 117 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L116-L117

Added lines #L116 - L117 were not covered by tests
)
}
// Spacer(modifier = Modifier.height(20.dp))
Text(

Check warning on line 121 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L121

Added line #L121 was not covered by tests
text =
if (!hasUnsyncedData) {
stringResource(R.string.clear_data_all_data_synced)
} else {
stringResource(
R.string.clear_data_unsynced_resource_count,
unsyncedResourceCount,

Check warning on line 128 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L124-L128

Added lines #L124 - L128 were not covered by tests
)
},
style =
MaterialTheme.typography.h6.copy(
fontSize = 22.sp,
fontWeight = FontWeight.Bold,
color = MaterialTheme.colors.onSurface,

Check warning on line 135 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L132-L135

Added lines #L132 - L135 were not covered by tests
),
)

if (hasUnsyncedData) {
Spacer(modifier = Modifier.height(24.dp))
Text(

Check warning on line 141 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L140-L141

Added lines #L140 - L141 were not covered by tests
text =
stringResource(
R.string.clear_data_data_loss_warning,
appName,
appName,

Check warning on line 146 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L143-L146

Added lines #L143 - L146 were not covered by tests
),
fontSize = 16.sp,

Check warning on line 148 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L148

Added line #L148 was not covered by tests
style =
MaterialTheme.typography.h6.copy(
fontSize = 18.sp,
color = MaterialTheme.colors.onSurface,

Check warning on line 152 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L150-L152

Added lines #L150 - L152 were not covered by tests
),
)
}

Spacer(modifier = Modifier.weight(1f))

Check warning on line 157 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L157

Added line #L157 was not covered by tests

Button(
onClick = onSyncData,
enabled = hasUnsyncedData,
shape = RoundedCornerShape(8.dp),
modifier = Modifier.fillMaxWidth().height(50.dp).padding(horizontal = 16.dp),
) {
Text(
text =
stringResource(
R.string.clear_data_sync,
appName,
),
)
}

Spacer(modifier = Modifier.height(12.dp))

Button(
onClick = onDeleteData,

Check warning on line 177 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L176-L177

Added lines #L176 - L177 were not covered by tests
enabled = !hasUnsyncedData || isDebug,
shape = RoundedCornerShape(8.dp),
modifier = Modifier.fillMaxWidth().height(50.dp).padding(horizontal = 16.dp),
) {

Check warning on line 181 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L179-L181

Added lines #L179 - L181 were not covered by tests
Text(
text =
stringResource(
R.string.clear_data_delete_button_text,
appName,

Check warning on line 186 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L184-L186

Added lines #L184 - L186 were not covered by tests
),
)

Check warning on line 188 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L188

Added line #L188 was not covered by tests
}

if (isClearing) {
Spacer(modifier = Modifier.height(24.dp))
CircularProgressIndicator(
modifier = Modifier.padding(top = 16.dp).size(48.dp),

Check warning on line 194 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L192-L194

Added lines #L192 - L194 were not covered by tests
)
}
}
}
}

@Composable
@PreviewWithBackgroundExcludeGenerated
private fun ClearDataScreenSyncedPreview() {
ClearData(
unsyncedResourceCount = 0,
appName = "OpenSRP 2",
isDebug = false,
onSyncData = {},
onDeleteData = {},

Check warning on line 209 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L209

Added line #L209 was not covered by tests
)
}

@Composable
@PreviewWithBackgroundExcludeGenerated
private fun ClearDataScreenUnsyncedPreview() {
ClearData(
unsyncedResourceCount = 77,
appName = "OpenSRP 2",
isDebug = true,
onSyncData = {},
onDeleteData = {},

Check warning on line 221 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/cleardata/ClearDataScreen.kt#L221

Added line #L221 was not covered by tests
)
}
Loading
Loading