Preset statuses

This commit is contained in:
LittleSheep 2024-06-27 01:33:03 +08:00
parent 834eed652d
commit 42acffff3e
9 changed files with 286 additions and 111 deletions

View File

@ -15,7 +15,7 @@ import 'package:solian/providers/content/channel.dart';
import 'package:solian/providers/content/post.dart'; import 'package:solian/providers/content/post.dart';
import 'package:solian/providers/content/realm.dart'; import 'package:solian/providers/content/realm.dart';
import 'package:solian/providers/friend.dart'; import 'package:solian/providers/friend.dart';
import 'package:solian/providers/status.dart'; import 'package:solian/providers/account_status.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:solian/theme.dart'; import 'package:solian/theme.dart';
import 'package:solian/translations.dart'; import 'package:solian/translations.dart';
@ -80,7 +80,7 @@ class SolianApp extends StatelessWidget {
Get.lazyPut(() => AttachmentProvider()); Get.lazyPut(() => AttachmentProvider());
Get.lazyPut(() => ChatProvider()); Get.lazyPut(() => ChatProvider());
Get.lazyPut(() => AccountProvider()); Get.lazyPut(() => AccountProvider());
Get.lazyPut(() => StatusController()); Get.lazyPut(() => StatusProvider());
Get.lazyPut(() => ChannelProvider()); Get.lazyPut(() => ChannelProvider());
Get.lazyPut(() => RealmProvider()); Get.lazyPut(() => RealmProvider());
Get.lazyPut(() => ChatCallProvider()); Get.lazyPut(() => ChatCallProvider());

View File

@ -2,7 +2,7 @@ class AccountStatus {
bool isDisturbable; bool isDisturbable;
bool isOnline; bool isOnline;
DateTime? lastSeenAt; DateTime? lastSeenAt;
dynamic status; Status? status;
AccountStatus({ AccountStatus({
required this.isDisturbable, required this.isDisturbable,
@ -15,13 +15,69 @@ class AccountStatus {
isDisturbable: json['is_disturbable'], isDisturbable: json['is_disturbable'],
isOnline: json['is_online'], isOnline: json['is_online'],
lastSeenAt: json['last_seen_at'] != null ? DateTime.parse(json['last_seen_at']) : null, lastSeenAt: json['last_seen_at'] != null ? DateTime.parse(json['last_seen_at']) : null,
status: json['status'], status: json['status'] != null ? Status.fromJson(json['status']) : null,
); );
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'is_disturbable': isDisturbable, 'is_disturbable': isDisturbable,
'is_online': isOnline, 'is_online': isOnline,
'last_seen_at': lastSeenAt?.toIso8601String(), 'last_seen_at': lastSeenAt?.toIso8601String(),
'status': status, 'status': status?.toJson(),
};
}
class Status {
int id;
DateTime createdAt;
DateTime updatedAt;
DateTime? deletedAt;
String type;
String label;
int attitude;
bool isNoDisturb;
bool isInvisible;
DateTime? clearAt;
int accountId;
Status({
required this.id,
required this.createdAt,
required this.updatedAt,
required this.deletedAt,
required this.type,
required this.label,
required this.attitude,
required this.isNoDisturb,
required this.isInvisible,
required this.clearAt,
required this.accountId,
});
factory Status.fromJson(Map<String, dynamic> json) => Status(
id: json['id'],
createdAt: DateTime.parse(json['created_at']),
updatedAt: DateTime.parse(json['updated_at']),
deletedAt: json['deleted_at'] != null ? DateTime.parse(json['deleted_at']) : null,
type: json['type'],
label: json['label'],
attitude: json['attitude'],
isNoDisturb: json['is_no_disturb'],
isInvisible: json['is_invisible'],
clearAt: json['clear_at'] != null ? DateTime.parse(json['clear_at']) : null,
accountId: json['account_id'],
);
Map<String, dynamic> toJson() => {
'id': id,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
'deleted_at': deletedAt?.toIso8601String(),
'type': type,
'label': label,
'attitude': attitude,
'is_no_disturb': isNoDisturb,
'is_invisible': isInvisible,
'clear_at': clearAt?.toIso8601String(),
'account_id': accountId,
}; };
} }

View File

@ -0,0 +1,117 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/models/account_status.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
class StatusProvider extends GetConnect {
static Map<String, (Widget, String, String?)> presetStatuses = {
'online': (
const Icon(Icons.circle, color: Colors.green),
'accountStatusOnline'.tr,
null,
),
'silent': (
const Icon(Icons.do_not_disturb_on, color: Colors.red),
'accountStatusSilent'.tr,
'accountStatusSilentDesc'.tr,
),
'invisible': (
const Icon(Icons.circle, color: Colors.grey),
'accountStatusInvisible'.tr,
'accountStatusInvisibleDesc'.tr,
),
};
@override
void onInit() {
final AuthProvider auth = Get.find();
httpClient.baseUrl = ServiceFinder.services['passport'];
httpClient.addAuthenticator(auth.requestAuthenticator);
}
Future<Response> getCurrentStatus() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
return await client.get('/api/users/me/status');
}
Future<Response> getSomeoneStatus(String name) =>
get('/api/users/$name/status');
Future<Response> setStatus(
String type,
String? label,
int attitude, {
bool isUpdate = false,
bool isSilent = false,
bool isInvisible = false,
DateTime? clearAt,
}) async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final payload = {
'type': type,
'label': label,
'is_no_disturb': isSilent,
'is_invisible': isInvisible,
'clear_at': clearAt?.toUtc().toIso8601String()
};
Response resp;
if (!isUpdate) {
resp = await client.post('/api/users/me/status', payload);
} else {
resp = await client.put('/api/users/me/status', payload);
}
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
Future<Response> clearStatus() async {
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) throw Exception('unauthorized');
final client = auth.configureClient('passport');
final resp = await client.delete('/api/users/me/status');
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
static (Widget, String) determineStatus(AccountStatus status,
{double size = 14}) {
Widget icon;
String? text;
if (!presetStatuses.keys.contains(status.status?.type)) {
text = status.status?.label;
}
if (status.isDisturbable && status.isOnline) {
icon = Icon(Icons.circle, color: Colors.green, size: size);
text ??= 'accountStatusOnline'.tr;
} else if (!status.isDisturbable && status.isOnline) {
icon = Icon(Icons.do_not_disturb_on, color: Colors.red, size: size);
text ??= 'accountStatusSilent'.tr;
} else {
icon = Icon(Icons.circle, color: Colors.grey, size: size);
text ??= 'accountStatusOffline'.tr;
}
return (icon, text);
}
}

