added double tap account change support

This commit is contained in:
Kima 2023-05-27 12:42:23 +02:00
parent be1265fc87
commit 08faa694f2
3 changed files with 107 additions and 37 deletions

View File

@ -1,4 +1,15 @@
import 'package:filcnaplo/api/providers/user_provider.dart';
import 'package:filcnaplo/models/settings.dart';
import 'package:filcnaplo/models/user.dart';
import 'package:filcnaplo_kreta_api/client/client.dart';
import 'package:filcnaplo_kreta_api/providers/absence_provider.dart';
import 'package:filcnaplo_kreta_api/providers/event_provider.dart';
import 'package:filcnaplo_kreta_api/providers/exam_provider.dart';
import 'package:filcnaplo_kreta_api/providers/grade_provider.dart';
import 'package:filcnaplo_kreta_api/providers/homework_provider.dart';
import 'package:filcnaplo_kreta_api/providers/message_provider.dart';
import 'package:filcnaplo_kreta_api/providers/note_provider.dart';
import 'package:filcnaplo_kreta_api/providers/timetable_provider.dart';
import 'package:filcnaplo_mobile_ui/common/profile_image/profile_image.dart';
import 'package:filcnaplo_mobile_ui/screens/settings/settings_screen.dart';
import 'package:flutter/material.dart';
@ -15,6 +26,28 @@ class ProfileButton extends StatelessWidget {
final bool pMode =
Provider.of<SettingsProvider>(context, listen: false).presentationMode;
late UserProvider user;
late User? account;
Future<void> restore() => Future.wait([
Provider.of<GradeProvider>(context, listen: false).restore(),
Provider.of<TimetableProvider>(context, listen: false).restoreUser(),
Provider.of<ExamProvider>(context, listen: false).restore(),
Provider.of<HomeworkProvider>(context, listen: false).restore(),
Provider.of<MessageProvider>(context, listen: false).restore(),
Provider.of<NoteProvider>(context, listen: false).restore(),
Provider.of<EventProvider>(context, listen: false).restore(),
Provider.of<AbsenceProvider>(context, listen: false).restore(),
Provider.of<KretaClient>(context, listen: false).refreshLogin(),
]);
user = Provider.of<UserProvider>(context);
try {
account = user.getUsers()[1];
} catch (err) {
account = null;
}
return ProfileImage(
backgroundColor: !pMode
? child.backgroundColor
@ -48,6 +81,12 @@ class ProfileButton extends StatelessWidget {
),
);
},
onDoubleTap: () {
if (account != null) {
user.setUser(account.id);
restore().then((_) => user.setUser(account!.id));
}
},
);
}
}

View File

