Merge branch 'soru/emote-settings' into 'main'

fix: Make emote settings work again

Closes #446

See merge request famedly/fluffychat!429
This commit is contained in:
Krille Fear 2021-06-24 14:43:46 +00:00
commit 4605f6de26
5 changed files with 49 additions and 40 deletions

View File

@ -215,10 +215,20 @@ class AppRoutes {
buildTransition: _dynamicTransition, buildTransition: _dynamicTransition,
), ),
VWidget( VWidget(
path: 'emotes', path: 'multiple_emotes',
widget: MultipleEmotesSettings(), widget: MultipleEmotesSettings(),
buildTransition: _dynamicTransition, buildTransition: _dynamicTransition,
), ),
VWidget(
path: 'emotes',
widget: EmotesSettings(),
buildTransition: _dynamicTransition,
),
VWidget(
path: 'emotes/:state_key',
widget: EmotesSettings(),
buildTransition: _dynamicTransition,
),
]; ];
List<VRouteElement> get _settingsRoutes => [ List<VRouteElement> get _settingsRoutes => [

View File

@ -240,9 +240,9 @@ class ChatDetailsController extends State<ChatDetails> {
if ((room.states['im.ponies.room_emotes'] ?? <String, Event>{}) if ((room.states['im.ponies.room_emotes'] ?? <String, Event>{})
.keys .keys
.any((String s) => s.isNotEmpty)) { .any((String s) => s.isNotEmpty)) {
VRouter.of(context).pushNamed('/rooms/${room.id}/emotes'); VRouter.of(context).push('/rooms/${room.id}/details/multiple_emotes');
} else { } else {
VRouter.of(context).pushNamed('/settings/emotes'); VRouter.of(context).push('/rooms/${room.id}/details/emotes');
} }
} }

View File

@ -6,6 +6,7 @@ import 'package:fluffychat/utils/platform_infos.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:vrouter/vrouter.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart'; import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
@ -13,10 +14,7 @@ import 'views/settings_emotes_view.dart';
import '../widgets/matrix.dart'; import '../widgets/matrix.dart';
class EmotesSettings extends StatefulWidget { class EmotesSettings extends StatefulWidget {
final Room room; EmotesSettings({Key key}) : super(key: key);
final String stateKey;
EmotesSettings({this.room, this.stateKey});
@override @override
EmotesSettingsController createState() => EmotesSettingsController(); EmotesSettingsController createState() => EmotesSettingsController();
@ -31,6 +29,11 @@ class EmoteEntry {
} }
class EmotesSettingsController extends State<EmotesSettings> { class EmotesSettingsController extends State<EmotesSettings> {
String get roomId => VRouter.of(context).pathParameters['roomid'];
Room get room =>
roomId != null ? Matrix.of(context).client.getRoomById(roomId) : null;
String get stateKey => VRouter.of(context).pathParameters['state_key'];
List<EmoteEntry> emotes; List<EmoteEntry> emotes;
bool showSave = false; bool showSave = false;
TextEditingController newEmoteController = TextEditingController(); TextEditingController newEmoteController = TextEditingController();
@ -43,10 +46,9 @@ class EmotesSettingsController extends State<EmotesSettings> {
final client = Matrix.of(context).client; final client = Matrix.of(context).client;
// be sure to preserve any data not in "short" // be sure to preserve any data not in "short"
Map<String, dynamic> content; Map<String, dynamic> content;
if (widget.room != null) { if (room != null) {
content = widget.room content =
.getState('im.ponies.room_emotes', widget.stateKey ?? '') room.getState('im.ponies.room_emotes', stateKey ?? '')?.content ??
?.content ??
<String, dynamic>{}; <String, dynamic>{};
} else { } else {
content = client.accountData['im.ponies.user_emotes']?.content ?? content = client.accountData['im.ponies.user_emotes']?.content ??
@ -74,11 +76,11 @@ class EmotesSettingsController extends State<EmotesSettings> {
} }
// remove the old "short" key // remove the old "short" key
content.remove('short'); content.remove('short');
if (widget.room != null) { if (room != null) {
await showFutureLoadingDialog( await showFutureLoadingDialog(
context: context, context: context,
future: () => client.setRoomStateWithKey(widget.room.id, future: () => client.setRoomStateWithKey(
'im.ponies.room_emotes', content, widget.stateKey ?? ''), room.id, 'im.ponies.room_emotes', content, stateKey ?? ''),
); );
} else { } else {
await showFutureLoadingDialog( await showFutureLoadingDialog(
@ -90,7 +92,7 @@ class EmotesSettingsController extends State<EmotesSettings> {
} }
Future<void> setIsGloballyActive(bool active) async { Future<void> setIsGloballyActive(bool active) async {
if (widget.room == null) { if (room == null) {
return; return;
} }
final client = Matrix.of(context).client; final client = Matrix.of(context).client;
@ -100,16 +102,14 @@ class EmotesSettingsController extends State<EmotesSettings> {
if (!(content['rooms'] is Map)) { if (!(content['rooms'] is Map)) {
content['rooms'] = <String, dynamic>{}; content['rooms'] = <String, dynamic>{};
} }
if (!(content['rooms'][widget.room.id] is Map)) { if (!(content['rooms'][room.id] is Map)) {
content['rooms'][widget.room.id] = <String, dynamic>{}; content['rooms'][room.id] = <String, dynamic>{};
} }
if (!(content['rooms'][widget.room.id][widget.stateKey ?? ''] is Map)) { if (!(content['rooms'][room.id][stateKey ?? ''] is Map)) {
content['rooms'][widget.room.id] content['rooms'][room.id][stateKey ?? ''] = <String, dynamic>{};
[widget.stateKey ?? ''] = <String, dynamic>{};
} }
} else if (content['rooms'] is Map && } else if (content['rooms'] is Map && content['rooms'][room.id] is Map) {
content['rooms'][widget.room.id] is Map) { content['rooms'][room.id].remove(stateKey ?? '');
content['rooms'][widget.room.id].remove(widget.stateKey ?? '');
} }
// and save // and save
await showFutureLoadingDialog( await showFutureLoadingDialog(
@ -159,17 +159,16 @@ class EmotesSettingsController extends State<EmotesSettings> {
} }
bool isGloballyActive(Client client) => bool isGloballyActive(Client client) =>
widget.room != null && room != null &&
client.accountData['im.ponies.emote_rooms']?.content is Map && client.accountData['im.ponies.emote_rooms']?.content is Map &&
client.accountData['im.ponies.emote_rooms'].content['rooms'] is Map && client.accountData['im.ponies.emote_rooms'].content['rooms'] is Map &&
client.accountData['im.ponies.emote_rooms'].content['rooms'] client.accountData['im.ponies.emote_rooms'].content['rooms'][room.id]
[widget.room.id] is Map && is Map &&
client.accountData['im.ponies.emote_rooms'].content['rooms'] client.accountData['im.ponies.emote_rooms'].content['rooms'][room.id]
[widget.room.id][widget.stateKey ?? ''] is Map; [stateKey ?? ''] is Map;
bool get readonly => widget.room == null bool get readonly =>
? false room == null ? false : !(room.canSendEvent('im.ponies.room_emotes'));
: !(widget.room.canSendEvent('im.ponies.room_emotes'));
void saveAction() async { void saveAction() async {
await _save(context); await _save(context);
@ -264,10 +263,9 @@ class EmotesSettingsController extends State<EmotesSettings> {
if (emotes == null) { if (emotes == null) {
emotes = <EmoteEntry>[]; emotes = <EmoteEntry>[];
Map<String, dynamic> emoteSource; Map<String, dynamic> emoteSource;
if (widget.room != null) { if (room != null) {
emoteSource = widget.room emoteSource =
.getState('im.ponies.room_emotes', widget.stateKey ?? '') room.getState('im.ponies.room_emotes', stateKey ?? '')?.content;
?.content;
} else { } else {
emoteSource = Matrix.of(context) emoteSource = Matrix.of(context)
.client .client

View File

@ -28,7 +28,7 @@ class EmotesSettingsView extends StatelessWidget {
: null, : null,
body: MaxWidthBody( body: MaxWidthBody(
child: StreamBuilder( child: StreamBuilder(
stream: controller.widget.room?.onUpdate?.stream, stream: controller.room?.onUpdate?.stream,
builder: (context, snapshot) { builder: (context, snapshot) {
return Column( return Column(
children: <Widget>[ children: <Widget>[
@ -81,7 +81,7 @@ class EmotesSettingsView extends StatelessWidget {
), ),
), ),
), ),
if (controller.widget.room != null) if (controller.room != null)
ListTile( ListTile(
title: Text(L10n.of(context).enableEmotesGlobally), title: Text(L10n.of(context).enableEmotesGlobally),
trailing: Switch( trailing: Switch(
@ -89,7 +89,7 @@ class EmotesSettingsView extends StatelessWidget {
onChanged: controller.setIsGloballyActive, onChanged: controller.setIsGloballyActive,
), ),
), ),
if (!controller.readonly || controller.widget.room != null) if (!controller.readonly || controller.room != null)
Divider( Divider(
height: 2, height: 2,
thickness: 2, thickness: 2,

View File

@ -44,7 +44,8 @@ class MultipleEmotesSettingsView extends StatelessWidget {
return ListTile( return ListTile(
title: Text(packName), title: Text(packName),
onTap: () async { onTap: () async {
VRouter.of(context).push('/settings/emotes'); VRouter.of(context)
.push('/rooms/${room.id}/details/emotes/${keys[i]}');
}, },
); );
}); });