View File

@ -1,56 +0,0 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:solian/models/account_status.dart';
import 'package:solian/providers/auth.dart';
import 'package:solian/services.dart';
class StatusController extends GetConnect {
static Map<String, (Widget, String, String?)> presetStatuses = {
'online': (
const Icon(Icons.circle, color: Colors.green),
'accountStatusOnline'.tr,
null,
),
'silent': (
const Icon(Icons.do_not_disturb_on, color: Colors.red),
'accountStatusSilent'.tr,
'accountStatusSilentDesc'.tr,
),
'invisible': (
const Icon(Icons.circle, color: Colors.grey),
'accountStatusInvisible'.tr,
'accountStatusInvisibleDesc'.tr,
),
};
@override
void onInit() {
final AuthProvider auth = Get.find();
httpClient.baseUrl = ServiceFinder.services['passport'];
httpClient.addAuthenticator(auth.requestAuthenticator);
}
Future<Response> getCurrentStatus() => get('/api/users/me/status');
Future<Response> getSomeoneStatus(String name) =>
get('/api/users/$name/status');
static (Widget, String) determineStatus(AccountStatus status,
{double size = 14}) {
Widget icon;
String? text = status.status?.label;
if (status.isDisturbable && status.isOnline) {
icon = Icon(Icons.circle, color: Colors.green, size: size);
text ??= 'accountStatusOnline'.tr;
} else if (!status.isDisturbable && status.isOnline) {
icon = Icon(Icons.do_not_disturb_on, color: Colors.red, size: size);
text ??= 'accountStatusSilent'.tr;
} else {
icon = Icon(Icons.circle, color: Colors.grey, size: size);
text ??= 'accountStatusOffline'.tr;
}
return (icon, text);
}
}

