-
Notifications
You must be signed in to change notification settings - Fork 32
#1712 | [DMP 2025] Signature capture #1678
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
Open
ShivangMishra
wants to merge
13
commits into
avniproject:master
Choose a base branch
from
ShivangMishra:dmp-2025-signature-capture
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
125e8a0
impl signature form element
ShivangMishra c72ff90
handling signature data type in FormElementGroup and Observations
ShivangMishra 33b71fb
Merge branch 'avniproject:master' into dmp-2025-signature-capture
ShivangMishra 2b403ec
use primitive value change action for signature form element
ShivangMishra 344fafb
fix incorrect key in action object for signature form element
ShivangMishra 77f5707
add signature type in media path getters
ShivangMishra acc938b
add Signature support for ExpandableMedia
ShivangMishra f04430c
fix signature image preview
ShivangMishra 9a5ae49
remove webview patch (not needed after library upgrade)
ShivangMishra 66d28e8
delete signature image file on clear value
ShivangMishra be4fa7c
add missing translation keys
ShivangMishra 63311c0
fix scroll view interference
ShivangMishra 5406368
address coderabbit nitpicks
ShivangMishra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
packages/openchs-android/src/views/form/formElement/SignatureFormElement.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import PropTypes from "prop-types"; | ||
| import React from "react"; | ||
| import AbstractFormElement from "./AbstractFormElement"; | ||
| import {StyleSheet, View, Image, TouchableNativeFeedback, Alert} from "react-native"; | ||
| import SignatureCanvas from "react-native-signature-canvas"; | ||
| import ValidationErrorMessage from "../ValidationErrorMessage"; | ||
| import FormElementLabelWithDocumentation from "../../common/FormElementLabelWithDocumentation"; | ||
| import Colors from "../../primitives/Colors"; | ||
| import General from "../../../utility/General"; | ||
| import _ from "lodash"; | ||
| import Icon from "react-native-vector-icons/MaterialCommunityIcons"; | ||
| import {ValidationResult} from "openchs-models"; | ||
| import { AlertMessage } from "../../common/AlertMessage"; | ||
| import FileSystem from "../../../model/FileSystem"; | ||
| import fs from 'react-native-fs'; | ||
|
|
||
| class SignatureFormElement extends AbstractFormElement { | ||
| static signatureFileDirectory = FileSystem.getImagesDir(); | ||
| static propTypes = { | ||
| element: PropTypes.object.isRequired, | ||
| actionName: PropTypes.string.isRequired, | ||
| value: PropTypes.object, | ||
| validationResult: PropTypes.object, | ||
| }; | ||
|
|
||
| constructor(props, context) { | ||
| super(props, context); | ||
| this.signatureRef = React.createRef(); | ||
| } | ||
|
|
||
| get signatureFilename() { | ||
| return _.get(this, "props.value.answer"); | ||
| } | ||
|
|
||
| updateValue(signatureValue, validationResult = null) { | ||
| if (General.isNilOrEmpty(signatureValue)) { | ||
| this.onUpdateObservations(null); | ||
| return; | ||
| } | ||
|
|
||
| const [header, base64Data] = signatureValue.split(','); | ||
| const mimeType = header.match(/data:(.*?);/)[1]; | ||
| const ext = mimeType.split('/')[1]; | ||
|
|
||
| const fileName = `${General.randomUUID()}.${ext}`; | ||
| const filePath = `${SignatureFormElement.signatureFileDirectory}/${fileName}`; | ||
|
|
||
| fs.writeFile(filePath, base64Data, 'base64') | ||
| .then(() => { | ||
| this.onUpdateObservations(fileName); | ||
| }) | ||
| .catch((error) => { | ||
| AlertMessage(`Error saving signature: ${error.message}`, "error"); | ||
| }); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| onUpdateObservations(fileName) { | ||
| this.dispatchAction(this.props.actionName, { | ||
| formElement: this.props.element, | ||
| parentFormElement: this.props.parentElement, | ||
| questionGroupIndex: this.props.questionGroupIndex, | ||
| value: fileName, | ||
| }); | ||
| } | ||
|
|
||
| clearAnswer() { | ||
| this.updateValue(null); | ||
| } | ||
|
|
||
| handleSignatureData = (signature) => { | ||
| this.updateValue(signature); | ||
| }; | ||
|
|
||
| handleEmpty = () => { | ||
| this.updateValue(null, ValidationResult.failure(this.props.element.uuid, this.I18n.t("signatureRequired"))); | ||
| }; | ||
|
|
||
| handleClear = () => { | ||
| this.clearAnswer(); | ||
| }; | ||
ShivangMishra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| handleEnd = () => { | ||
| // Don't read signature on end, only when save is clicked | ||
| }; | ||
|
|
||
| render() { | ||
| return ( | ||
| <View style={{marginVertical: 16}}> | ||
| <FormElementLabelWithDocumentation element={this.props.element} /> | ||
| {this.signatureFilename ? ( | ||
| <View | ||
| style={{ | ||
| flexDirection: "row", | ||
| height: 100, | ||
| justifyContent: "space-between", | ||
| paddingHorizontal: 8, | ||
| }} | ||
| > | ||
| <Image | ||
| resizeMode="center" | ||
| style={{ | ||
| flex: 1, | ||
| }} | ||
| source={{uri: `file://${SignatureFormElement.signatureFileDirectory}/${this.signatureFilename}`}} | ||
| /> | ||
| <TouchableNativeFeedback onPress={() => this.clearAnswer()}> | ||
| <Icon name={"backspace"} style={[styles.icon]} /> | ||
| </TouchableNativeFeedback> | ||
| </View> | ||
| ) : ( | ||
| <View style={styles.signatureContainer}> | ||
| <SignatureCanvas | ||
| ref={this.signatureRef} | ||
| onEnd={this.handleEnd} | ||
| onOK={this.handleSignatureData} | ||
| onEmpty={this.handleEmpty} | ||
| onClear={this.handleClear} | ||
| autoClear={false} | ||
| descriptionText={this.I18n.t("signHere")} | ||
| clearText={this.I18n.t("clear")} | ||
| confirmText={this.I18n.t("save")} | ||
| /> | ||
| </View> | ||
| )} | ||
| <View style={styles.lineStyle} /> | ||
| <ValidationErrorMessage validationResult={this.props.validationResult} /> | ||
| </View> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| icon: { | ||
| color: Colors.ActionButtonColor, | ||
| opacity: 0.8, | ||
| alignSelf: "center", | ||
| fontSize: 36, | ||
| }, | ||
| lineStyle: { | ||
| flex: 1, | ||
| borderColor: Colors.BlackBackground, | ||
| borderBottomWidth: StyleSheet.hairlineWidth, | ||
| opacity: 0.1, | ||
| }, | ||
| signatureContainer: { | ||
| flex: 1, | ||
| height: 360, | ||
| marginTop: 8, | ||
| backgroundColor: Colors.InputBackground, | ||
| borderWidth: 1, | ||
| borderColor: Colors.InputBorderNormal, | ||
| borderRadius: 4, | ||
| overflow: "hidden", | ||
| }, | ||
| }); | ||
|
|
||
| export default SignatureFormElement; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.