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:get/get.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:solian/exts.dart';
|
|
|
|
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();
|
|
|
|
|
|
|
|
int? _avatar;
|
|
|
|
int? _banner;
|
|
|
|
DateTime? _birthday;
|
|
|
|
|
|
|
|
bool _isBusy = false;
|
|
|
|
|
|
|
|
void selectBirthday() async {
|
|
|
|
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 {
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
final prof = auth.userProfile.value!;
|
2024-05-22 15:18:01 +00:00
|
|
|
setState(() {
|
2024-07-24 17:18:47 +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']);
|
2024-05-22 15:18:01 +00:00
|
|
|
_birthdayController.text =
|
2024-05-22 16:11:54 +00:00
|
|
|
DateFormat('yyyy-MM-dd').format(_birthday!.toLocal());
|
2024-05-22 15:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_isBusy = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> updateImage(String position) async {
|
|
|
|
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;
|
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
final AttachmentProvider provider = Get.find();
|
|
|
|
|
2024-06-23 11:41:50 +00:00
|
|
|
Response? attachResp;
|
2024-05-22 15:18:01 +00:00
|
|
|
try {
|
|
|
|
final file = File(image.path);
|
|
|
|
final hash = await calculateFileSha256(file);
|
2024-07-19 15:56:59 +00:00
|
|
|
final meta = await calculateImageMetaFromFile(file);
|
|
|
|
|
2024-05-22 15:18:01 +00:00
|
|
|
attachResp = await provider.createAttachment(
|
2024-06-29 12:25:29 +00:00
|
|
|
await file.readAsBytes(),
|
|
|
|
file.path,
|
2024-05-22 15:18:01 +00:00
|
|
|
hash,
|
|
|
|
'p.$position',
|
2024-07-19 15:56:59 +00:00
|
|
|
{...meta},
|
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
|
|
|
}
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-22 15:18:01 +00:00
|
|
|
|
|
|
|
final resp = await client.put(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/users/me/$position',
|
2024-05-22 15:18:01 +00:00
|
|
|
{'attachment': attachResp.body['id']},
|
|
|
|
);
|
|
|
|
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 updatePersonalize() async {
|
|
|
|
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);
|
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-22 16:11:54 +00:00
|
|
|
|
|
|
|
_birthday?.toIso8601String();
|
|
|
|
final resp = await client.put(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/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();
|
|
|
|
|
|
|
|
Future.delayed(Duration.zero, () => syncWidget());
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 SizedBox(height: 24),
|
|
|
|
Stack(
|
|
|
|
children: [
|
|
|
|
AccountAvatar(content: _avatar, radius: 40),
|
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
left: 40,
|
|
|
|
child: FloatingActionButton.small(
|
|
|
|
heroTag: const Key('avatar-editor'),
|
|
|
|
onPressed: () => updateImage('avatar'),
|
|
|
|
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 SizedBox(height: 16),
|
|
|
|
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(
|
2024-07-16 11:46:53 +00:00
|
|
|
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: () => updateImage('banner'),
|
|
|
|
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 SizedBox(height: 24),
|
|
|
|
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 SizedBox(width: 16),
|
|
|
|
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 SizedBox(height: 16),
|
|
|
|
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 SizedBox(width: 16),
|
|
|
|
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 SizedBox(height: 16),
|
|
|
|
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 SizedBox(height: 16),
|
|
|
|
TextField(
|
|
|
|
controller: _birthdayController,
|
|
|
|
readOnly: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: 'birthday'.tr,
|
2024-05-22 15:18:01 +00:00
|
|
|
),
|
2024-06-03 15:36:46 +00:00
|
|
|
onTap: () => selectBirthday(),
|
|
|
|
).paddingSymmetric(horizontal: padding),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: _isBusy ? null : () => syncWidget(),
|
|
|
|
child: Text('reset'.tr),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: _isBusy ? null : () => updatePersonalize(),
|
|
|
|
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
|
|
|
}
|