2023-10-03 13:00:59 -04:00
|
|
|
import 'package:recon/apis/contact_api.dart';
|
|
|
|
import 'package:recon/auxiliary.dart';
|
|
|
|
import 'package:recon/client_holder.dart';
|
|
|
|
import 'package:recon/models/users/user.dart';
|
|
|
|
import 'package:recon/widgets/generic_avatar.dart';
|
2023-04-30 09:43:59 -04:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
|
|
|
|
class UserListTile extends StatefulWidget {
|
2023-05-06 13:01:15 -04:00
|
|
|
const UserListTile({required this.user, required this.isFriend, required this.onChanged, super.key});
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
final User user;
|
2023-05-04 13:04:33 -04:00
|
|
|
final bool isFriend;
|
2023-05-06 13:01:15 -04:00
|
|
|
final Function()? onChanged;
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<UserListTile> createState() => _UserListTileState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _UserListTileState extends State<UserListTile> {
|
|
|
|
final DateFormat _regDateFormat = DateFormat.yMMMMd('en_US');
|
2023-05-04 13:04:33 -04:00
|
|
|
late bool _localAdded = widget.isFriend;
|
2023-05-04 14:57:16 -04:00
|
|
|
bool _loading = false;
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-05 07:15:20 -04:00
|
|
|
final colorScheme = Theme
|
|
|
|
.of(context)
|
|
|
|
.colorScheme;
|
|
|
|
final style = _localAdded ? IconButton.styleFrom(
|
|
|
|
foregroundColor: colorScheme.onBackground,
|
|
|
|
side: BorderSide(
|
|
|
|
color: colorScheme.error,
|
|
|
|
width: 2
|
|
|
|
),
|
|
|
|
) : IconButton.styleFrom(
|
|
|
|
foregroundColor: colorScheme.onBackground,
|
|
|
|
side: BorderSide(
|
|
|
|
color: colorScheme.primary,
|
|
|
|
width: 2
|
|
|
|
),
|
|
|
|
);
|
2023-04-30 09:43:59 -04:00
|
|
|
return ListTile(
|
2023-09-29 03:51:46 -04:00
|
|
|
leading: GenericAvatar(imageUri: Aux.resdbToHttp(widget.user.userProfile?.iconUrl),),
|
2023-04-30 09:43:59 -04:00
|
|
|
title: Text(widget.user.username),
|
|
|
|
subtitle: Text(_regDateFormat.format(widget.user.registrationDate)),
|
|
|
|
trailing: IconButton(
|
2023-05-05 07:15:20 -04:00
|
|
|
splashRadius: 24,
|
|
|
|
iconSize: 20,
|
|
|
|
icon: _localAdded ? const Icon(Icons.person_remove) : const Icon(Icons.person_add),
|
|
|
|
style: style,
|
2023-05-04 14:57:16 -04:00
|
|
|
onPressed: _loading ? null : () async {
|
2023-10-10 05:07:54 -04:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text("Sorry, this feature is not yet available")));
|
|
|
|
return;
|
2023-04-30 09:43:59 -04:00
|
|
|
setState(() {
|
2023-05-04 14:57:16 -04:00
|
|
|
_loading = true;
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
if (_localAdded) {
|
2023-09-29 07:17:17 -04:00
|
|
|
await ContactApi.removeUserAsFriend(ClientHolder
|
2023-05-05 07:15:20 -04:00
|
|
|
.of(context)
|
|
|
|
.apiClient, user: widget.user);
|
2023-05-04 14:57:16 -04:00
|
|
|
} else {
|
2023-09-29 07:17:17 -04:00
|
|
|
await ContactApi.addUserAsFriend(ClientHolder
|
2023-05-05 07:15:20 -04:00
|
|
|
.of(context)
|
|
|
|
.apiClient, user: widget.user);
|
2023-05-04 14:57:16 -04:00
|
|
|
}
|
2023-05-06 13:01:15 -04:00
|
|
|
setState(() {
|
|
|
|
_loading = false;
|
|
|
|
_localAdded = !_localAdded;
|
|
|
|
});
|
|
|
|
widget.onChanged?.call();
|
2023-05-04 14:57:16 -04:00
|
|
|
} catch (e, s) {
|
|
|
|
FlutterError.reportError(FlutterErrorDetails(exception: e, stack: s));
|
2023-09-30 08:20:08 -04:00
|
|
|
if (context.mounted) {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2023-05-04 14:57:16 -04:00
|
|
|
SnackBar(
|
|
|
|
duration: const Duration(seconds: 5),
|
|
|
|
content: Text(
|
|
|
|
"Something went wrong: $e",
|
|
|
|
softWrap: true,
|
|
|
|
maxLines: null,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2023-09-30 08:20:08 -04:00
|
|
|
}
|
2023-05-04 14:57:16 -04:00
|
|
|
setState(() {
|
|
|
|
_loading = false;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2023-04-30 09:43:59 -04:00
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|