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

[ANDROAPP-5891] Implement Signature #3475

Merged
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
Expand Up @@ -11,7 +11,6 @@
import org.dhis2.R;
import org.dhis2.commons.featureconfig.ui.FeatureConfigView;
import org.dhis2.databinding.DevelopmentActivityBinding;
import org.dhis2.ui.dialogs.signature.SignatureDialog;
import org.dhis2.usescases.general.ActivityGlobalAbstract;
import org.hisp.dhis.android.core.D2;
import org.hisp.dhis.android.core.D2Manager;
Expand All @@ -25,8 +24,6 @@
import java.io.Writer;
import java.util.List;

import kotlin.Unit;

public class DevelopmentActivity extends ActivityGlobalAbstract {

private int count;
Expand All @@ -41,7 +38,6 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
loadIconsDevTools();
loadCrashControl();
loadFeatureConfig();
loadSignature();
loadConflicts();
}

Expand Down Expand Up @@ -189,13 +185,6 @@ private void loadFeatureConfig() {
});
}

private void loadSignature() {
binding.signature.setOnClickListener(view -> {
new SignatureDialog("Signature", bitmap -> Unit.INSTANCE).show(getSupportFragmentManager(), "Signature");
}
);
}

@Override
public void onBackPressed() {
setResult(RESULT_OK);
Expand Down
16 changes: 0 additions & 16 deletions app/src/main/res/layout/development_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,6 @@
android:layout_gravity="end"
android:text="Features list" />

<TextView
style="@style/TextPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/primaryAlpha"
android:text="Handwrite signature"
android:textColor="@color/colorAccent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/signature"
style="@style/Widget.MaterialComponents.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="Signature" />
<TextView
style="@style/TextPrimary"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions form/src/main/java/org/dhis2/form/model/UiEventType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ enum class UiEventType {
OPEN_FILE,
EMAIL,
PHONE_NUMBER,
SHARE_IMAGE,
}
20 changes: 14 additions & 6 deletions form/src/main/java/org/dhis2/form/ui/FormView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,15 @@ class FormView : Fragment() {
}

Intent.ACTION_VIEW -> {
data = if (!currentValue.value.startsWith("http://") && !currentValue.value.startsWith("https://")) {
Uri.parse("http://${currentValue.value}")
} else {
Uri.parse(currentValue.value)
}
data =
if (!currentValue.value.startsWith("http://") && !currentValue.value.startsWith(
"https://",
)
) {
Uri.parse("http://${currentValue.value}")
} else {
Uri.parse(currentValue.value)
}
}

Intent.ACTION_SEND -> {
Expand All @@ -645,7 +649,10 @@ class FormView : Fragment() {
FormFileProvider.fileProviderAuthority,
File(currentValue.value),
)
setDataAndType(contentUri, requireContext().contentResolver.getType(contentUri))
setDataAndType(
contentUri,
requireContext().contentResolver.getType(contentUri),
)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(Intent.EXTRA_STREAM, contentUri)
}
Expand Down Expand Up @@ -960,6 +967,7 @@ class FormView : Fragment() {
requireContext().getString(R.string.from_gallery) -> {
pickImage.launch(Intent(Intent.ACTION_PICK).apply { type = "image/*" })
}

requireContext().getString(R.string.cancel) -> {
viewModel.getFocusedItemUid()?.let {
viewModel.submitIntent(FormIntent.OnAddImageFinished(it))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.dhis2.form.model.UiEventType.PHONE_NUMBER
import org.dhis2.form.model.UiEventType.QR_CODE
import org.dhis2.form.model.UiEventType.REQUEST_CURRENT_LOCATION
import org.dhis2.form.model.UiEventType.REQUEST_LOCATION_BY_MAP
import org.dhis2.form.model.UiEventType.SHARE_IMAGE
import org.dhis2.form.model.UiEventType.SHOW_DESCRIPTION
import org.dhis2.form.model.UiEventType.SHOW_PICTURE
import org.dhis2.form.model.UiRenderType
Expand Down Expand Up @@ -139,6 +140,11 @@ class UiEventFactoryImpl(
value,
uid,
)
SHARE_IMAGE -> RecyclerViewUiEvents.OpenChooserIntent(
Intent.ACTION_SEND,
fieldUiModel.displayName,
uid,
)
else -> null
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ internal fun FieldProvider(
ProvideInputSignature(
modifier = modifierWithFocus,
fieldUiModel = fieldUiModel,
intentHandler = intentHandler,
uiEventHandler = uiEventHandler,
)
}

Expand All @@ -353,6 +351,7 @@ internal fun FieldProvider(
fieldUiModel = fieldUiModel,
)
}

else -> {
ProvideInputCoordinate(
modifier = modifierWithFocus,
Expand All @@ -375,6 +374,7 @@ internal fun FieldProvider(
resources = resources,
)
}

ValueType.REFERENCE,
ValueType.GEOJSON,
ValueType.USERNAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
package org.dhis2.form.ui.provider.inputfield

import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.platform.LocalContext
import org.dhis2.form.extensions.inputState
import org.dhis2.form.extensions.legend
import org.dhis2.form.extensions.supportingText
import org.dhis2.form.model.FieldUiModel
import org.dhis2.form.model.UiEventType
import org.dhis2.form.ui.event.RecyclerViewUiEvents
import org.dhis2.form.ui.FormView
import org.dhis2.form.ui.intent.FormIntent
import org.hisp.dhis.android.core.arch.helpers.FileResourceDirectoryHelper
import org.hisp.dhis.android.core.common.ValueType
import org.hisp.dhis.mobile.ui.designsystem.component.InputSignature
import java.io.File

@Composable
fun ProvideInputSignature(
modifier: Modifier,
fieldUiModel: FieldUiModel,
intentHandler: (FormIntent) -> Unit,
uiEventHandler: (RecyclerViewUiEvents) -> Unit,
) {
val context = LocalContext.current

val imageBitmap: ImageBitmap? = fieldUiModel.displayName?.let { path ->
File(path)
.takeIf { it.exists() }
?.let { BitmapFactory.decodeFile(it.absolutePath) }
?.asImageBitmap()
}

var uploadState by remember(fieldUiModel) { mutableStateOf(getUploadState(fieldUiModel.displayName, fieldUiModel.isLoadingData)) }

InputSignature(
modifier = modifier,
title = fieldUiModel.label,
state = fieldUiModel.inputState(),
supportingText = fieldUiModel.supportingText(),
legendData = fieldUiModel.legend(),
isRequired = fieldUiModel.mandatory,
uploadState = uploadState,
load = { imageBitmap },
painterFor = imageBitmap?.let {
{
Expand All @@ -53,29 +50,32 @@ fun ProvideInputSignature(
}
},
onDownloadButtonClick = {
uiEventHandler.invoke(RecyclerViewUiEvents.OpenFile(fieldUiModel))
fieldUiModel.invokeUiEvent(UiEventType.OPEN_FILE)
},
onResetButtonClicked = {
fieldUiModel.onClear()
uploadState = getUploadState(fieldUiModel.displayName, false)
intentHandler.invoke(
FormIntent.OnAddImageFinished(
uid = fieldUiModel.uid,
),
)
},
onAddButtonClicked = {
uploadState = getUploadState(fieldUiModel.displayName, true)
fieldUiModel.invokeUiEvent(UiEventType.ADD_SIGNATURE)
},
onShareButtonClick = {
uiEventHandler.invoke(
RecyclerViewUiEvents.OpenChooserIntent(
Intent.ACTION_SEND,
fieldUiModel.displayName,
fieldUiModel.uid,
),
)
fieldUiModel.invokeUiEvent(UiEventType.SHARE_IMAGE)
},
onSaveSignature = {
it.asAndroidBitmap().let {
val file = File(
FileResourceDirectoryHelper.getFileResourceDirectory(context),
FormView.TEMP_FILE,
)
file.outputStream().use { out ->
it.compress(Bitmap.CompressFormat.PNG, 85, out)
out.flush()
}
fieldUiModel.invokeIntent(
FormIntent.OnStoreFile(
fieldUiModel.uid,
file.path,
ValueType.IMAGE,
),
)
}
},
)
}
Loading