toggle to set suggested status for all spaces
This commit is contained in:
parent
13b5711421
commit
ec66e45b64
5 changed files with 84 additions and 132 deletions
|
|
@ -3681,24 +3681,8 @@
|
|||
"lockSpace": "Lock Space",
|
||||
"lockChat": "Lock Chat",
|
||||
"archiveSpace": "Archive Space",
|
||||
"suggestTo": "Suggest to {spaceName}",
|
||||
"@suggestTo": {
|
||||
"placeholders": {
|
||||
"spaceName": {}
|
||||
}
|
||||
},
|
||||
"suggestChatDesc": "Suggested chats will appear in the chat list for {spaceName}",
|
||||
"@suggestToDesc": {
|
||||
"placeholders": {
|
||||
"spaceName": {}
|
||||
}
|
||||
},
|
||||
"suggestExchangeDesc": "Suggested exchanges will appear in the chat list for {spaceName}",
|
||||
"@suggestToExchangeDesc": {
|
||||
"placeholders": {
|
||||
"spaceName": {}
|
||||
}
|
||||
},
|
||||
"suggestToChat": "Suggest this chat",
|
||||
"suggestToChatDesc": "Suggested chats will appear in chat lists",
|
||||
"acceptSelection": "Accept Correction",
|
||||
"acceptSelectionAnyway": "Use this anyway",
|
||||
"makingActivity": "Making activity",
|
||||
|
|
|
|||
|
|
@ -130,9 +130,7 @@ class NewGroupController extends State<NewGroup> {
|
|||
powerLevelContentOverride:
|
||||
await ClassChatPowerLevels.powerLevelOverrideForClassChat(
|
||||
context,
|
||||
addToSpaceKey.currentState!.parents
|
||||
.map((suggestionStatus) => suggestionStatus.room)
|
||||
.toList(),
|
||||
addToSpaceKey.currentState!.parents,
|
||||
),
|
||||
invite: [
|
||||
if (addConversationBotKey.currentState?.addBot ?? false)
|
||||
|
|
|
|||
|
|
@ -176,9 +176,7 @@ class NewSpaceController extends State<NewSpace> {
|
|||
powerLevelContentOverride: addToSpaceKey.currentState != null
|
||||
? await ClassChatPowerLevels.powerLevelOverrideForClassChat(
|
||||
context,
|
||||
addToSpaceKey.currentState!.parents
|
||||
.map((suggestionStatus) => suggestionStatus.room)
|
||||
.toList(),
|
||||
addToSpaceKey.currentState!.parents,
|
||||
)
|
||||
: null,
|
||||
// initialState: [
|
||||
|
|
|
|||
|
|
@ -977,7 +977,40 @@ extension PangeaRoom on Room {
|
|||
return joinedRooms > 0 ? true : false;
|
||||
}
|
||||
|
||||
Future<bool> suggestedInSpace(Room space) async {
|
||||
Future<bool> isSuggested() async {
|
||||
final List<Room> spaceParents = client.rooms
|
||||
.where(
|
||||
(room) =>
|
||||
room.isSpace &&
|
||||
room.spaceChildren.any(
|
||||
(sc) => sc.roomId == id,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
for (final parent in spaceParents) {
|
||||
final suggested = await isSuggestedInSpace(parent);
|
||||
if (!suggested) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> setSuggested(bool suggested) async {
|
||||
final List<Room> spaceParents = client.rooms
|
||||
.where(
|
||||
(room) =>
|
||||
room.isSpace &&
|
||||
room.spaceChildren.any(
|
||||
(sc) => sc.roomId == id,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
for (final parent in spaceParents) {
|
||||
await setSuggestedInSpace(suggested, parent);
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> isSuggestedInSpace(Room space) async {
|
||||
try {
|
||||
final Map<String, dynamic> resp =
|
||||
await client.getRoomStateWithKey(space.id, EventTypes.spaceChild, id);
|
||||
|
|
|
|||
|
|
@ -33,9 +33,10 @@ class AddToSpaceToggles extends StatefulWidget {
|
|||
|
||||
class AddToSpaceState extends State<AddToSpaceToggles> {
|
||||
late Room? room;
|
||||
late List<SuggestionStatus> parents;
|
||||
late List<Room> parents;
|
||||
late List<Room> possibleParents;
|
||||
late bool isOpen;
|
||||
late bool isSuggested;
|
||||
|
||||
AddToSpaceState({Key? key});
|
||||
|
||||
|
|
@ -46,6 +47,9 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
? Matrix.of(context).client.getRoomById(widget.roomId!)
|
||||
: null;
|
||||
|
||||
isSuggested = true;
|
||||
room?.isSuggested().then((value) => isSuggested = value);
|
||||
|
||||
possibleParents = Matrix.of(context)
|
||||
.client
|
||||
.rooms
|
||||
|
|
@ -63,8 +67,6 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
(r) =>
|
||||
r.spaceChildren.any((room) => room.roomId == widget.roomId),
|
||||
)
|
||||
.map((r) => SuggestionStatus(false, r))
|
||||
.cast<SuggestionStatus>()
|
||||
.toList()
|
||||
: [];
|
||||
|
||||
|
|
@ -72,7 +74,7 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
final activeSpace =
|
||||
Matrix.of(context).client.getRoomById(widget.activeSpaceId!);
|
||||
if (activeSpace != null && activeSpace.canIAddSpaceChild(null)) {
|
||||
parents.add(SuggestionStatus(true, activeSpace));
|
||||
parents.add(activeSpace);
|
||||
} else {
|
||||
ErrorHandler.logError(
|
||||
e: Exception('activeSpaceId ${widget.activeSpaceId} not found'),
|
||||
|
|
@ -84,10 +86,9 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
//if possibleParent in parents, put first
|
||||
//use sort but use any instead of contains because contains uses == and we want to compare by id
|
||||
possibleParents.sort((a, b) {
|
||||
if (parents.any((suggestionStatus) => suggestionStatus.room.id == a.id)) {
|
||||
if (parents.any((parent) => parent.id == a.id)) {
|
||||
return -1;
|
||||
} else if (parents
|
||||
.any((suggestionStatus) => suggestionStatus.room.id == b.id)) {
|
||||
} else if (parents.any((parent) => parent.id == b.id)) {
|
||||
return 1;
|
||||
} else {
|
||||
return a.name.compareTo(b.name);
|
||||
|
|
@ -95,35 +96,21 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
});
|
||||
|
||||
isOpen = widget.startOpen;
|
||||
initSuggestedParents();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
Future<void> initSuggestedParents() async {
|
||||
if (room != null) {
|
||||
for (var i = 0; i < parents.length; i++) {
|
||||
final parent = parents[i];
|
||||
final bool suggested =
|
||||
await room?.suggestedInSpace(parent.room) ?? false;
|
||||
parents[i].suggested = suggested;
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _addSingleSpace(String roomToAddId, Room newParent) async {
|
||||
GoogleAnalytics.addParent(roomToAddId, newParent.classCode);
|
||||
await newParent.setSpaceChild(
|
||||
roomToAddId,
|
||||
suggested: isSuggestedInSpace(newParent),
|
||||
suggested: isSuggested,
|
||||
);
|
||||
await setSuggested(true, newParent);
|
||||
}
|
||||
|
||||
Future<void> addSpaces(String roomToAddId) async {
|
||||
final List<Future<void>> addFutures = [];
|
||||
for (final SuggestionStatus newParent in parents) {
|
||||
addFutures.add(_addSingleSpace(roomToAddId, newParent.room));
|
||||
for (final Room parent in parents) {
|
||||
addFutures.add(_addSingleSpace(roomToAddId, parent));
|
||||
}
|
||||
await addFutures.wait;
|
||||
}
|
||||
|
|
@ -148,38 +135,15 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
|
||||
setState(
|
||||
() => add
|
||||
? parents.add(SuggestionStatus(true, possibleParent))
|
||||
? parents.add(possibleParent)
|
||||
: parents.removeWhere(
|
||||
(suggestionStatus) =>
|
||||
suggestionStatus.room.id == possibleParent.id,
|
||||
(parent) => parent.id == possibleParent.id,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> setSuggested(bool suggest, Room possibleParent) async {
|
||||
if (room != null) {
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () => room!.setSuggestedInSpace(suggest, possibleParent),
|
||||
);
|
||||
}
|
||||
|
||||
for (final SuggestionStatus suggestionStatus in parents) {
|
||||
if (suggestionStatus.room.id == possibleParent.id) {
|
||||
suggestionStatus.suggested = suggest;
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
bool isSuggestedInSpace(Room parent) =>
|
||||
parents.firstWhereOrNull((r) => r.room.id == parent.id)?.suggested ??
|
||||
false;
|
||||
|
||||
Widget getAddToSpaceToggleItem(int index) {
|
||||
final Room possibleParent = possibleParents[index];
|
||||
final String possibleParentName = possibleParent.getLocalizedDisplayname();
|
||||
final bool canAdd = possibleParent.canIAddSpaceChild(room);
|
||||
|
||||
return Opacity(
|
||||
|
|
@ -189,7 +153,7 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
SwitchListTile.adaptive(
|
||||
title: possibleParent.nameAndRoomTypeIcon(),
|
||||
activeColor: AppConfig.activeToggleColor,
|
||||
value: parents.any((r) => r.room.id == possibleParent.id),
|
||||
value: parents.any((r) => r.id == possibleParent.id),
|
||||
onChanged: (bool add) => canAdd
|
||||
? handleAdd(add, possibleParent)
|
||||
: ScaffoldMessenger.of(context).showSnackBar(
|
||||
|
|
@ -198,53 +162,6 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
),
|
||||
),
|
||||
),
|
||||
AnimatedSize(
|
||||
duration: const Duration(milliseconds: 300),
|
||||
curve: Curves.easeInOut,
|
||||
child: parents.any((r) => r.room.id == possibleParent.id)
|
||||
? SwitchListTile.adaptive(
|
||||
title: Row(
|
||||
children: [
|
||||
const SizedBox(width: 32),
|
||||
Expanded(
|
||||
child: Text(
|
||||
L10n.of(context)!.suggestTo(possibleParentName),
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).colorScheme.secondary,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
subtitle: Row(
|
||||
children: [
|
||||
const SizedBox(width: 32),
|
||||
Expanded(
|
||||
child: Text(
|
||||
widget.mode == AddToClassMode.chat
|
||||
? L10n.of(context)!
|
||||
.suggestChatDesc(possibleParentName)
|
||||
: L10n.of(context)!.suggestExchangeDesc(
|
||||
possibleParentName,
|
||||
),
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
activeColor: AppConfig.activeToggleColor,
|
||||
value: isSuggestedInSpace(possibleParent),
|
||||
onChanged: (bool suggest) => canAdd
|
||||
? setSuggested(suggest, possibleParent)
|
||||
: ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(L10n.of(context)!.noPermission),
|
||||
),
|
||||
),
|
||||
)
|
||||
: Container(),
|
||||
),
|
||||
Divider(
|
||||
height: 0.5,
|
||||
color: Theme.of(context).colorScheme.secondary.withAlpha(25),
|
||||
|
|
@ -254,6 +171,16 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
);
|
||||
}
|
||||
|
||||
Future<void> setSuggested(bool suggested) async {
|
||||
setState(() => isSuggested = suggested);
|
||||
if (room != null) {
|
||||
await showFutureLoadingDialog(
|
||||
context: context,
|
||||
future: () async => await room?.setSuggested(suggested),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final String title = widget.mode == AddToClassMode.exchange
|
||||
|
|
@ -292,9 +219,28 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
const Divider(height: 1),
|
||||
possibleParents.isNotEmpty
|
||||
? Column(
|
||||
children: possibleParents
|
||||
.mapIndexed((index, _) => getAddToSpaceToggleItem(index))
|
||||
.toList(),
|
||||
children: [
|
||||
SwitchListTile.adaptive(
|
||||
title: Text(L10n.of(context)!.suggestToChat),
|
||||
secondary: Icon(
|
||||
isSuggested
|
||||
? Icons.visibility_outlined
|
||||
: Icons.visibility_off_outlined,
|
||||
),
|
||||
subtitle: Text(L10n.of(context)!.suggestToChatDesc),
|
||||
activeColor: AppConfig.activeToggleColor,
|
||||
value: isSuggested,
|
||||
onChanged: (bool add) => setSuggested(add),
|
||||
),
|
||||
Divider(
|
||||
height: 0.5,
|
||||
color:
|
||||
Theme.of(context).colorScheme.secondary.withAlpha(25),
|
||||
),
|
||||
...possibleParents.mapIndexed(
|
||||
(index, _) => getAddToSpaceToggleItem(index),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Center(
|
||||
child: Padding(
|
||||
|
|
@ -312,10 +258,3 @@ class AddToSpaceState extends State<AddToSpaceToggles> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SuggestionStatus {
|
||||
bool suggested;
|
||||
final Room room;
|
||||
|
||||
SuggestionStatus(this.suggested, this.room);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue