mirror of
https://github.com/twonlyapp/twonly-app.git
synced 2026-05-25 02:12:13 +00:00
rewrite global user variable
This commit is contained in:
parent
3d35615136
commit
56aa6e9f7e
5 changed files with 71 additions and 85 deletions
|
|
@ -176,12 +176,12 @@ class _AppMainWidgetState extends State<AppMainWidget> {
|
|||
_isTwonlyLocked = false;
|
||||
}),
|
||||
);
|
||||
} else if (AppSession.currentUser.twonlySafeBackup == null && !_skipBackup) {
|
||||
} else if (AppSession.currentUser.twonlySafeBackup == null &&
|
||||
!_skipBackup) {
|
||||
child = SetupBackupView(
|
||||
callBack: () {
|
||||
callBack: () => setState(() {
|
||||
_skipBackup = true;
|
||||
setState(() {});
|
||||
},
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
child = HomeView(
|
||||
|
|
|
|||
|
|
@ -27,17 +27,9 @@ class AppState {
|
|||
}
|
||||
|
||||
class AppGlobalKeys {
|
||||
static final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey =
|
||||
GlobalKey<ScaffoldMessengerState>();
|
||||
static final scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();
|
||||
}
|
||||
|
||||
late ApiService apiService;
|
||||
|
||||
// uses for background notification
|
||||
late TwonlyDB twonlyDB;
|
||||
|
||||
// Cached UserData in the memory. Every time the user data is changed the `updateUserdata` function is called,
|
||||
// which will update this global variable. The variable is set in the main.dart and after the user has registered in the register.view.dart
|
||||
class AppSession {
|
||||
static late UserData currentUser;
|
||||
|
||||
|
|
@ -48,3 +40,6 @@ class AppSession {
|
|||
_userDataUpdateController.add(null);
|
||||
}
|
||||
}
|
||||
|
||||
late ApiService apiService;
|
||||
late TwonlyDB twonlyDB;
|
||||
|
|
|
|||
|
|
@ -21,9 +21,6 @@ class SettingsChangeProvider with ChangeNotifier, DiagnosticableTreeMixin {
|
|||
|
||||
notifyListeners();
|
||||
|
||||
await updateUserdata((user) {
|
||||
user.themeMode = newThemeMode;
|
||||
return user;
|
||||
});
|
||||
await updateUser((u) => u.themeMode = newThemeMode);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,8 +34,7 @@ class _PublicProfileViewState extends State<PublicProfileView> {
|
|||
_qrCode = await getProfileQrCodeData();
|
||||
_userAvatar = await getUserAvatar();
|
||||
_publicKey = await getUserPublicKey();
|
||||
|
||||
setState(() {});
|
||||
if (mounted) setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -126,7 +125,9 @@ class _PublicProfileViewState extends State<PublicProfileView> {
|
|||
text: context.lang.shareYourProfile,
|
||||
subtitle: (_publicKey == null)
|
||||
? null
|
||||
: Text('https://me.twonly.eu/${AppSession.currentUser.username}'),
|
||||
: Text(
|
||||
'https://me.twonly.eu/${AppSession.currentUser.username}',
|
||||
),
|
||||
onTap: () {
|
||||
final params = ShareParams(
|
||||
text:
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:twonly/globals.dart';
|
||||
import 'package:twonly/src/utils/misc.dart';
|
||||
import 'package:twonly/src/utils/storage.dart';
|
||||
import 'package:twonly/src/views/components/animate_icon.dart';
|
||||
|
|
@ -14,32 +13,30 @@ class ChatReactionSelectionView extends StatefulWidget {
|
|||
}
|
||||
|
||||
class _ChatReactionSelectionView extends State<ChatReactionSelectionView> {
|
||||
List<String> selectedEmojis = [];
|
||||
List<String> _selectedEmojis = [];
|
||||
|
||||
List<String> _emojisFromSession() {
|
||||
final user = AppSession.currentUser;
|
||||
if (user.preSelectedEmojies != null) {
|
||||
return user.preSelectedEmojies!;
|
||||
}
|
||||
return EmojiAnimation.animatedIcons.keys.toList().sublist(0, 6);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
unawaited(initAsync());
|
||||
}
|
||||
|
||||
Future<void> initAsync() async {
|
||||
final user = await getUser();
|
||||
if (user != null && user.preSelectedEmojies != null) {
|
||||
selectedEmojis = user.preSelectedEmojies!;
|
||||
} else {
|
||||
selectedEmojis = EmojiAnimation.animatedIcons.keys.toList().sublist(0, 6);
|
||||
}
|
||||
setState(() {});
|
||||
_selectedEmojis = _emojisFromSession();
|
||||
}
|
||||
|
||||
Future<void> _onEmojiSelected(String emoji) async {
|
||||
if (selectedEmojis.contains(emoji)) {
|
||||
selectedEmojis.remove(emoji);
|
||||
if (_selectedEmojis.contains(emoji)) {
|
||||
_selectedEmojis.remove(emoji);
|
||||
} else {
|
||||
if (selectedEmojis.length < 12) {
|
||||
selectedEmojis.add(emoji);
|
||||
if (_selectedEmojis.length < 12) {
|
||||
_selectedEmojis.add(emoji);
|
||||
await updateUser((user) {
|
||||
user.preSelectedEmojies = selectedEmojis;
|
||||
user.preSelectedEmojies = _selectedEmojis;
|
||||
});
|
||||
} else {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
|
|
@ -55,56 +52,52 @@ class _ChatReactionSelectionView extends State<ChatReactionSelectionView> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Select Reactions'),
|
||||
),
|
||||
body: GridView.builder(
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 4, // Number of columns
|
||||
),
|
||||
itemCount: EmojiAnimation.animatedIcons.keys.length,
|
||||
itemBuilder: (context, index) {
|
||||
final emoji = EmojiAnimation.animatedIcons.keys.elementAt(index);
|
||||
return GestureDetector(
|
||||
onTap: () async {
|
||||
await _onEmojiSelected(emoji);
|
||||
},
|
||||
child: Card(
|
||||
color: selectedEmojis.contains(emoji)
|
||||
? context.color.primary.withAlpha(150)
|
||||
: context.color.surface,
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 40,
|
||||
height: 40,
|
||||
child: EmojiAnimation(
|
||||
emoji: emoji,
|
||||
// repeat: selectedEmojis.contains(emoji),
|
||||
return StreamBuilder<void>(
|
||||
stream: AppSession.onUserUpdated,
|
||||
builder: (context, _) {
|
||||
_selectedEmojis = _emojisFromSession();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Select Reactions'),
|
||||
),
|
||||
body: GridView.builder(
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 4,
|
||||
),
|
||||
itemCount: EmojiAnimation.animatedIcons.keys.length,
|
||||
itemBuilder: (context, index) {
|
||||
final emoji = EmojiAnimation.animatedIcons.keys.elementAt(index);
|
||||
return GestureDetector(
|
||||
onTap: () => _onEmojiSelected(emoji),
|
||||
child: Card(
|
||||
color: _selectedEmojis.contains(emoji)
|
||||
? context.color.primary.withAlpha(150)
|
||||
: context.color.surface,
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 40,
|
||||
height: 40,
|
||||
child: EmojiAnimation(emoji: emoji),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
floatingActionButton: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 30),
|
||||
child: FloatingActionButton(
|
||||
foregroundColor: Colors.white,
|
||||
onPressed: () => updateUser(
|
||||
(u) => u.preSelectedEmojies = EmojiAnimation.animatedIcons.keys
|
||||
.toList()
|
||||
.sublist(0, 6),
|
||||
),
|
||||
child: const Icon(Icons.settings_backup_restore_rounded),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
floatingActionButton: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 30),
|
||||
child: FloatingActionButton(
|
||||
foregroundColor: Colors.white,
|
||||
onPressed: () async {
|
||||
selectedEmojis = EmojiAnimation.animatedIcons.keys.toList().sublist(
|
||||
0,
|
||||
6,
|
||||
);
|
||||
setState(() {});
|
||||
await updateUser((user) {
|
||||
user.preSelectedEmojies = selectedEmojis;
|
||||
});
|
||||
},
|
||||
child: const Icon(Icons.settings_backup_restore_rounded),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue