Add LinearProgressIndicator for background queries

This commit is contained in:
Mike Schwörer 2024-06-13 17:00:08 +02:00
parent beb1005710
commit 8126327b95
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
6 changed files with 119 additions and 39 deletions

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/components/layout/app_bar_progress_indicator.dart';
import 'package:simplecloudnotifier/pages/debug/debug_main.dart';
import 'package:simplecloudnotifier/state/app_theme.dart';
import 'package:simplecloudnotifier/utils/navi.dart';
@ -71,6 +72,10 @@ class SCNAppBar extends StatelessWidget implements PreferredSizeWidget {
title: Text(title ?? 'Simple Cloud Notifier 2.0'),
actions: actions,
backgroundColor: Theme.of(context).secondaryHeaderColor,
bottom: PreferredSize(
preferredSize: Size(double.infinity, 1.0),
child: AppBarProgressIndicator(),
),
);
}

View File

@ -0,0 +1,21 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/state/app_bar_state.dart';
class AppBarProgressIndicator extends StatelessWidget implements PreferredSizeWidget {
@override
Size get preferredSize => Size(double.infinity, 1.0);
@override
Widget build(BuildContext context) {
return Consumer<AppBarState>(
builder: (context, value, child) {
if (value.loadingIndeterminate) {
return LinearProgressIndicator(value: null);
} else {
return SizedBox.square(dimension: 4); // 4 height is the same as the LinearProgressIndicator
}
},
);
}
}

View File

