diff --git a/lib/ui/widgets/first_screen/card_name_editable.dart b/lib/ui/widgets/first_screen/card_name_editable.dart index a8366fee47e804ba79bb40ad4be0b2a35602cd52..201d33761ce6214b3dcb4ec9148eb2dcd7525539 100644 --- a/lib/ui/widgets/first_screen/card_name_editable.dart +++ b/lib/ui/widgets/first_screen/card_name_editable.dart @@ -16,205 +16,195 @@ class CardNameEditable extends StatefulWidget { class _CardNameEditableState extends State<CardNameEditable> { bool _isEditingText = false; - final TextEditingController _editingController = TextEditingController(); + final TextEditingController _controller = TextEditingController(); late String currentText; - late String defValue; + final String defValue = tr('your_name_here'); - // late FocusNode myFocusNode; final String pubKey = SharedPreferencesHelper().getPubKey(); String _previousValue = ''; + bool _isSubmitting = false; @override void initState() { - super.initState(); - defValue = tr('your_name_here'); currentText = defValue; - // myFocusNode = FocusNode(); - _initValue(); + super.initState(); } - Future<void> _initValue() async { + Future<String> _initValue() async { final String localUsername = SharedPreferencesHelper().getName(); try { - final String? name = await getCesiumPlusUser(pubKey); + String? name = await getCesiumPlusUser(pubKey); logger( 'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue, retrieved_name: $name'); if (name != null) { - _editingController.text = name; + name = name.replaceAll(userNameSuffix, ''); + _controller.text = name; currentText = name; SharedPreferencesHelper().setName(name: name); } else { - _editingController.text = ''; + _controller.text = ''; currentText = defValue; SharedPreferencesHelper().setName(name: ''); } } catch (e) { logger(e); - _editingController.text = localUsername; + _controller.text = localUsername; currentText = localUsername; } - _previousValue = _editingController.text; + _previousValue = _controller.text; + _controller.selection = TextSelection.fromPosition( + TextPosition(offset: _controller.text.length)); + logger( 'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue'); + return currentText; } @override void dispose() { - _editingController.dispose(); + _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { - final bool isSet = currentText.isNotEmpty && currentText != defValue; - return _isEditingText - ? SizedBox( - width: 150.0, - child: SizedBox( - height: 40.0, - child: TextField( - // focusNode: myFocusNode, - decoration: InputDecoration( - contentPadding: const EdgeInsets.symmetric( - vertical: 5.0, horizontal: 7.0), - filled: true, - fillColor: Colors.white, - enabledBorder: const OutlineInputBorder( - borderSide: BorderSide(color: Colors.grey), - ), - focusedBorder: const OutlineInputBorder( - 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), + return FutureBuilder<String>( + future: _initValue(), + builder: (BuildContext context, AsyncSnapshot<String> snapshot) { + return _isEditingText + ? SizedBox( + width: 150.0, + child: SizedBox( + height: 40.0, + child: TextField( + // focusNode: myFocusNode, + decoration: InputDecoration( + contentPadding: const EdgeInsets.symmetric( + vertical: 5.0, horizontal: 7.0), + filled: true, + fillColor: Colors.white, + enabledBorder: const OutlineInputBorder( + borderSide: BorderSide(color: Colors.grey), ), - ), - GestureDetector( - onTap: () { - _updateValue(_editingController.text); - }, - child: const Padding( - padding: EdgeInsets.only(right: 8.0), - child: Icon(Icons.check), + focusedBorder: const OutlineInputBorder( + borderSide: BorderSide(width: 2.0), ), + 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, - onChanged: (String value) { - if (value.isEmpty) { - _deleteValue(); - } - }, - /*onSubmitted: (String newValue) { + cursorColor: Colors.black, + onSubmitted: _updateValue, + enabled: !_isSubmitting, + /* onChanged: (String value) { + if (value.isEmpty) { + _deleteValue(); + } + }, */ + /*onSubmitted: (String newValue) { updateName(newValue); }, */ - // maxLength: 15, - autofocus: true, - controller: _editingController, - ))) - : Tooltip( - message: tr('your_name_here'), - child: InkWell( - child: RichText( - // softWrap: true, - maxLines: 2, - overflow: TextOverflow.ellipsis, - text: TextSpan( - style: DefaultTextStyle.of(context).style, - children: <TextSpan>[ - if (currentText == defValue) - TextSpan( - text: currentText.toUpperCase(), - style: const TextStyle( - fontFamily: 'SourceCodePro', color: Colors.grey)), - if (isSet) - TextSpan( - text: currentText, style: cardTextStyle(context, 15)), - /* TextSpan( + // maxLength: 15, + autofocus: true, + controller: _controller, + ))) + : Tooltip( + message: tr('your_name_here'), + child: InkWell( + child: RichText( + // softWrap: true, + maxLines: 2, + overflow: TextOverflow.ellipsis, + text: TextSpan( + style: DefaultTextStyle.of(context).style, + children: <TextSpan>[ + if (currentText == defValue) + TextSpan( + text: currentText.toUpperCase(), + style: const TextStyle( + fontFamily: 'SourceCodePro', + color: Colors.grey)), + if (currentText.isNotEmpty && currentText != defValue) + TextSpan( + text: currentText, + style: cardTextStyle(context, 15)), + /* TextSpan( text: ' Lorem ipsum dolor sit amet, consectetur adipiscing elit', style: cardTextStyle(context, 15), ), */ - if (isSet) - TextSpan( - text: userNameSuffix, - style: cardTextStyle(context, 12), + if (currentText.isNotEmpty && currentText != defValue) + TextSpan( + text: userNameSuffix, + style: cardTextStyle(context, 12), + ), + ], ), - ], - ), - ), - onTap: () { - setState(() { - try { - /* _editingController.selection = TextSelection( - baseOffset: 0, extentOffset: currentText.length);*/ - } catch (e) { - logger(e); - } - _isEditingText = true; - }); - }, - )); + ), + onTap: () { + setState(() { + _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 { + if (newValue.isEmpty) { + return _deleteValue(); + } logger('updating with newValue: $newValue'); try { + setState(() { + _isSubmitting = true; + }); if (_validate(newValue)) { await createOrUpdateCesiumPlusUser(newValue); setState(() { _previousValue = newValue; + currentText = newValue; }); } else { setState(() { - _editingController.text = _previousValue; + _controller.text = _previousValue; currentText = _previousValue.isEmpty ? defValue : _previousValue; }); } } catch (e) { setState(() { - _editingController.text = _previousValue; + _controller.text = _previousValue; currentText = _previousValue.isEmpty ? defValue : _previousValue; }); } setState(() { _isEditingText = false; + _isSubmitting = false; }); logger( 'currentText: $currentText, newValue: $newValue, _previousValue: $_previousValue'); @@ -222,20 +212,24 @@ class _CardNameEditableState extends State<CardNameEditable> { Future<void> _deleteValue() async { try { + setState(() { + _isSubmitting = true; + }); await deleteCesiumPlusUser(); SharedPreferencesHelper().setName(name: ''); setState(() { - _editingController.text = ''; + _controller.text = ''; currentText = defValue; }); } catch (e) { setState(() { - _editingController.text = _previousValue; + _controller.text = _previousValue; currentText = _previousValue.isEmpty ? defValue : _previousValue; }); } setState(() { _isEditingText = false; + _isSubmitting = false; }); logger( 'delete with currentText: $currentText, _previousValue: $_previousValue');