Merge pull request #267 from pangeachat/suggested-toggle
toggle to set suggested status for all spaces
This commit is contained in:
commit
6a3d04707e
8 changed files with 104 additions and 144 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: [
|
||||
|
|
|
|||
|
|
@ -34,8 +34,9 @@ class ClassController extends BaseController {
|
|||
Future<void> fixClassPowerLevels() async {
|
||||
try {
|
||||
final List<Future<void>> classFixes = [];
|
||||
for (final room in (await _pangeaController
|
||||
.matrixState.client.classesAndExchangesImTeaching)) {
|
||||
final teacherSpaces = await _pangeaController
|
||||
.matrixState.client.classesAndExchangesImTeaching;
|
||||
for (final room in teacherSpaces) {
|
||||
classFixes.add(room.setClassPowerLevels());
|
||||
}
|
||||
await Future.wait(classFixes);
|
||||
|
|
|
|||
|
|
@ -62,13 +62,19 @@ extension ClassAndExchangeSettingsRoomExtension on Room {
|
|||
}
|
||||
final Event? currentPower = getState(EventTypes.RoomPowerLevels);
|
||||
final Map<String, dynamic>? currentPowerContent =
|
||||
currentPower?.content["events"] as Map<String, dynamic>?;
|
||||
final spaceChildPower = currentPowerContent?[EventTypes.spaceChild];
|
||||
currentPower?.content as Map<String, dynamic>?;
|
||||
if (currentPowerContent == null) {
|
||||
return;
|
||||
}
|
||||
if (!(currentPowerContent.containsKey("events"))) {
|
||||
currentPowerContent["events"] = {};
|
||||
}
|
||||
final spaceChildPower =
|
||||
currentPowerContent["events"][EventTypes.spaceChild];
|
||||
final studentAnalyticsPower =
|
||||
currentPowerContent?[PangeaEventTypes.studentAnalyticsSummary];
|
||||
currentPowerContent[PangeaEventTypes.studentAnalyticsSummary];
|
||||
|
||||
if ((spaceChildPower == null || studentAnalyticsPower == null) &&
|
||||
currentPowerContent != null) {
|
||||
if ((spaceChildPower == null || studentAnalyticsPower == null)) {
|
||||
currentPowerContent["events"][EventTypes.spaceChild] = 0;
|
||||
currentPowerContent["events"]
|
||||
[PangeaEventTypes.studentAnalyticsSummary] = 0;
|
||||
|
|
|
|||
|
|
@ -253,11 +253,10 @@ extension PangeaRoom on Room {
|
|||
|
||||
BotOptionsModel? get botOptions => _botOptions;
|
||||
|
||||
Future<bool> suggestedInSpace(Room space) async =>
|
||||
await _suggestedInSpace(space);
|
||||
Future<void> setSuggested(bool suggested) async =>
|
||||
await _setSuggested(suggested);
|
||||
|
||||
Future<void> setSuggestedInSpace(bool suggest, Room space) async =>
|
||||
await _setSuggestedInSpace(suggest, space);
|
||||
Future<bool> isSuggested() async => await _isSuggested();
|
||||
|
||||
// user_permissions
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,40 @@ extension RoomSettingsRoomExtension on Room {
|
|||
);
|
||||
}
|
||||
|
||||
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,39 +135,18 @@ 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);
|
||||
final bool canAdd = !(!possibleParent.isRoomAdmin &&
|
||||
widget.mode == AddToClassMode.exchange) &&
|
||||
possibleParent.canIAddSpaceChild(room);
|
||||
|
||||
return Opacity(
|
||||
opacity: canAdd ? 1 : 0.5,
|
||||
|
|
@ -189,7 +155,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 +164,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 +173,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 +221,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 +260,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