FurryChat/lib/views/settings.dart

192 lines
5.9 KiB
Dart
Raw Normal View History

2020-01-01 18:10:13 +00:00
import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/adaptive_page_layout.dart';
import 'package:fluffychat/components/content_banner.dart';
import 'package:fluffychat/components/matrix.dart';
2020-01-08 13:19:15 +00:00
import 'package:fluffychat/utils/app_route.dart';
2020-01-01 18:10:13 +00:00
import 'package:fluffychat/views/chat_list.dart';
2020-01-09 21:52:27 +00:00
import 'package:fluffychat/views/sign_up.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:toast/toast.dart';
2020-01-04 08:40:38 +00:00
import 'package:url_launcher/url_launcher.dart';
2020-01-01 18:10:13 +00:00
class SettingsView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(),
secondScaffold: Settings(),
);
}
}
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
Future<dynamic> profileFuture;
dynamic profile;
void logoutAction(BuildContext context) async {
MatrixState matrix = Matrix.of(context);
2020-01-08 13:19:15 +00:00
await matrix.tryRequestWithLoadingDialog(matrix.client.logout());
2020-01-01 18:10:13 +00:00
matrix.clean();
2020-01-08 13:19:15 +00:00
await Navigator.of(context).pushAndRemoveUntil(
2020-01-09 21:52:27 +00:00
AppRoute.defaultRoute(context, SignUp()), (r) => false);
2020-01-01 18:10:13 +00:00
}
void setDisplaynameAction(BuildContext context, String displayname) async {
final MatrixState matrix = Matrix.of(context);
final Map<String, dynamic> success =
await matrix.tryRequestWithLoadingDialog(
2020-01-02 14:10:21 +00:00
matrix.client.jsonRequest(
2020-01-01 18:10:13 +00:00
type: HTTPType.PUT,
action: "/client/r0/profile/${matrix.client.userID}/displayname",
data: {"displayname": displayname},
),
);
2020-01-02 21:31:39 +00:00
if (success != null && success.isEmpty) {
2020-01-01 18:10:13 +00:00
Toast.show(
"Displayname has been changed",
context,
duration: Toast.LENGTH_LONG,
);
setState(() {
profileFuture = null;
profile = null;
});
}
}
void setAvatarAction(BuildContext context) async {
final File tempFile = await ImagePicker.pickImage(
source: ImageSource.gallery,
imageQuality: 50,
maxWidth: 1600,
maxHeight: 1600);
if (tempFile == null) return;
final MatrixState matrix = Matrix.of(context);
2020-01-04 08:40:38 +00:00
final success = await matrix.tryRequestWithLoadingDialog(
2020-01-01 18:10:13 +00:00
matrix.client.setAvatar(
MatrixFile(
bytes: await tempFile.readAsBytes(),
path: tempFile.path,
),
),
);
2020-01-04 08:40:38 +00:00
print(success);
if (success != false) {
2020-01-01 18:10:13 +00:00
Toast.show(
"Avatar has been changed",
context,
duration: Toast.LENGTH_LONG,
);
setState(() {
profileFuture = null;
profile = null;
});
}
}
@override
Widget build(BuildContext context) {
final Client client = Matrix.of(context).client;
profileFuture ??= client.getProfileFromUserId(client.userID);
2020-01-08 13:19:15 +00:00
profileFuture.then((p) {
if (mounted) setState(() => profile = p);
});
2020-01-01 18:10:13 +00:00
return Scaffold(
appBar: AppBar(
title: Text("Settings"),
),
body: ListView(
children: <Widget>[
ContentBanner(
profile?.avatarUrl ?? MxContent(""),
defaultIcon: Icons.account_circle,
loading: profile == null,
),
2020-01-04 08:40:38 +00:00
ListTile(
title: Text(
"Profile",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
2020-01-01 18:10:13 +00:00
kIsWeb
? Container()
: ListTile(
title: Text("Upload avatar"),
2020-01-04 08:40:38 +00:00
leading: Icon(Icons.camera),
2020-01-01 18:10:13 +00:00
onTap: () => setAvatarAction(context),
),
ListTile(
2020-01-04 08:40:38 +00:00
leading: Icon(Icons.edit),
2020-01-01 18:10:13 +00:00
title: TextField(
readOnly: profile == null,
textInputAction: TextInputAction.done,
onSubmitted: (s) => setDisplaynameAction(context, s),
decoration: InputDecoration(
border: InputBorder.none,
labelText: "Edit displayname",
labelStyle: TextStyle(color: Colors.black),
hintText: (profile?.displayname ?? ""),
),
),
),
ListTile(
2020-01-04 08:40:38 +00:00
title: Text(
"About",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
leading: Icon(Icons.help),
title: Text("Help"),
onTap: () => launch(
"https://gitlab.com/ChristianPauly/fluffychat-flutter/issues")),
2020-01-08 19:21:05 +00:00
ListTile(
leading: Icon(Icons.list),
title: Text("Changelog"),
onTap: () => launch(
"https://gitlab.com/ChristianPauly/fluffychat-flutter/blob/master/CHANGELOG.md")),
2020-01-04 08:40:38 +00:00
ListTile(
leading: Icon(Icons.link),
title: Text("License"),
onTap: () => launch(
"https://gitlab.com/ChristianPauly/fluffychat-flutter/raw/master/LICENSE")),
ListTile(
leading: Icon(Icons.code),
title: Text("Source code"),
onTap: () => launch(
"https://gitlab.com/ChristianPauly/fluffychat-flutter")),
ListTile(
title: Text(
"Logout",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
leading: Icon(Icons.exit_to_app),
2020-01-01 18:10:13 +00:00
title: Text("Logout"),
onTap: () => logoutAction(context),
),
],
),
);
}
}