diff --git a/lib/screens/account/personalize.dart b/lib/screens/account/personalize.dart index f3deee2..5aee0e2 100644 --- a/lib/screens/account/personalize.dart +++ b/lib/screens/account/personalize.dart @@ -68,21 +68,23 @@ class _PersonalizeScreenWidgetState extends State { void resetInputs() async { final auth = context.read(); final prof = await auth.getProfiles(); - _usernameController.text = prof['name']; - _nicknameController.text = prof['nick']; - _descriptionController.text = prof['description']; - _firstNameController.text = prof['profile']['first_name']; - _lastNameController.text = prof['profile']['last_name']; - if (prof['avatar'] != null && prof['avatar'].isNotEmpty) { - _avatar = getRequestUri('passport', '/api/avatar/${prof['avatar']}').toString(); - } - if (prof['banner'] != null && prof['banner'].isNotEmpty) { - _banner = getRequestUri('passport', '/api/avatar/${prof['banner']}').toString(); - } - if (prof['profile']['birthday'] != null) { - _birthday = DateTime.parse(prof['profile']['birthday']); - _birthdayController.text = DateFormat('yyyy-MM-dd hh:mm').format(_birthday!); - } + setState(() { + _usernameController.text = prof['name']; + _nicknameController.text = prof['nick']; + _descriptionController.text = prof['description']; + _firstNameController.text = prof['profile']['first_name']; + _lastNameController.text = prof['profile']['last_name']; + if (prof['avatar'] != null && prof['avatar'].isNotEmpty) { + _avatar = getRequestUri('passport', '/api/avatar/${prof['avatar']}').toString(); + } + if (prof['banner'] != null && prof['banner'].isNotEmpty) { + _banner = getRequestUri('passport', '/api/avatar/${prof['banner']}').toString(); + } + if (prof['profile']['birthday'] != null) { + _birthday = DateTime.parse(prof['profile']['birthday']); + _birthdayController.text = DateFormat('yyyy-MM-dd hh:mm').format(_birthday!); + } + }); } void applyChanges() async { @@ -107,9 +109,7 @@ class _PersonalizeScreenWidgetState extends State { ); if (res.statusCode == 200) { await auth.fetchProfiles(); - setState(() { - resetInputs(); - }); + resetInputs(); ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(AppLocalizations.of(context)!.personalizeApplied), @@ -122,7 +122,7 @@ class _PersonalizeScreenWidgetState extends State { setState(() => _isSubmitting = false); } - Future applyAvatar(String position) async { + Future applyImage(String position) async { final auth = context.read(); if (!await auth.isAuthorized()) return; @@ -139,9 +139,7 @@ class _PersonalizeScreenWidgetState extends State { var res = await auth.client!.send(req); if (res.statusCode == 200) { await auth.fetchProfiles(); - setState(() { - resetInputs(); - }); + resetInputs(); ScaffoldMessenger.of(context).showSnackBar(SnackBar( content: Text(AppLocalizations.of(context)!.personalizeApplied), @@ -161,9 +159,7 @@ class _PersonalizeScreenWidgetState extends State { super.initState(); Future.delayed(Duration.zero, () { - setState(() { - resetInputs(); - }); + resetInputs(); }); } @@ -182,7 +178,8 @@ class _PersonalizeScreenWidgetState extends State { bottom: 0, left: 40, child: FloatingActionButton.small( - onPressed: () => applyAvatar('avatar'), + heroTag: const Key('avatar-editor'), + onPressed: () => applyImage('avatar'), child: const Icon( Icons.camera, ), @@ -222,7 +219,8 @@ class _PersonalizeScreenWidgetState extends State { bottom: 16, right: 16, child: FloatingActionButton( - onPressed: () => applyAvatar('banner'), + heroTag: const Key('banner-editor'), + onPressed: () => applyImage('banner'), child: const Icon( Icons.camera_alt, ), diff --git a/lib/screens/users/userinfo.dart b/lib/screens/users/userinfo.dart index fb060b0..7d88dd5 100644 --- a/lib/screens/users/userinfo.dart +++ b/lib/screens/users/userinfo.dart @@ -32,17 +32,26 @@ class _UserInfoScreenState extends State { _client.get(getRequestUri('passport', '/api/users/${widget.name}')), _client.get(getRequestUri('passport', '/api/users/${widget.name}/page')) ], eagerError: true); - final mistakeRes = res.indexWhere((x) => x.statusCode != 200); + final mistakeRes = res.indexWhere((x) => x.statusCode != 200 && x.statusCode != 400); if (mistakeRes > -1) { - var message = utf8.decode(res[0].bodyBytes); + var message = utf8.decode(res[mistakeRes].bodyBytes); context.showErrorDialog(message); throw Exception(message); } else { final info = Account.fromJson(jsonDecode(utf8.decode(res[0].bodyBytes))); - final page = PersonalPage.fromJson(jsonDecode(utf8.decode(res[1].bodyBytes))); + final page = res[1].statusCode == 200 ? PersonalPage.fromJson(jsonDecode(utf8.decode(res[1].bodyBytes))) : null; setState(() { _userinfo = info; - _page = page; + _page = page ?? + PersonalPage( + id: 0, + createdAt: DateTime.now(), + updatedAt: DateTime.now(), + content: '', + script: '', + style: '', + accountId: info.id, + ); }); return info; }