View File

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/models/account.dart'; import 'package:solian/models/account.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/providers/status.dart'; import 'package:solian/providers/account_status.dart';
import 'package:solian/router.dart'; import 'package:solian/router.dart';
import 'package:solian/screens/auth/signin.dart'; import 'package:solian/screens/auth/signin.dart';
import 'package:solian/screens/auth/signup.dart'; import 'package:solian/screens/auth/signup.dart';
@ -112,9 +112,22 @@ class _AccountScreenState extends State<AccountScreen> {
} }
} }
class AccountHeading extends StatelessWidget { class AccountHeading extends StatefulWidget {
const AccountHeading({super.key}); const AccountHeading({super.key});
@override
State<AccountHeading> createState() => _AccountHeadingState();
}
class _AccountHeadingState extends State<AccountHeading> {
late Future<Response> _status;
@override
void initState() {
_status = Get.find<StatusProvider>().getCurrentStatus();
super.initState();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final AuthProvider provider = Get.find(); final AuthProvider provider = Get.find();
@ -133,11 +146,16 @@ class AccountHeading extends StatelessWidget {
name: prof.body['name'], name: prof.body['name'],
nick: prof.body['nick'], nick: prof.body['nick'],
desc: prof.body['description'], desc: prof.body['description'],
status: Get.find<StatusController>().getCurrentStatus(), status: _status,
badges: prof.body['badges'] badges: prof.body['badges']
?.map((e) => AccountBadge.fromJson(e)) ?.map((e) => AccountBadge.fromJson(e))
.toList() .toList()
.cast<AccountBadge>(), .cast<AccountBadge>(),
onEditStatus: () {
setState(() {
_status = Get.find<StatusProvider>().getCurrentStatus();
});
},
); );
}, },
); );

View File

@ -362,10 +362,7 @@ class _ChannelChatScreenState extends State<ChannelChatScreen> {
} }
return SliverToBoxAdapter( return SliverToBoxAdapter(
child: const LinearProgressIndicator() child: const LinearProgressIndicator().animate().slideY(),
.animate()
.slideY()
.paddingOnly(bottom: 4),
); );
}), }),
], ],

View File

@ -3,7 +3,7 @@ import 'package:get/get.dart';
import 'package:solian/models/account.dart'; import 'package:solian/models/account.dart';
import 'package:solian/models/account_status.dart'; import 'package:solian/models/account_status.dart';
import 'package:solian/platform.dart'; import 'package:solian/platform.dart';
import 'package:solian/providers/status.dart'; import 'package:solian/providers/account_status.dart';
import 'package:solian/widgets/account/account_avatar.dart'; import 'package:solian/widgets/account/account_avatar.dart';
import 'package:solian/widgets/account/account_badge.dart'; import 'package:solian/widgets/account/account_badge.dart';
import 'package:solian/widgets/account/account_status_action.dart'; import 'package:solian/widgets/account/account_status_action.dart';
@ -32,11 +32,15 @@ class AccountHeadingWidget extends StatelessWidget {
}); });
void showStatusAction(BuildContext context, bool hasStatus) { void showStatusAction(BuildContext context, bool hasStatus) {
if (onEditStatus == null) return;
showModalBottomSheet( showModalBottomSheet(
useRootNavigator: true, useRootNavigator: true,
context: context, context: context,
builder: (context) => AccountStatusAction(hasStatus: hasStatus), builder: (context) => AccountStatusAction(hasStatus: hasStatus),
); ).then((val) {
if(val == true) onEditStatus!();
});
} }
@override @override
@ -102,7 +106,7 @@ class AccountHeadingWidget extends StatelessWidget {
return Text('loading'.tr); return Text('loading'.tr);
} }
final info = StatusController.determineStatus( final info = StatusProvider.determineStatus(
AccountStatus.fromJson(snapshot.data!.body), AccountStatus.fromJson(snapshot.data!.body),
); );

View File