@ -10,6 +10,8 @@ class ProfileImage extends StatefulWidget {
const ProfileImage({
Key? key,
this.onTap,
this.onDoubleTap,
this.onLongPress,
this.name,
this.backgroundColor,
this.radius = 20.0,
@ -21,6 +23,8 @@ class ProfileImage extends StatefulWidget {
}) : super(key: key);
final void Function()? onTap;
final void Function()? onDoubleTap;
final void Function()? onLongPress;
final String? name;
final Color? backgroundColor;
final double radius;
@ -46,7 +50,10 @@ class _ProfileImageState extends State<ProfileImage> {
void updatePic() {
profilePicture = widget.profilePictureString != ""
? Image.memory(const Base64Decoder().convert(widget.profilePictureString), fit: BoxFit.scaleDown, gaplessPlayback: true)
? Image.memory(
const Base64Decoder().convert(widget.profilePictureString),
fit: BoxFit.scaleDown,
gaplessPlayback: true)
: null;
profPicSaved = widget.profilePictureString;
}
@ -63,7 +70,8 @@ class _ProfileImageState extends State<ProfileImage> {
}
Widget buildWithoutHero(BuildContext context) {
Color color = ColorUtils.foregroundColor(widget.backgroundColor ?? Theme.of(context).scaffoldBackgroundColor);
Color color = ColorUtils.foregroundColor(
widget.backgroundColor ?? Theme.of(context).scaffoldBackgroundColor);
Color roleColor;
if (Theme.of(context).brightness == Brightness.light) {
@ -78,9 +86,12 @@ class _ProfileImageState extends State<ProfileImage> {
Material(
clipBehavior: Clip.hardEdge,
shape: const CircleBorder(),
color: widget.backgroundColor ?? AppColors.of(context).text.withOpacity(.15),
color: widget.backgroundColor ??
AppColors.of(context).text.withOpacity(.15),
child: InkWell(
onTap: widget.onTap,
onDoubleTap: widget.onDoubleTap,
onLongPress: widget.onLongPress,
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
height: widget.radius * 2,
@ -88,7 +99,8 @@ class _ProfileImageState extends State<ProfileImage> {
decoration: const BoxDecoration(
shape: BoxShape.circle,
),
child: widget.name != null && (widget.name?.trim().length ?? 0) > 0
child:
widget.name != null && (widget.name?.trim().length ?? 0) > 0
? Center(
child: widget.censored
? Container(
@ -101,7 +113,9 @@ class _ProfileImageState extends State<ProfileImage> {
)
: profilePicture ??
Text(
(widget.name?.trim().length ?? 0) > 0 ? (widget.name ?? "?").trim()[0] : "?",
(widget.name?.trim().length ?? 0) > 0
? (widget.name ?? "?").trim()[0]
: "?",
style: TextStyle(
color: color,
fontWeight: FontWeight.w600,
@ -121,7 +135,8 @@ class _ProfileImageState extends State<ProfileImage> {
width: widget.radius * 2,
child: Container(
alignment: Alignment.bottomRight,
child: Icon(Icons.shield, color: roleColor, size: widget.radius / 1.3),
child: Icon(Icons.shield,
color: roleColor, size: widget.radius / 1.3),
),
),
],
@ -129,7 +144,8 @@ class _ProfileImageState extends State<ProfileImage> {
}
Widget buildWithHero(BuildContext context) {
Color color = ColorUtils.foregroundColor(widget.backgroundColor ?? Theme.of(context).scaffoldBackgroundColor);
Color color = ColorUtils.foregroundColor(
widget.backgroundColor ?? Theme.of(context).scaffoldBackgroundColor);
Color roleColor;
if (Theme.of(context).brightness == Brightness.light) {
@ -141,7 +157,9 @@ class _ProfileImageState extends State<ProfileImage> {
Widget child = FittedBox(
fit: BoxFit.fitHeight,
child: Text(
(widget.name?.trim().length ?? 0) > 0 ? (widget.name ?? "?").trim()[0] : "?",
(widget.name?.trim().length ?? 0) > 0
? (widget.name ?? "?").trim()[0]
: "?",
style: TextStyle(
color: color,
fontWeight: FontWeight.w600,
@ -163,7 +181,10 @@ class _ProfileImageState extends State<ProfileImage> {
child: Material(
clipBehavior: Clip.hardEdge,
shape: const CircleBorder(),
color: profilePicture != null ? Colors.transparent : widget.backgroundColor ?? AppColors.of(context).text.withOpacity(.15),
color: profilePicture != null
? Colors.transparent
: widget.backgroundColor ??
AppColors.of(context).text.withOpacity(.15),
child: AnimatedContainer(
duration: const Duration(milliseconds: 200),
height: widget.radius * 2,
@ -204,7 +225,8 @@ class _ProfileImageState extends State<ProfileImage> {
width: widget.radius * 2,
child: Container(
alignment: Alignment.bottomRight,
child: Icon(Icons.shield, color: roleColor, size: widget.radius / 1.3),
child: Icon(Icons.shield,
color: roleColor, size: widget.radius / 1.3),
),
),
),
@ -216,6 +238,8 @@ class _ProfileImageState extends State<ProfileImage> {
shape: const CircleBorder(),
child: InkWell(
onTap: widget.onTap,
onDoubleTap: widget.onDoubleTap,
onLongPress: widget.onLongPress,
child: SizedBox(
height: widget.radius * 2,
width: widget.radius * 2,

View File

@ -1,5 +1,4 @@
import 'package:filcnaplo/models/user.dart';
import 'package:filcnaplo/utils/color.dart';
import 'package:filcnaplo_mobile_ui/common/bottom_card.dart';
import 'package:filcnaplo_mobile_ui/common/detail.dart';
import 'package:filcnaplo_mobile_ui/common/profile_image/profile_image.dart';
@ -13,7 +12,8 @@ class AccountView extends StatelessWidget {
final User user;
static void show(User user, {required BuildContext context}) => showBottomCard(context: context, child: AccountView(user));
static void show(User user, {required BuildContext context}) =>
showBottomCard(context: context, child: AccountView(user));
@override
Widget build(BuildContext context) {
@ -28,7 +28,7 @@ class AccountView extends StatelessWidget {
AccountTile(
profileImage: ProfileImage(
name: _firstName,
backgroundColor: ColorUtils.stringToColor(user.name),
backgroundColor: Theme.of(context).colorScheme.primary,
role: user.role,
),
name: SelectableText(
@ -41,12 +41,19 @@ class AccountView extends StatelessWidget {
),
// User details
Detail(title: "birthdate".i18n, description: DateFormat("yyyy. MM. dd.").format(user.student.birth)),
Detail(
title: "birthdate".i18n,
description:
DateFormat("yyyy. MM. dd.").format(user.student.birth)),
Detail(title: "school".i18n, description: user.student.school.name),
if (user.student.className != null) Detail(title: "class".i18n, description: user.student.className!),
if (user.student.address != null) Detail(title: "address".i18n, description: user.student.address!),
if (user.student.className != null)
Detail(title: "class".i18n, description: user.student.className!),
if (user.student.address != null)
Detail(title: "address".i18n, description: user.student.address!),
if (user.student.parents.isNotEmpty)
Detail(title: "parents".plural(user.student.parents.length), description: user.student.parents.join(", ")),
Detail(
title: "parents".plural(user.student.parents.length),
description: user.student.parents.join(", ")),
],
),
);