2024-05-02 10:56:40 +00:00
|
|
|
import 'dart:convert';
|
2024-05-03 08:16:42 +00:00
|
|
|
import 'dart:io';
|
2024-05-02 10:56:40 +00:00
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-05-03 08:16:42 +00:00
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2024-05-02 10:56:40 +00:00
|
|
|
import 'package:intl/intl.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/utils/service_url.dart';
|
2024-05-03 08:16:42 +00:00
|
|
|
import 'package:solian/widgets/account/account_avatar.dart';
|
2024-05-02 10:56:40 +00:00
|
|
|
import 'package:solian/widgets/exts.dart';
|
2024-05-03 05:39:52 +00:00
|
|
|
import 'package:solian/widgets/scaffold.dart';
|
2024-05-02 10:56:40 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
|
|
class PersonalizeScreen extends StatelessWidget {
|
|
|
|
const PersonalizeScreen({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-03 05:39:52 +00:00
|
|
|
return IndentScaffold(
|
2024-05-02 10:56:40 +00:00
|
|
|
title: AppLocalizations.of(context)!.personalize,
|
|
|
|
hideDrawer: true,
|
|
|
|
child: const PersonalizeScreenWidget(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class PersonalizeScreenWidget extends StatefulWidget {
|
|
|
|
const PersonalizeScreenWidget({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<PersonalizeScreenWidget> createState() => _PersonalizeScreenWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PersonalizeScreenWidgetState extends State<PersonalizeScreenWidget> {
|
2024-05-03 08:16:42 +00:00
|
|
|
final _imagePicker = ImagePicker();
|
|
|
|
|
2024-05-02 10:56:40 +00:00
|
|
|
final _usernameController = TextEditingController();
|
|
|
|
final _nicknameController = TextEditingController();
|
|
|
|
final _firstNameController = TextEditingController();
|
|
|
|
final _lastNameController = TextEditingController();
|
|
|
|
final _descriptionController = TextEditingController();
|
|
|
|
final _birthdayController = TextEditingController();
|
|
|
|
|
2024-05-03 08:16:42 +00:00
|
|
|
String? _avatar;
|
|
|
|
String? _banner;
|
2024-05-02 10:56:40 +00:00
|
|
|
DateTime? _birthday;
|
|
|
|
|
|
|
|
bool _isSubmitting = false;
|
|
|
|
|
|
|
|
void editBirthday() async {
|
|
|
|
final DateTime? picked = await showDatePicker(
|
|
|
|
context: context,
|
|
|
|
initialDate: _birthday,
|
|
|
|
firstDate: DateTime(DateTime.now().year - 200),
|
|
|
|
lastDate: DateTime(DateTime.now().year + 200),
|
|
|
|
);
|
|
|
|
if (picked != null && picked != _birthday) {
|
|
|
|
setState(() {
|
|
|
|
_birthday = picked;
|
|
|
|
_birthdayController.text = DateFormat('yyyy-MM-dd hh:mm').format(_birthday!);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void resetInputs() async {
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
final prof = await auth.getProfiles();
|
2024-05-03 15:38:51 +00:00
|
|
|
setState(() {
|
|
|
|
_usernameController.text = prof['name'];
|
|
|
|
_nicknameController.text = prof['nick'];
|
|
|
|
_descriptionController.text = prof['description'];
|
|
|
|
_firstNameController.text = prof['profile']['first_name'];
|
|
|
|
_lastNameController.text = prof['profile']['last_name'];
|
|
|
|
if (prof['avatar'] != null && prof['avatar'].isNotEmpty) {
|
|
|
|
_avatar = getRequestUri('passport', '/api/avatar/${prof['avatar']}').toString();
|
|
|
|
}
|
|
|
|
if (prof['banner'] != null && prof['banner'].isNotEmpty) {
|
|
|
|
_banner = getRequestUri('passport', '/api/avatar/${prof['banner']}').toString();
|
|
|
|
}
|
|
|
|
if (prof['profile']['birthday'] != null) {
|
|
|
|
_birthday = DateTime.parse(prof['profile']['birthday']);
|
|
|
|
_birthdayController.text = DateFormat('yyyy-MM-dd hh:mm').format(_birthday!);
|
|
|
|
}
|
|
|
|
});
|
2024-05-02 10:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void applyChanges() async {
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) {
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final res = await auth.client!.put(
|
|
|
|
getRequestUri('passport', '/api/users/me'),
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: jsonEncode({
|
|
|
|
'nick': _nicknameController.value.text,
|
|
|
|
'description': _descriptionController.value.text,
|
|
|
|
'first_name': _firstNameController.value.text,
|
|
|
|
'last_name': _lastNameController.value.text,
|
|
|
|
'birthday': _birthday?.toIso8601String(),
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
await auth.fetchProfiles();
|
2024-05-03 15:38:51 +00:00
|
|
|
resetInputs();
|
2024-05-02 10:56:40 +00:00
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.personalizeApplied),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
var message = utf8.decode(res.bodyBytes);
|
|
|
|
context.showErrorDialog(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
2024-05-03 15:38:51 +00:00
|
|
|
Future<void> applyImage(String position) async {
|
2024-05-03 08:16:42 +00:00
|
|
|
final auth = context.read<AuthProvider>();
|
|
|
|
if (!await auth.isAuthorized()) return;
|
|
|
|
|
|
|
|
final image = await _imagePicker.pickImage(source: ImageSource.gallery);
|
|
|
|
if (image == null) return;
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = true);
|
|
|
|
|
|
|
|
final file = File(image.path);
|
|
|
|
try {
|
|
|
|
final req = MultipartRequest('PUT', getRequestUri('passport', '/api/users/me/$position'));
|
|
|
|
req.files.add(await MultipartFile.fromPath(position, file.path));
|
|
|
|
|
|
|
|
var res = await auth.client!.send(req);
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
await auth.fetchProfiles();
|
2024-05-03 15:38:51 +00:00
|
|
|
resetInputs();
|
2024-05-03 08:16:42 +00:00
|
|
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text(AppLocalizations.of(context)!.personalizeApplied),
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
throw Exception(utf8.decode(await res.stream.toBytes()));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() => _isSubmitting = false);
|
|
|
|
}
|
|
|
|
|
2024-05-02 10:56:40 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
2024-05-03 08:16:42 +00:00
|
|
|
Future.delayed(Duration.zero, () {
|
2024-05-03 15:38:51 +00:00
|
|
|
resetInputs();
|
2024-05-03 08:16:42 +00:00
|
|
|
});
|
2024-05-02 10:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Container(
|
2024-05-03 08:16:42 +00:00
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 32),
|
|
|
|
child: ListView(
|
2024-05-02 10:56:40 +00:00
|
|
|
children: [
|
|
|
|
_isSubmitting ? const LinearProgressIndicator().animate().scaleX() : Container(),
|
2024-05-03 08:16:42 +00:00
|
|
|
const SizedBox(height: 24),
|
|
|
|
Stack(
|
|
|
|
children: [
|
|
|
|
AccountAvatar(source: _avatar ?? '', radius: 40, direct: true),
|
|
|
|
Positioned(
|
|
|
|
bottom: 0,
|
|
|
|
left: 40,
|
|
|
|
child: FloatingActionButton.small(
|
2024-05-03 15:38:51 +00:00
|
|
|
heroTag: const Key('avatar-editor'),
|
|
|
|
onPressed: () => applyImage('avatar'),
|
2024-05-03 08:16:42 +00:00
|
|
|
child: const Icon(
|
|
|
|
Icons.camera,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
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.surfaceVariant,
|
|
|
|
child: _banner != null
|
|
|
|
? Image.network(
|
|
|
|
_banner!,
|
|
|
|
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(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Positioned(
|
|
|
|
bottom: 16,
|
|
|
|
right: 16,
|
|
|
|
child: FloatingActionButton(
|
2024-05-03 15:38:51 +00:00
|
|
|
heroTag: const Key('banner-editor'),
|
|
|
|
onPressed: () => applyImage('banner'),
|
2024-05-03 08:16:42 +00:00
|
|
|
child: const Icon(
|
|
|
|
Icons.camera_alt,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 24),
|
2024-05-02 10:56:40 +00:00
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
2024-05-02 12:45:24 +00:00
|
|
|
readOnly: true,
|
2024-05-02 10:56:40 +00:00
|
|
|
controller: _usernameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: AppLocalizations.of(context)!.username,
|
|
|
|
prefixText: '@',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 16),
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
controller: _nicknameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: AppLocalizations.of(context)!.nickname,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
controller: _firstNameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: AppLocalizations.of(context)!.firstName,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(width: 16),
|
|
|
|
Flexible(
|
|
|
|
flex: 1,
|
|
|
|
child: TextField(
|
|
|
|
controller: _lastNameController,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: AppLocalizations.of(context)!.lastName,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
TextField(
|
|
|
|
controller: _descriptionController,
|
|
|
|
keyboardType: TextInputType.multiline,
|
|
|
|
maxLines: null,
|
|
|
|
minLines: 3,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: AppLocalizations.of(context)!.description,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
TextField(
|
|
|
|
controller: _birthdayController,
|
|
|
|
readOnly: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
border: const OutlineInputBorder(),
|
|
|
|
labelText: AppLocalizations.of(context)!.birthday,
|
|
|
|
),
|
|
|
|
onTap: editBirthday,
|
|
|
|
),
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: _isSubmitting ? null : () => resetInputs(),
|
|
|
|
child: Text(AppLocalizations.of(context)!.reset),
|
|
|
|
),
|
|
|
|
ElevatedButton(
|
|
|
|
onPressed: _isSubmitting ? null : () => applyChanges(),
|
|
|
|
child: Text(AppLocalizations.of(context)!.apply),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|