Skip to content
Snippets Groups Projects
Commit 345c9a39 authored by vjrj's avatar vjrj
Browse files

Correct edit of card names

parent b4b4e242
No related branches found
No related tags found
No related merge requests found
...@@ -16,205 +16,195 @@ class CardNameEditable extends StatefulWidget { ...@@ -16,205 +16,195 @@ class CardNameEditable extends StatefulWidget {
class _CardNameEditableState extends State<CardNameEditable> { class _CardNameEditableState extends State<CardNameEditable> {
bool _isEditingText = false; bool _isEditingText = false;
final TextEditingController _editingController = TextEditingController(); final TextEditingController _controller = TextEditingController();
late String currentText; late String currentText;
late String defValue; final String defValue = tr('your_name_here');
// late FocusNode myFocusNode;
final String pubKey = SharedPreferencesHelper().getPubKey(); final String pubKey = SharedPreferencesHelper().getPubKey();
String _previousValue = ''; String _previousValue = '';
bool _isSubmitting = false;
@override @override
void initState() { void initState() {
super.initState();
defValue = tr('your_name_here');
currentText = defValue; currentText = defValue;
// myFocusNode = FocusNode(); super.initState();
_initValue();
} }
Future<void> _initValue() async { Future<String> _initValue() async {
final String localUsername = SharedPreferencesHelper().getName(); final String localUsername = SharedPreferencesHelper().getName();
try { try {
final String? name = await getCesiumPlusUser(pubKey); String? name = await getCesiumPlusUser(pubKey);
logger( logger(
'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue, retrieved_name: $name'); 'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue, retrieved_name: $name');
if (name != null) { if (name != null) {
_editingController.text = name; name = name.replaceAll(userNameSuffix, '');
_controller.text = name;
currentText = name; currentText = name;
SharedPreferencesHelper().setName(name: name); SharedPreferencesHelper().setName(name: name);
} else { } else {
_editingController.text = ''; _controller.text = '';
currentText = defValue; currentText = defValue;
SharedPreferencesHelper().setName(name: ''); SharedPreferencesHelper().setName(name: '');
} }
} catch (e) { } catch (e) {
logger(e); logger(e);
_editingController.text = localUsername; _controller.text = localUsername;
currentText = localUsername; currentText = localUsername;
} }
_previousValue = _editingController.text; _previousValue = _controller.text;
_controller.selection = TextSelection.fromPosition(
TextPosition(offset: _controller.text.length));
logger( logger(
'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue'); 'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue');
return currentText;
} }
@override @override
void dispose() { void dispose() {
_editingController.dispose(); _controller.dispose();
super.dispose(); super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final bool isSet = currentText.isNotEmpty && currentText != defValue; return FutureBuilder<String>(
return _isEditingText future: _initValue(),
? SizedBox( builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
width: 150.0, return _isEditingText
child: SizedBox( ? SizedBox(
height: 40.0, width: 150.0,
child: TextField( child: SizedBox(
// focusNode: myFocusNode, height: 40.0,
decoration: InputDecoration( child: TextField(
contentPadding: const EdgeInsets.symmetric( // focusNode: myFocusNode,
vertical: 5.0, horizontal: 7.0), decoration: InputDecoration(
filled: true, contentPadding: const EdgeInsets.symmetric(
fillColor: Colors.white, vertical: 5.0, horizontal: 7.0),
enabledBorder: const OutlineInputBorder( filled: true,
borderSide: BorderSide(color: Colors.grey), fillColor: Colors.white,
), enabledBorder: const OutlineInputBorder(
focusedBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.grey),
borderSide: BorderSide(width: 2.0),
),
suffixIcon: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
onTap: () {
setState(() {
_isEditingText = false;
});
},
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.cancel_outlined),
), ),
), focusedBorder: const OutlineInputBorder(
GestureDetector( borderSide: BorderSide(width: 2.0),
onTap: () {
_updateValue(_editingController.text);
},
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.check),
), ),
suffix: const Text('$userNameSuffix '),
suffixIcon: _isSubmitting
? const RefreshProgressIndicator()
: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
GestureDetector(
onTap: () {
setState(() {
_isEditingText = false;
});
},
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.cancel_outlined),
),
),
GestureDetector(
onTap: () {
_updateValue(_controller.text);
},
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.check),
),
),
],
),
), ),
], cursorColor: Colors.black,
), onSubmitted: _updateValue,
), enabled: !_isSubmitting,
cursorColor: Colors.black, /* onChanged: (String value) {
onSubmitted: _updateValue, if (value.isEmpty) {
onChanged: (String value) { _deleteValue();
if (value.isEmpty) { }
_deleteValue(); }, */
} /*onSubmitted: (String newValue) {
},
/*onSubmitted: (String newValue) {
updateName(newValue); updateName(newValue);
}, */ }, */
// maxLength: 15, // maxLength: 15,
autofocus: true, autofocus: true,
controller: _editingController, controller: _controller,
))) )))
: Tooltip( : Tooltip(
message: tr('your_name_here'), message: tr('your_name_here'),
child: InkWell( child: InkWell(
child: RichText( child: RichText(
// softWrap: true, // softWrap: true,
maxLines: 2, maxLines: 2,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
text: TextSpan( text: TextSpan(
style: DefaultTextStyle.of(context).style, style: DefaultTextStyle.of(context).style,
children: <TextSpan>[ children: <TextSpan>[
if (currentText == defValue) if (currentText == defValue)
TextSpan( TextSpan(
text: currentText.toUpperCase(), text: currentText.toUpperCase(),
style: const TextStyle( style: const TextStyle(
fontFamily: 'SourceCodePro', color: Colors.grey)), fontFamily: 'SourceCodePro',
if (isSet) color: Colors.grey)),
TextSpan( if (currentText.isNotEmpty && currentText != defValue)
text: currentText, style: cardTextStyle(context, 15)), TextSpan(
/* TextSpan( text: currentText,
style: cardTextStyle(context, 15)),
/* TextSpan(
text: ' Lorem ipsum dolor sit amet, consectetur adipiscing elit', text: ' Lorem ipsum dolor sit amet, consectetur adipiscing elit',
style: cardTextStyle(context, 15), style: cardTextStyle(context, 15),
), */ ), */
if (isSet) if (currentText.isNotEmpty && currentText != defValue)
TextSpan( TextSpan(
text: userNameSuffix, text: userNameSuffix,
style: cardTextStyle(context, 12), style: cardTextStyle(context, 12),
),
],
), ),
], ),
), onTap: () {
), setState(() {
onTap: () { _isEditingText = true;
setState(() { });
try { },
/* _editingController.selection = TextSelection( ));
baseOffset: 0, extentOffset: currentText.length);*/ });
} catch (e) {
logger(e);
}
_isEditingText = true;
});
},
));
} }
/*
void updateName(String newValue) {
setState(() {
if (newValue.isEmpty) {
// FIXME delete old name
SharedPreferencesHelper().setName(name: '');
currentText = _currentTextOrDef();
} else if (newValue == defValue) {
currentText = newValue;
} else {
try {
// await createOrUpdateCesiumPlusUser(newValue);
SharedPreferencesHelper().setName(name: newValue);
currentText = newValue;
} catch (e) {
logger(e);
// FIXME show message
}
}
_isEditingText = false;
});
}*/
Future<void> _updateValue(String newValue) async { Future<void> _updateValue(String newValue) async {
if (newValue.isEmpty) {
return _deleteValue();
}
logger('updating with newValue: $newValue'); logger('updating with newValue: $newValue');
try { try {
setState(() {
_isSubmitting = true;
});
if (_validate(newValue)) { if (_validate(newValue)) {
await createOrUpdateCesiumPlusUser(newValue); await createOrUpdateCesiumPlusUser(newValue);
setState(() { setState(() {
_previousValue = newValue; _previousValue = newValue;
currentText = newValue;
}); });
} else { } else {
setState(() { setState(() {
_editingController.text = _previousValue; _controller.text = _previousValue;
currentText = _previousValue.isEmpty ? defValue : _previousValue; currentText = _previousValue.isEmpty ? defValue : _previousValue;
}); });
} }
} catch (e) { } catch (e) {
setState(() { setState(() {
_editingController.text = _previousValue; _controller.text = _previousValue;
currentText = _previousValue.isEmpty ? defValue : _previousValue; currentText = _previousValue.isEmpty ? defValue : _previousValue;
}); });
} }
setState(() { setState(() {
_isEditingText = false; _isEditingText = false;
_isSubmitting = false;
}); });
logger( logger(
'currentText: $currentText, newValue: $newValue, _previousValue: $_previousValue'); 'currentText: $currentText, newValue: $newValue, _previousValue: $_previousValue');
...@@ -222,20 +212,24 @@ class _CardNameEditableState extends State<CardNameEditable> { ...@@ -222,20 +212,24 @@ class _CardNameEditableState extends State<CardNameEditable> {
Future<void> _deleteValue() async { Future<void> _deleteValue() async {
try { try {
setState(() {
_isSubmitting = true;
});
await deleteCesiumPlusUser(); await deleteCesiumPlusUser();
SharedPreferencesHelper().setName(name: ''); SharedPreferencesHelper().setName(name: '');
setState(() { setState(() {
_editingController.text = ''; _controller.text = '';
currentText = defValue; currentText = defValue;
}); });
} catch (e) { } catch (e) {
setState(() { setState(() {
_editingController.text = _previousValue; _controller.text = _previousValue;
currentText = _previousValue.isEmpty ? defValue : _previousValue; currentText = _previousValue.isEmpty ? defValue : _previousValue;
}); });
} }
setState(() { setState(() {
_isEditingText = false; _isEditingText = false;
_isSubmitting = false;
}); });
logger( logger(
'delete with currentText: $currentText, _previousValue: $_previousValue'); 'delete with currentText: $currentText, _previousValue: $_previousValue');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment