Solian/lib/screens/account/personalize.dart

364 lines
11 KiB
Dart
Raw Normal View History

2024-05-22 15:18:01 +00:00
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_animate/flutter_animate.dart';
import 'package:gap/gap.dart';
2024-05-22 15:18:01 +00:00
import 'package:get/get.dart';
2024-08-01 16:12:16 +00:00
import 'package:image_cropper/image_cropper.dart';
2024-05-22 15:18:01 +00:00
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:solian/exts.dart';
import 'package:solian/models/attachment.dart';
2024-05-22 15:18:01 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/content/attachment.dart';
import 'package:solian/services.dart';
import 'package:solian/widgets/account/account_avatar.dart';
class PersonalizeScreen extends StatefulWidget {
const PersonalizeScreen({super.key});
@override
State<PersonalizeScreen> createState() => _PersonalizeScreenState();
}
class _PersonalizeScreenState extends State<PersonalizeScreen> {
final _imagePicker = ImagePicker();
final _usernameController = TextEditingController();
final _nicknameController = TextEditingController();
final _firstNameController = TextEditingController();
final _lastNameController = TextEditingController();
final _descriptionController = TextEditingController();
final _birthdayController = TextEditingController();
2024-08-21 16:42:17 +00:00
String? _avatar;
String? _banner;
2024-05-22 15:18:01 +00:00
DateTime? _birthday;
bool _isBusy = false;
void _selectBirthday() async {
2024-05-22 15:18:01 +00:00
final DateTime? picked = await showDatePicker(
context: context,
2024-05-22 16:11:54 +00:00
initialDate: _birthday?.toLocal(),
2024-05-22 15:18:01 +00:00
firstDate: DateTime(DateTime.now().year - 200),
2024-05-22 16:11:54 +00:00
lastDate: DateTime(DateTime.now().year),
2024-05-22 15:18:01 +00:00
);
if (picked != null && picked != _birthday) {
setState(() {
_birthday = picked;
2024-06-27 03:44:27 +00:00
_birthdayController.text = DateFormat('y/M/d').format(_birthday!);
2024-05-22 15:18:01 +00:00
});
}
}
void _syncWidget() async {
2024-08-01 16:12:16 +00:00
_isBusy = true;
2024-05-22 15:18:01 +00:00
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
final prof = auth.userProfile.value!;
2024-08-01 16:12:16 +00:00
_usernameController.text = prof['name'];
_nicknameController.text = prof['nick'];
_descriptionController.text = prof['description'];
_firstNameController.text = prof['profile']['first_name'];
_lastNameController.text = prof['profile']['last_name'];
_avatar = prof['avatar'];
_banner = prof['banner'];
if (prof['profile']['birthday'] != null) {
_birthday = DateTime.parse(prof['profile']['birthday']);
_birthdayController.text =
DateFormat('yyyy-MM-dd').format(_birthday!.toLocal());
}
_isBusy = false;
2024-05-22 15:18:01 +00:00
}
Future<void> _editImage(String position) async {
2024-05-22 15:18:01 +00:00
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return;
2024-05-22 15:18:01 +00:00
final image = await _imagePicker.pickImage(source: ImageSource.gallery);
if (image == null) return;
2024-08-01 16:12:16 +00:00
CroppedFile? croppedFile = await ImageCropper().cropImage(
sourcePath: image.path,
uiSettings: [
AndroidUiSettings(
toolbarTitle: 'cropImage'.tr,
toolbarColor: Theme.of(context).colorScheme.primary,
toolbarWidgetColor: Theme.of(context).colorScheme.onPrimary,
aspectRatioPresets: [
if (position == 'avatar') CropAspectRatioPreset.square,
if (position == 'banner') _BannerCropAspectRatioPreset(),
],
2024-08-01 16:12:16 +00:00
),
IOSUiSettings(
title: 'cropImage'.tr,
aspectRatioPresets: [
if (position == 'avatar') CropAspectRatioPreset.square,
if (position == 'banner') _BannerCropAspectRatioPreset(),
],
2024-08-01 16:12:16 +00:00
),
WebUiSettings(
context: context,
),
],
);
if (croppedFile == null) return;
final file = File(croppedFile.path);
2024-05-22 15:18:01 +00:00
setState(() => _isBusy = true);
final AttachmentProvider attach = Get.find();
2024-05-22 15:18:01 +00:00
Attachment? attachResult;
2024-05-22 15:18:01 +00:00
try {
attachResult = await attach.createAttachmentDirectly(
2024-08-01 16:12:16 +00:00
await file.readAsBytes(),
file.path,
'avatar',
2024-08-01 16:12:16 +00:00
null,
);
2024-05-22 15:18:01 +00:00
} catch (e) {
setState(() => _isBusy = false);
context.showErrorDialog(e);
2024-06-23 11:41:50 +00:00
return;
2024-05-22 15:18:01 +00:00
}
final client = auth.configureClient('auth');
2024-05-22 15:18:01 +00:00
final resp = await client.put(
'/users/me/$position',
2024-08-21 16:42:17 +00:00
{'attachment': attachResult.rid},
2024-05-22 15:18:01 +00:00
);
if (resp.statusCode == 200) {
_syncWidget();
2024-05-22 16:11:54 +00:00
context.showSnackbar('accountPersonalizeApplied'.tr);
} else {
context.showErrorDialog(resp.bodyString);
}
setState(() => _isBusy = false);
}
void _editUserInfo() async {
2024-05-22 16:11:54 +00:00
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return;
2024-05-22 15:18:01 +00:00
2024-05-22 16:11:54 +00:00
setState(() => _isBusy = true);
final client = auth.configureClient('auth');
2024-05-22 16:11:54 +00:00
_birthday?.toIso8601String();
final resp = await client.put(
'/users/me',
2024-05-22 16:11:54 +00:00
{
'nick': _nicknameController.value.text,
'description': _descriptionController.value.text,
'first_name': _firstNameController.value.text,
'last_name': _lastNameController.value.text,
'birthday': _birthday?.toUtc().toIso8601String(),
},
);
if (resp.statusCode == 200) {
_syncWidget();
2024-05-22 15:18:01 +00:00
context.showSnackbar('accountPersonalizeApplied'.tr);
} else {
context.showErrorDialog(resp.bodyString);
}
setState(() => _isBusy = false);
}
@override
void initState() {
super.initState();
2024-08-01 16:12:16 +00:00
_syncWidget();
2024-05-22 15:18:01 +00:00
}
@override
Widget build(BuildContext context) {
2024-06-03 15:36:46 +00:00
const double padding = 32;
2024-05-22 15:18:01 +00:00
return Material(
color: Theme.of(context).colorScheme.surface,
2024-06-03 15:36:46 +00:00
child: ListView(
children: [
if (_isBusy) const LinearProgressIndicator().animate().scaleX(),
const Gap(24),
2024-06-03 15:36:46 +00:00
Stack(
children: [
AccountAvatar(content: _avatar, radius: 40),
Positioned(
bottom: 0,
left: 40,
child: FloatingActionButton.small(
heroTag: const Key('avatar-editor'),
onPressed: () => _editImage('avatar'),
2024-06-03 15:36:46 +00:00
child: const Icon(
Icons.camera,
2024-05-22 15:18:01 +00:00
),
),
2024-06-03 15:36:46 +00:00
),
],
).paddingSymmetric(horizontal: padding),
const Gap(16),
2024-06-03 15:36:46 +00:00
Stack(
children: [
ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: AspectRatio(
aspectRatio: 16 / 9,
child: Container(
color: Theme.of(context).colorScheme.surfaceContainerHigh,
child: _banner != null
? Image.network(
ServiceFinder.buildUrl(
'files', '/attachments/$_banner'),
2024-06-03 15:36:46 +00:00
fit: BoxFit.cover,
loadingBuilder: (BuildContext context, Widget child,
ImageChunkEvent? loadingProgress) {
if (loadingProgress == null) return child;
return Center(
child: CircularProgressIndicator(
value: loadingProgress.expectedTotalBytes !=
null
? loadingProgress.cumulativeBytesLoaded /
loadingProgress.expectedTotalBytes!
: null,
),
);
},
)
: Container(),
2024-05-22 15:18:01 +00:00
),
),
2024-06-03 15:36:46 +00:00
),
Positioned(
bottom: 16,
right: 16,
child: FloatingActionButton(
heroTag: const Key('banner-editor'),
onPressed: () => _editImage('banner'),
2024-06-03 15:36:46 +00:00
child: const Icon(
Icons.camera_alt,
2024-05-22 15:18:01 +00:00
),
),
2024-06-03 15:36:46 +00:00
),
],
).paddingSymmetric(horizontal: padding),
const Gap(24),
2024-06-03 15:36:46 +00:00
Row(
children: [
Flexible(
flex: 1,
child: TextField(
readOnly: true,
controller: _usernameController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'username'.tr,
prefixText: '@',
2024-05-22 15:18:01 +00:00
),
),
2024-06-03 15:36:46 +00:00
),
const Gap(16),
2024-06-03 15:36:46 +00:00
Flexible(
flex: 1,
child: TextField(
controller: _nicknameController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'nickname'.tr,
2024-05-22 15:18:01 +00:00
),
),
2024-06-03 15:36:46 +00:00
),
],
).paddingSymmetric(horizontal: padding),
const Gap(16),
2024-06-03 15:36:46 +00:00
Row(
children: [
Flexible(
flex: 1,
child: TextField(
controller: _firstNameController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'firstName'.tr,
2024-05-22 15:18:01 +00:00
),
),
2024-06-03 15:36:46 +00:00
),
const Gap(16),
2024-06-03 15:36:46 +00:00
Flexible(
flex: 1,
child: TextField(
controller: _lastNameController,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'lastName'.tr,
2024-05-22 15:18:01 +00:00
),
),
),
2024-06-03 15:36:46 +00:00
],
).paddingSymmetric(horizontal: padding),
const Gap(16),
2024-06-03 15:36:46 +00:00
TextField(
controller: _descriptionController,
keyboardType: TextInputType.multiline,
maxLines: null,
minLines: 3,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'description'.tr,
2024-05-22 15:18:01 +00:00
),
2024-06-03 15:36:46 +00:00
).paddingSymmetric(horizontal: padding),
const Gap(16),
2024-06-03 15:36:46 +00:00
TextField(
controller: _birthdayController,
readOnly: true,
decoration: InputDecoration(
border: const OutlineInputBorder(),
labelText: 'birthday'.tr,
2024-05-22 15:18:01 +00:00
),
onTap: () => _selectBirthday(),
2024-06-03 15:36:46 +00:00
).paddingSymmetric(horizontal: padding),
const Gap(16),
2024-06-03 15:36:46 +00:00
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
TextButton(
onPressed: _isBusy ? null : () => _syncWidget(),
2024-06-03 15:36:46 +00:00
child: Text('reset'.tr),
),
ElevatedButton(
onPressed: _isBusy ? null : () => _editUserInfo(),
2024-06-03 15:36:46 +00:00
child: Text('apply'.tr),
),
],
).paddingSymmetric(horizontal: padding),
],
2024-05-22 15:18:01 +00:00
),
);
}
2024-07-09 14:39:44 +00:00
@override
void dispose() {
_usernameController.dispose();
_nicknameController.dispose();
_firstNameController.dispose();
_lastNameController.dispose();
_descriptionController.dispose();
_birthdayController.dispose();
super.dispose();
}
2024-05-22 15:18:01 +00:00
}
class _BannerCropAspectRatioPreset extends CropAspectRatioPresetData {
@override
(int, int)? get data => (16, 7);
@override
String get name => '16x7';
}