Finish KeyToken operations

This commit is contained in:
2025-04-18 18:56:17 +02:00
parent 1f0f280286
commit 78c895547e
23 changed files with 1089 additions and 211 deletions

View File

@@ -35,14 +35,13 @@ class AccountRootPage extends StatefulWidget {
}
class _AccountRootPageState extends State<AccountRootPage> {
late ImmediateFuture<int>? futureSubscriptionCount;
late ImmediateFuture<int>? futureClientCount;
late ImmediateFuture<int>? futureKeyCount;
late ImmediateFuture<int>? futureChannelAllCount;
late ImmediateFuture<int>? futureChannelSubscribedCount;
late ImmediateFuture<int>? futureChannelOwnedCount;
late ImmediateFuture<int>? futureSenderNamesCount;
late ImmediateFuture<User>? futureUser;
ImmediateFuture<int> _futureSubscriptionCount = ImmediateFuture.ofPending();
ImmediateFuture<int> _futureClientCount = ImmediateFuture.ofPending();
ImmediateFuture<int> _futureKeyCount = ImmediateFuture.ofPending();
ImmediateFuture<int> _futureChannelAllCount = ImmediateFuture.ofPending();
ImmediateFuture<int> _futureChannelOwnedCount = ImmediateFuture.ofPending();
ImmediateFuture<int> _futureSenderNamesCount = ImmediateFuture.ofPending();
ImmediateFuture<User> _futureUser = ImmediateFuture.ofPending();
late AppAuth userAcc;
@@ -92,58 +91,51 @@ class _AccountRootPageState extends State<AccountRootPage> {
}
void _createFutures() {
futureSubscriptionCount = null;
futureClientCount = null;
futureKeyCount = null;
futureChannelAllCount = null;
futureChannelSubscribedCount = null;
futureChannelOwnedCount = null;
futureSenderNamesCount = null;
_futureSubscriptionCount = ImmediateFuture.ofPending();
_futureClientCount = ImmediateFuture.ofPending();
_futureKeyCount = ImmediateFuture.ofPending();
_futureChannelAllCount = ImmediateFuture.ofPending();
_futureChannelOwnedCount = ImmediateFuture.ofPending();
_futureSenderNamesCount = ImmediateFuture.ofPending();
if (userAcc.isAuth()) {
futureChannelAllCount = ImmediateFuture.ofFuture(() async {
_futureChannelAllCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final channels = await APIClient.getChannelList(userAcc, ChannelSelector.all);
return channels.length;
}());
futureChannelSubscribedCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final channels = await APIClient.getChannelList(userAcc, ChannelSelector.subscribed);
return channels.length;
}());
futureChannelOwnedCount = ImmediateFuture.ofFuture(() async {
_futureChannelOwnedCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final channels = await APIClient.getChannelList(userAcc, ChannelSelector.owned);
return channels.length;
}());
futureSubscriptionCount = ImmediateFuture.ofFuture(() async {
_futureSubscriptionCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final subs = await APIClient.getSubscriptionList(userAcc);
return subs.length;
}());
futureClientCount = ImmediateFuture.ofFuture(() async {
_futureClientCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final clients = await APIClient.getClientList(userAcc);
return clients.length;
}());
futureKeyCount = ImmediateFuture.ofFuture(() async {
_futureKeyCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final keys = await APIClient.getKeyTokenList(userAcc);
return keys.length;
}());
futureSenderNamesCount = ImmediateFuture.ofFuture(() async {
_futureSenderNamesCount = ImmediateFuture.ofFuture(() async {
if (!userAcc.isAuth()) throw new Exception('not logged in');
final senders = (await APIClient.getSenderNameList(userAcc)).map((p) => p.name).toList();
return senders.length;
}());
futureUser = ImmediateFuture.ofFuture(userAcc.loadUser(force: false));
_futureUser = ImmediateFuture.ofFuture(userAcc.loadUser(force: false));
}
}
@@ -157,7 +149,6 @@ class _AccountRootPageState extends State<AccountRootPage> {
// refresh all data and then replace teh futures used in build()
final channelsAll = await APIClient.getChannelList(userAcc, ChannelSelector.all);
final channelsSubscribed = await APIClient.getChannelList(userAcc, ChannelSelector.subscribed);
final subs = await APIClient.getSubscriptionList(userAcc);
final clients = await APIClient.getClientList(userAcc);
final keys = await APIClient.getKeyTokenList(userAcc);
@@ -165,13 +156,12 @@ class _AccountRootPageState extends State<AccountRootPage> {
final user = await userAcc.loadUser(force: true);
setState(() {
futureChannelAllCount = ImmediateFuture.ofValue(channelsAll.length);
futureChannelSubscribedCount = ImmediateFuture.ofValue(channelsSubscribed.length);
futureSubscriptionCount = ImmediateFuture.ofValue(subs.length);
futureClientCount = ImmediateFuture.ofValue(clients.length);
futureKeyCount = ImmediateFuture.ofValue(keys.length);
futureSenderNamesCount = ImmediateFuture.ofValue(senderNames.length);
futureUser = ImmediateFuture.ofValue(user);
_futureChannelAllCount = ImmediateFuture.ofValue(channelsAll.length);
_futureSubscriptionCount = ImmediateFuture.ofValue(subs.length);
_futureClientCount = ImmediateFuture.ofValue(clients.length);
_futureKeyCount = ImmediateFuture.ofValue(keys.length);
_futureSenderNamesCount = ImmediateFuture.ofValue(senderNames.length);
_futureUser = ImmediateFuture.ofValue(user);
});
} catch (exc, trace) {
ApplicationLog.error('Failed to refresh account data: ' + exc.toString(), trace: trace);
@@ -192,10 +182,10 @@ class _AccountRootPageState extends State<AccountRootPage> {
return _buildNoAuth(context);
} else {
return FutureBuilder(
future: futureUser!.future,
future: _futureUser.future,
builder: ((context, snapshot) {
if (futureUser?.value != null) {
return _buildShowAccount(context, acc, futureUser!.value!);
if (_futureUser.value != null) {
return _buildShowAccount(context, acc, _futureUser.value!);
} else if (snapshot.connectionState == ConnectionState.done && snapshot.hasError) {
return ErrorDisplay(errorMessage: '${snapshot.error}');
} else if (snapshot.connectionState == ConnectionState.done) {
@@ -354,10 +344,12 @@ class _AccountRootPageState extends State<AccountRootPage> {
children: [
SizedBox(width: 80, child: Text("Channels", style: TextStyle(color: Theme.of(context).textTheme.bodyLarge?.color?.withAlpha(160)))),
FutureBuilder(
future: futureChannelOwnedCount!.future,
future: _futureChannelOwnedCount.future,
builder: (context, snapshot) {
if (futureChannelOwnedCount?.value != null) {
return Text('${futureChannelOwnedCount!.value}');
if (_futureChannelOwnedCount.value != null) {
return Text('${_futureChannelOwnedCount.value}');
} else if (snapshot.connectionState == ConnectionState.done && snapshot.hasError) {
return Text('ERROR: ${snapshot.error}', style: TextStyle(color: Colors.red));
} else if (snapshot.connectionState == ConnectionState.done) {
return Text('${snapshot.data}');
} else {
@@ -393,11 +385,11 @@ class _AccountRootPageState extends State<AccountRootPage> {
List<Widget> _buildCards(BuildContext context, User user) {
return [
_buildNumberCard(context, 'Subscription', 's', futureSubscriptionCount, () => Navi.push(context, () => SubscriptionListPage())),
_buildNumberCard(context, 'Client', 's', futureClientCount, () => Navi.push(context, () => ClientListPage())),
_buildNumberCard(context, 'Key', 's', futureKeyCount, () => Navi.push(context, () => KeyTokenListPage())),
_buildNumberCard(context, 'Channel', 's', futureChannelAllCount, () => Navi.push(context, () => ChannelListExtendedPage())),
_buildNumberCard(context, 'Sender', '', futureSenderNamesCount, () => Navi.push(context, () => SenderListPage())),
_buildNumberCard(context, 'Subscription', 's', _futureSubscriptionCount, () => Navi.push(context, () => SubscriptionListPage())),
_buildNumberCard(context, 'Client', 's', _futureClientCount, () => Navi.push(context, () => ClientListPage())),
_buildNumberCard(context, 'Key', 's', _futureKeyCount, () => Navi.push(context, () => KeyTokenListPage())),
_buildNumberCard(context, 'Channel', 's', _futureChannelAllCount, () => Navi.push(context, () => ChannelListExtendedPage())),
_buildNumberCard(context, 'Sender', '', _futureSenderNamesCount, () => Navi.push(context, () => SenderListPage())),
UI.buttonCard(
context: context,
margin: EdgeInsets.fromLTRB(0, 4, 0, 4),
@@ -415,17 +407,19 @@ class _AccountRootPageState extends State<AccountRootPage> {
];
}
Widget _buildNumberCard(BuildContext context, String txt, String pluralSuffix, ImmediateFuture<int>? future, void Function() action) {
Widget _buildNumberCard(BuildContext context, String txt, String pluralSuffix, ImmediateFuture<int> future, void Function() action) {
return UI.buttonCard(
context: context,
margin: EdgeInsets.fromLTRB(0, 4, 0, 4),
child: Row(
children: [
FutureBuilder(
future: future?.future,
future: future.future,
builder: (context, snapshot) {
if (future?.value != null) {
return Text('${future!.value}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20));
if (future.value != null) {
return Text('${future.value}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20));
} else if (snapshot.connectionState == ConnectionState.done && snapshot.hasError) {
return Text('ERROR: ${snapshot.error}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20, color: Colors.red));
} else if (snapshot.connectionState == ConnectionState.done) {
return Text('${snapshot.data}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20));
} else {
@@ -435,10 +429,12 @@ class _AccountRootPageState extends State<AccountRootPage> {
),
const SizedBox(width: 12),
FutureBuilder(
future: future?.future,
future: future.future,
builder: (context, snapshot) {
if (future?.value != null) {
return Text('${txt}${((future!.value != 1) ? pluralSuffix : '')}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20));
if (future.value != null) {
return Text('${txt}${((future.value != 1) ? pluralSuffix : '')}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20));
} else if (snapshot.connectionState == ConnectionState.done && snapshot.hasError) {
return Text('ERROR: ${snapshot.error}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20, color: Colors.red));
} else if (snapshot.connectionState == ConnectionState.done) {
return Text('${txt}${((snapshot.data != 1) ? pluralSuffix : '')}', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20));
} else {
@@ -562,7 +558,7 @@ class _AccountRootPageState extends State<AccountRootPage> {
try {
final user = await APIClient.updateUser(acc, acc.userID!, username: newusername);
setState(() {
futureUser = ImmediateFuture.ofValue(user);
_futureUser = ImmediateFuture.ofValue(user);
});
Toaster.success("Success", 'Username changed');