implement changing username

This commit is contained in:
Mike Schwörer 2025-04-12 14:35:08 +02:00
parent cdb92757aa
commit e6709cd4af
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
3 changed files with 75 additions and 2 deletions

View File

@ -162,6 +162,20 @@ class APIClient {
); );
} }
static Future<User> updateUser(TokenSource auth, String uid, {String? username, String? proToken}) async {
return await _request(
name: 'updateUser',
method: 'PATCH',
relURL: 'users/$uid',
jsonBody: {
if (username != null) 'username': username,
if (proToken != null) 'pro_token': proToken,
},
fn: User.fromJson,
authToken: auth.getToken(),
);
}
static Future<Client> addClient(TokenSource auth, String fcmToken, String agentModel, String agentVersion, String? name, String clientType) async { static Future<Client> addClient(TokenSource auth, String fcmToken, String agentModel, String agentVersion, String? name, String clientType) async {
return await _request( return await _request(
name: 'addClient', name: 'addClient',

View File

@ -13,6 +13,7 @@ import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/state/globals.dart'; import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/app_auth.dart'; import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/types/immediate_future.dart'; import 'package:simplecloudnotifier/types/immediate_future.dart';
import 'package:simplecloudnotifier/utils/dialogs.dart';
import 'package:simplecloudnotifier/utils/navi.dart'; import 'package:simplecloudnotifier/utils/navi.dart';
import 'package:simplecloudnotifier/utils/toaster.dart'; import 'package:simplecloudnotifier/utils/toaster.dart';
import 'package:simplecloudnotifier/utils/ui.dart'; import 'package:simplecloudnotifier/utils/ui.dart';
@ -359,13 +360,15 @@ class _AccountRootPageState extends State<AccountRootPage> {
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
UI.buttonIconOnly( UI.buttonIconOnly(
onPressed: () {/*TODO*/}, onPressed: _changeUsername,
icon: FontAwesomeIcons.pen, icon: FontAwesomeIcons.pen,
), ),
const SizedBox(height: 4), const SizedBox(height: 4),
if (!user.isPro) if (!user.isPro)
UI.buttonIconOnly( UI.buttonIconOnly(
onPressed: () {/*TODO*/}, onPressed: () {
Toaster.info("Not Implemented", "Account Upgrading will be implemented in a later version"); // TODO
},
icon: FontAwesomeIcons.cartCircleArrowUp, icon: FontAwesomeIcons.cartCircleArrowUp,
), ),
], ],
@ -502,4 +505,31 @@ class _AccountRootPageState extends State<AccountRootPage> {
void _deleteAccount() async { void _deleteAccount() async {
//TODO //TODO
} }
void _changeUsername() async {
final acc = AppAuth();
if (!acc.isAuth()) return;
var newusername = await UIDialogs.showTextInput(context, 'Change your public username', 'Enter new username');
if (newusername == null) return;
newusername = newusername.trim();
if (newusername == '') {
Toaster.error("Error", 'Username cannot be empty');
return;
}
try {
final user = await APIClient.updateUser(acc, acc.userID!, username: newusername);
setState(() {
futureUser = ImmediateFuture.ofValue(user);
});
Toaster.success("Success", 'Username changed');
_backgroundRefresh();
} catch (exc, trace) {
Toaster.error("Error", 'Failed to update username');
ApplicationLog.error('Failed to update username: ' + exc.toString(), trace: trace);
}
}
} }

View File

@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
class UIDialogs {
static Future<String?> showTextInput(BuildContext context, String title, String hintText) {
var _textFieldController = TextEditingController();
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: TextField(
autofocus: true,
controller: _textFieldController,
decoration: InputDecoration(hintText: hintText),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: Text('Cancel'),
),
TextButton(
onPressed: () => Navigator.of(context).pop(_textFieldController.text),
child: Text('OK'),
),
],
),
);
}
}