Skip to content
Snippets Groups Projects
Commit 19af5b69 authored by vjrj's avatar vjrj
Browse files

Fix editable card name when switching from cards

parent 4ef01e05
No related branches found
No related tags found
No related merge requests found
......@@ -113,7 +113,11 @@ class _FirstScreenState extends State<FirstScreen> {
//controller: _controller,
// shrinkWrap: true,
children: <Widget>[
CreditCard(key: creditCardKey),
CreditCard(
key: creditCardKey,
publicKey: prefsHelper.getPubKey(),
cardName: prefsHelper.getName(),
isG1nkgoCard: prefsHelper.isG1nkgoCard()),
const SizedBox(height: 8),
Padding(
padding:
......
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../g1/api.dart';
import '../../../shared_prefs_helper.dart';
......@@ -10,9 +9,17 @@ import '../connectivity_widget_wrapper_wrapper.dart';
import 'card_text_style.dart';
class CardNameEditable extends StatefulWidget {
const CardNameEditable({super.key, required this.defValue});
const CardNameEditable(
{super.key,
required this.defValue,
required this.publicKey,
required this.cardName,
required this.isG1nkgoCard});
final String defValue;
final String publicKey;
final String cardName;
final bool isG1nkgoCard;
@override
State<CardNameEditable> createState() => _CardNameEditableState();
......@@ -20,174 +27,218 @@ class CardNameEditable extends StatefulWidget {
class _CardNameEditableState extends State<CardNameEditable> {
bool _isEditingText = false;
bool _isSubmitting = false;
final TextEditingController _controller = TextEditingController();
late String currentText;
String _previousValue = '';
bool _isSubmitting = false;
@override
void initState() {
final String localUsername = SharedPreferencesHelper().getName();
currentText = localUsername.isEmpty ? widget.defValue : localUsername;
super.initState();
_initValue();
}
Future<void> _initValue() async {
final String localUsername = widget.cardName;
if (localUsername.isEmpty) {
setState(() {
currentText = widget.defValue;
_controller.text = currentText;
});
} else {
setState(() {
currentText = localUsername;
_controller.text = currentText;
});
}
await _fetchAndSetUsername();
}
@override
void didUpdateWidget(CardNameEditable oldWidget) {
super.didUpdateWidget(oldWidget);
if (widget.cardName != oldWidget.cardName) {
final String localUsername = widget.cardName;
if (localUsername.isEmpty) {
setState(() {
currentText = widget.defValue;
_controller.text = currentText;
});
} else {
setState(() {
currentText = localUsername;
_controller.text = currentText;
});
}
// Optionally
// _fetchAndSetUsername();
}
}
Future<String> _initValue() async {
loggerDev('Building CardNameEditable');
final String localUsername = SharedPreferencesHelper().getName();
Future<void> _fetchAndSetUsername() async {
final bool isConnected = await ConnectivityWidgetWrapperWrapper.isConnected;
if (isConnected) {
try {
String? name =
await getCesiumPlusUser(SharedPreferencesHelper().getPubKey());
logger(
'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue, retrieved_name: $name');
if (localUsername != name) {
if (name != null) {
name = name.replaceAll(g1nkgoUserNameSuffix, '');
_controller.text = name;
String? name = await getCesiumPlusUser(widget.publicKey);
if (name != null && name.isNotEmpty) {
name = name.replaceAll(g1nkgoUserNameSuffix, '');
setState(() {
_controller.text = name!;
currentText = name;
SharedPreferencesHelper().setName(name: name, notify: false);
} else {
});
SharedPreferencesHelper().setName(name: name, notify: false);
} else {
setState(() {
_controller.text = '';
currentText = widget.defValue;
SharedPreferencesHelper().setName(name: '', notify: false);
}
});
SharedPreferencesHelper().setName(name: '', notify: false);
}
} catch (e) {
logger(e);
_controller.text = localUsername;
currentText = localUsername;
logger('Error: $e');
}
} else {
// not connected, same an on exception
_controller.text = localUsername;
currentText = localUsername;
}
_previousValue = _controller.text;
_controller.selection = TextSelection.fromPosition(
TextPosition(offset: _controller.text.length));
}
logger(
'currentText: $currentText, localUsername: $localUsername, _previousValue: $_previousValue');
return currentText;
Widget _buildEditingField() {
if (currentText == widget.defValue) {
_controller.text = '';
} else {
_controller.text = currentText;
}
return SizedBox(
width: 150.0,
child: SizedBox(
height: 40.0,
child: TextField(
style: const TextStyle(color: Colors.black87),
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),
),
suffix: const Text('$g1nkgoUserNameSuffix '),
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,
color: Colors.black87),
),
),
GestureDetector(
onTap: () {
_updateValue(_controller.text);
},
child: const Padding(
padding: EdgeInsets.only(right: 8.0),
child: Icon(Icons.check, color: Colors.black87),
),
),
],
),
),
cursorColor: Colors.black87,
onSubmitted: _updateValue,
enabled: !_isSubmitting,
autofocus: true,
controller: _controller,
),
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
Widget _buildDisplayField() {
return InkWell(
onTap: () {
if (widget.isG1nkgoCard) {
setState(() {
_isEditingText = true;
});
}
},
child: RichText(
maxLines: 2,
overflow: TextOverflow.ellipsis,
text: TextSpan(
style: DefaultTextStyle.of(context).style,
children: <TextSpan>[
if (currentText == widget.defValue)
TextSpan(
text: currentText.toUpperCase(),
style: const TextStyle(
fontFamily: 'SourceCodePro', color: Colors.grey),
),
if (currentText.isNotEmpty && currentText != widget.defValue)
TextSpan(
text: currentText,
style: cardTextStyle(context, fontSize: 15),
),
// Suffix
if (currentText.isNotEmpty && currentText != widget.defValue)
TextSpan(
text: widget.isG1nkgoCard
? g1nkgoUserNameSuffix
: protectedUserNameSuffix,
style: cardTextStyle(context, fontSize: 12),
),
],
),
),
);
}
@override
Widget build(BuildContext context) {
return Consumer<SharedPreferencesHelper>(builder: (BuildContext context,
SharedPreferencesHelper prefsHelper, Widget? child) {
return FutureBuilder<String>(
future: _initValue(),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
const Color black = Colors.black87;
if (snapshot.hasData) {
return _isEditingText
? SizedBox(
width: 150.0,
child: SizedBox(
height: 40.0,
child: TextField(
// focusNode: myFocusNode,
style: const TextStyle(color: black),
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),
),
suffix: const Text('$g1nkgoUserNameSuffix '),
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,
color: black),
),
),
GestureDetector(
onTap: () {
_updateValue(_controller.text);
},
child: const Padding(
padding:
EdgeInsets.only(right: 8.0),
child:
Icon(Icons.check, color: black),
),
),
],
),
),
cursorColor: black,
onSubmitted: _updateValue,
enabled: !_isSubmitting,
/* onChanged: (String value) {
if (value.isEmpty) {
_deleteValue();
}
}, */
/*onSubmitted: (String newValue) {
updateName(newValue);
}, */
// maxLength: 15,
autofocus: true,
controller: _controller,
)))
: Tooltip(
message: widget.defValue,
child: CardNameText(
currentText: currentText,
isGinkgoCard:
SharedPreferencesHelper().isG1nkgoCard(),
onTap: () => SharedPreferencesHelper().isG1nkgoCard()
? setState(() {
_isEditingText = true;
})
: null));
} else {
return CardNameText(
currentText: currentText,
isGinkgoCard: SharedPreferencesHelper().isG1nkgoCard(),
onTap: () {});
}
loggerDev(
"Building CardNameEditable for ${widget.publicKey} '${widget.cardName}'");
return GestureDetector(
onTap: () {
if (widget.isG1nkgoCard) {
setState(() {
_isEditingText = true;
});
});
}
},
child: _isEditingText ? _buildEditingField() : _buildDisplayField(),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Future<void> _updateValue(String newValue) async {
if (newValue.isEmpty) {
return _deleteValue();
}
logger('updating with newValue: $newValue');
setState(() {
_isSubmitting = true;
});
try {
setState(() {
_isSubmitting = true;
});
if (_validate(newValue)) {
await createOrUpdateCesiumPlusUser(newValue);
SharedPreferencesHelper().setName(name: newValue, notify: false);
setState(() {
currentText = newValue;
});
if (!context.mounted) {
return;
}
......@@ -196,36 +247,21 @@ class _CardNameEditableState extends State<CardNameEditable> {
content: Text(tr('card_name_changed')),
),
);
setState(() {
_previousValue = newValue;
currentText = newValue;
});
} else {
setState(() {
_controller.text = _previousValue;
currentText =
_previousValue.isEmpty ? widget.defValue : _previousValue;
});
}
} catch (e) {
setState(() {
_controller.text = _previousValue;
currentText = _previousValue.isEmpty ? widget.defValue : _previousValue;
});
loggerDev(e.toString());
}
setState(() {
_isEditingText = false;
_isSubmitting = false;
});
logger(
'currentText: $currentText, newValue: $newValue, _previousValue: $_previousValue');
}
Future<void> _deleteValue() async {
setState(() {
_isSubmitting = true;
});
try {
setState(() {
_isSubmitting = true;
});
await deleteCesiumPlusUser();
SharedPreferencesHelper().setName(name: '');
setState(() {
......@@ -233,17 +269,12 @@ class _CardNameEditableState extends State<CardNameEditable> {
currentText = widget.defValue;
});
} catch (e) {
setState(() {
_controller.text = _previousValue;
currentText = _previousValue.isEmpty ? widget.defValue : _previousValue;
});
logger('Error: $e');
}
setState(() {
_isEditingText = false;
_isSubmitting = false;
});
logger(
'delete with currentText: $currentText, _previousValue: $_previousValue');
}
bool _validate(String newValue) {
......@@ -268,7 +299,6 @@ class CardNameText extends StatelessWidget {
@override
Widget build(BuildContext context) {
// Dup above
final String defValue = isGinkgoCard ? tr('your_name_here') : '';
return InkWell(
onTap: onTap,
......
......@@ -11,10 +11,15 @@ import 'card_name_editable.dart';
import 'card_text_style.dart';
class CreditCard extends StatelessWidget {
CreditCard({super.key});
const CreditCard(
{super.key,
required this.publicKey,
required this.cardName,
required this.isG1nkgoCard});
final String publicKey = SharedPreferencesHelper().getPubKey();
final String cardName = SharedPreferencesHelper().getName();
final String publicKey;
final String cardName;
final bool isG1nkgoCard;
@override
Widget build(BuildContext context) {
......@@ -22,8 +27,6 @@ class CreditCard extends StatelessWidget {
final bool bigDevice = bigScreen(context);
final double cardPadding = bigDevice ? 26.0 : 16.0;
final String pubKey = SharedPreferencesHelper().getPubKey();
return Card(
elevation: 8.0,
shape: RoundedRectangleBorder(
......@@ -84,7 +87,7 @@ class CreditCard extends StatelessWidget {
GestureDetector(
onTap: () {
showQrDialog(
context: context, publicKey: pubKey);
context: context, publicKey: publicKey);
},
child: SvgPicture.asset(
width: MediaQuery.of(context).size.width <
......@@ -96,9 +99,11 @@ class CreditCard extends StatelessWidget {
const SizedBox(width: 10.0),
Expanded(
child: CardNameEditable(
defValue: SharedPreferencesHelper().isG1nkgoCard()
? tr('your_name_here')
: '',
publicKey: publicKey,
cardName: cardName,
isG1nkgoCard: isG1nkgoCard,
defValue:
isG1nkgoCard ? tr('your_name_here') : '',
)),
])),
const SizedBox(height: 6.0),
......@@ -108,12 +113,13 @@ class CreditCard extends StatelessWidget {
child: Row(children: <Widget>[
GestureDetector(
onTap: () => showQrDialog(
context: context, publicKey: pubKey),
context: context, publicKey: publicKey),
child: FittedBox(
key: creditCardPubKey,
fit: BoxFit.scaleDown,
child: Text(
simplifyPubKey(extractPublicKey(pubKey)),
simplifyPubKey(
extractPublicKey(publicKey)),
style: cardTextStyle(context),
))),
GestureDetector(
......
......@@ -168,7 +168,7 @@ class CreditCardMini extends StatelessWidget {
}
void onCardTap(BuildContext context) {
logger('Card ${card.name} was tapped!');
logger("Card ${card.pubKey} '${card.name}' was tapped!");
SharedPreferencesHelper().selectCurrentWallet(card);
Navigator.pop(context);
}
......
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