@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/exts.dart'; import 'package:solian/exts.dart';
import 'package:solian/models/account.dart'; import 'package:solian/models/account.dart';
import 'package:solian/providers/status.dart'; import 'package:solian/providers/account_status.dart';
import 'package:solian/services.dart'; import 'package:solian/services.dart';
import 'package:solian/widgets/account/account_heading.dart'; import 'package:solian/widgets/account/account_heading.dart';
@ -61,7 +61,7 @@ class _AccountProfilePopupState extends State<AccountProfilePopup> {
nick: _userinfo!.nick, nick: _userinfo!.nick,
desc: _userinfo!.description, desc: _userinfo!.description,
badges: _userinfo!.badges, badges: _userinfo!.badges,
status: Get.find<StatusController>().getSomeoneStatus(_userinfo!.name), status: Get.find<StatusProvider>().getSomeoneStatus(_userinfo!.name),
).paddingOnly(top: 16), ).paddingOnly(top: 16),
], ],
), ),

View File

@ -1,14 +1,25 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:solian/providers/status.dart'; import 'package:solian/exts.dart';
import 'package:solian/providers/account_status.dart';
class AccountStatusAction extends StatelessWidget { class AccountStatusAction extends StatefulWidget {
final bool hasStatus; final bool hasStatus;
const AccountStatusAction({super.key, this.hasStatus = false}); const AccountStatusAction({super.key, this.hasStatus = false});
@override
State<AccountStatusAction> createState() => _AccountStatusActionState();
}
class _AccountStatusActionState extends State<AccountStatusAction> {
bool _isBusy = false;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final StatusProvider controller = Get.find();
return SizedBox( return SizedBox(
child: Column( child: Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
@ -17,44 +28,72 @@ class AccountStatusAction extends StatelessWidget {
'accountChangeStatus'.tr, 'accountChangeStatus'.tr,
style: Theme.of(context).textTheme.headlineSmall, style: Theme.of(context).textTheme.headlineSmall,
).paddingOnly(left: 24, right: 24, top: 32, bottom: 16), ).paddingOnly(left: 24, right: 24, top: 32, bottom: 16),
Expanded( if (_isBusy)
const LinearProgressIndicator()
.paddingOnly(bottom: 14)
.animate()
.slideY(curve: Curves.fastEaseInToSlowEaseOut),
SizedBox(
height: 48,
child: ListView( child: ListView(
children: [ scrollDirection: Axis.horizontal,
SizedBox( children: StatusProvider.presetStatuses.entries
height: 48, .map(
child: ListView( (x) => ActionChip(
scrollDirection: Axis.horizontal, avatar: x.value.$1,
children: StatusController.presetStatuses.entries label: Text(x.value.$2),
.map( tooltip: x.value.$3,
(x) => ActionChip( onPressed: _isBusy
avatar: x.value.$1, ? null
label: Text(x.value.$2), : () async {
tooltip: x.value.$3, setState(() => _isBusy = true);
onPressed: () {}, try {
).paddingOnly(right: 6), await controller.setStatus(
) x.key,
.toList(), x.value.$2,
).paddingSymmetric(horizontal: 18), 0,
), isInvisible: x.key == 'invisible',
const Divider(thickness: 0.3, height: 0.3) isSilent: x.key == 'silent',
.paddingSymmetric(vertical: 16), );
ListTile( Navigator.of(context).pop(true);
contentPadding: const EdgeInsets.symmetric(horizontal: 24), } catch (e) {
leading: hasStatus context.showErrorDialog(e);
? const Icon(Icons.edit) }
: const Icon(Icons.add), setState(() => _isBusy = false);
title: Text('accountCustomStatus'.tr), },
onTap: () {}, ).paddingOnly(right: 6),
), )
ListTile( .toList(),
contentPadding: const EdgeInsets.symmetric(horizontal: 24), ).paddingSymmetric(horizontal: 18),
leading: const Icon(Icons.clear),
title: Text('accountClearStatus'.tr),
onTap: () {},
),
],
),
), ),
const Divider(thickness: 0.3, height: 0.3)
.paddingSymmetric(vertical: 16),
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: widget.hasStatus
? const Icon(Icons.edit)
: const Icon(Icons.add),
title: Text('accountCustomStatus'.tr),
onTap: _isBusy ? null : () {},
),
if (widget.hasStatus)
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Icons.clear),
title: Text('accountClearStatus'.tr),
onTap: _isBusy
? null
: () async {
setState(() => _isBusy = true);
try {
await controller.clearStatus();
Navigator.of(context).pop(true);
} catch (e) {
context.showErrorDialog(e);
}
setState(() => _isBusy = false);
},
),
], ],
), ),
); );