@ -7,12 +7,14 @@ import 'package:hive_flutter/hive_flutter.dart';
import 'package:simplecloudnotifier/api/api_client.dart';
import 'package:simplecloudnotifier/models/client.dart';
import 'package:simplecloudnotifier/nav_layout.dart';
import 'package:simplecloudnotifier/state/app_bar_state.dart';
import 'package:simplecloudnotifier/state/app_theme.dart';
import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/request_log.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:simplecloudnotifier/utils/navi.dart';
import 'package:toastification/toastification.dart';
import 'firebase_options.dart';
@ -113,8 +115,9 @@ void main() async {
providers: [
ChangeNotifierProvider(create: (context) => AppAuth(), lazy: false),
ChangeNotifierProvider(create: (context) => AppTheme(), lazy: false),
ChangeNotifierProvider(create: (context) => AppBarState(), lazy: false),
],
child: const SCNApp(),
child: SCNApp(),
),
);
}
@ -156,7 +159,7 @@ void setFirebaseToken(String fcmToken) async {
}
class SCNApp extends StatelessWidget {
const SCNApp({super.key});
SCNApp({super.key});
@override
Widget build(BuildContext context) {
@ -169,6 +172,7 @@ class SCNApp extends StatelessWidget {
child: Consumer<AppTheme>(
builder: (context, appTheme, child) => MaterialApp(
title: 'SimpleCloudNotifier',
navigatorObservers: [Navi.routeObserver],
theme: ThemeData(
//TODO color settings
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue, brightness: appTheme.darkMode ? Brightness.dark : Brightness.light),

View File

@ -11,6 +11,7 @@ import 'package:simplecloudnotifier/models/keytoken.dart';
import 'package:simplecloudnotifier/models/message.dart';
import 'package:simplecloudnotifier/models/user.dart';
import 'package:simplecloudnotifier/state/app_auth.dart';
import 'package:simplecloudnotifier/state/app_bar_state.dart';
import 'package:simplecloudnotifier/utils/toaster.dart';
import 'package:simplecloudnotifier/utils/ui.dart';
@ -32,11 +33,16 @@ class _MessageViewPageState extends State<MessageViewPage> {
@override
void initState() {
super.initState();
mainFuture = fetchData();
super.initState();
}
Future<(Message, ChannelPreview, KeyTokenPreview, UserPreview)> fetchData() async {
try {
await Future.delayed(const Duration(seconds: 0), () {}); // this is annoyingly important - otherwise we call setLoadingIndeterminate directly in initStat() and get an exception....
AppBarState().setLoadingIndeterminate(true);
final acc = Provider.of<AppAuth>(context, listen: false);
final msg = await APIClient.getMessage(acc, widget.message.messageID);
@ -49,15 +55,16 @@ class _MessageViewPageState extends State<MessageViewPage> {
final key = await fut_key;
final usr = await fut_usr;
//TODO getShortUser for sender
//await Future.delayed(const Duration(seconds: 2), () {});
//await Future.delayed(const Duration(seconds: 10), () {});
final r = (msg, chn, key, usr);
mainFutureSnapshot = r;
return r;
} finally {
AppBarState().setLoadingIndeterminate(false);
}
}
@override
@ -77,11 +84,11 @@ class _MessageViewPageState extends State<MessageViewPage> {
builder: (context, snapshot) {
if (snapshot.hasData) {
final (msg, chn, tok, usr) = snapshot.data!;
return _buildMessageView(context, msg, chn, tok, usr, false);
return _buildMessageView(context, msg, chn, tok, usr);
} else if (snapshot.hasError) {
return Center(child: Text('${snapshot.error}')); //TODO nice error page
} else if (!widget.message.trimmed) {
return _buildMessageView(context, widget.message, null, null, null, true);
return _buildMessageView(context, widget.message, null, null, null);
} else {
return const Center(child: CircularProgressIndicator());
}
@ -111,7 +118,7 @@ class _MessageViewPageState extends State<MessageViewPage> {
}
}
Widget _buildMessageView(BuildContext context, Message message, ChannelPreview? channel, KeyTokenPreview? token, UserPreview? user, bool loading) {
Widget _buildMessageView(BuildContext context, Message message, ChannelPreview? channel, KeyTokenPreview? token, UserPreview? user) {
final userAccUserID = context.select<AppAuth, String?>((v) => v.userID);
return SingleChildScrollView(
@ -120,7 +127,7 @@ class _MessageViewPageState extends State<MessageViewPage> {
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
..._buildMessageHeader(context, message, channel, loading),
..._buildMessageHeader(context, message, channel),
SizedBox(height: 8),
if (message.content != null) ..._buildMessageContent(context, message),
SizedBox(height: 8),
@ -141,7 +148,7 @@ class _MessageViewPageState extends State<MessageViewPage> {
return channel?.displayName ?? message.channelInternalName;
}
List<Widget> _buildMessageHeader(BuildContext context, Message message, ChannelPreview? channel, bool loading) {
List<Widget> _buildMessageHeader(BuildContext context, Message message, ChannelPreview? channel) {
return [
Row(
children: [
@ -156,24 +163,7 @@ class _MessageViewPageState extends State<MessageViewPage> {
],
),
SizedBox(height: 8),
if (!loading) Text(message.title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
if (loading)
Stack(
children: [
Row(
children: [
Flexible(child: Text(message.title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold))),
SizedBox(height: 20, width: 20),
],
),
Row(
children: [
Expanded(child: SizedBox(width: 0)),
SizedBox(child: CircularProgressIndicator(), height: 20, width: 20),
],
),
],
),
Text(message.title, style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
];
}

View File

@ -0,0 +1,20 @@
import 'package:flutter/foundation.dart';
class AppBarState extends ChangeNotifier {
static AppBarState? _singleton = AppBarState._internal();
factory AppBarState() {
return _singleton ?? (_singleton = AppBarState._internal());
}
AppBarState._internal() {}
bool _loadingIndeterminate = false;
bool get loadingIndeterminate => _loadingIndeterminate;
void setLoadingIndeterminate(bool v) {
if (_loadingIndeterminate == v) return;
_loadingIndeterminate = v;
notifyListeners();
}
}

View File

@ -1,11 +1,51 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:simplecloudnotifier/state/app_bar_state.dart';
class Navi {
static final SCNRouteObserver routeObserver = SCNRouteObserver();
static void push<T extends Widget>(BuildContext context, T Function() builder) {
Provider.of<AppBarState>(context, listen: false).setLoadingIndeterminate(false);
Navigator.push(context, MaterialPageRoute<T>(builder: (context) => builder()));
}
static void popToRoot(BuildContext context) {
Provider.of<AppBarState>(context, listen: false).setLoadingIndeterminate(false);
Navigator.popUntil(context, (route) => route.isFirst);
}
}
class SCNRouteObserver extends RouteObserver<PageRoute<dynamic>> {
@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
if (route is PageRoute) {
AppBarState().setLoadingIndeterminate(false);
print('[SCNRouteObserver] .didPush()');
}
}
@override
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
if (newRoute is PageRoute) {
AppBarState().setLoadingIndeterminate(false);
print('[SCNRouteObserver] .didReplace()');
}
}
@override
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);
if (previousRoute is PageRoute && route is PageRoute) {
AppBarState().setLoadingIndeterminate(false);
print('[SCNRouteObserver] .didPop()');
}
}
}