mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-03 19:54:12 +00:00 
			
		
		
		
	Can change group name
This commit is contained in:
		@@ -3,6 +3,7 @@ import 'package:comunic/models/advanced_group_info.dart';
 | 
			
		||||
import 'package:comunic/models/api_request.dart';
 | 
			
		||||
import 'package:comunic/models/group.dart';
 | 
			
		||||
import 'package:comunic/utils/api_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/map_utils.dart';
 | 
			
		||||
 | 
			
		||||
/// Groups helper
 | 
			
		||||
///
 | 
			
		||||
@@ -192,6 +193,29 @@ class GroupsHelper {
 | 
			
		||||
    return groupInfo.info;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Update (set) new group settings
 | 
			
		||||
  ///
 | 
			
		||||
  /// Throws in case of error
 | 
			
		||||
  static Future<void> setSettings(AdvancedGroupInfo settings) async {
 | 
			
		||||
    await APIRequest(uri: "groups/set_settings", needLogin: true)
 | 
			
		||||
        .addInt("id", settings.id)
 | 
			
		||||
        .addString("name", settings.name)
 | 
			
		||||
        .addString("virtual_directory", settings.virtualDirectory)
 | 
			
		||||
        .addString("visibility",
 | 
			
		||||
            invertMap(_APIGroupsVisibilityLevelsMap)[settings.visibilityLevel])
 | 
			
		||||
        .addString(
 | 
			
		||||
            "registration_level",
 | 
			
		||||
            invertMap(
 | 
			
		||||
                _APIGroupsRegistrationLevelsMap)[settings.registrationLevel])
 | 
			
		||||
        .addString(
 | 
			
		||||
            "posts_level",
 | 
			
		||||
            invertMap(
 | 
			
		||||
                _APIGroupsPostsCreationLevelsMap)[settings.postCreationLevel])
 | 
			
		||||
        .addString("description", settings.description)
 | 
			
		||||
        .addString("url", settings.url)
 | 
			
		||||
        .execWithThrow();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Turn an API entry into a group object
 | 
			
		||||
  Group _getGroupFromAPI(Map<String, dynamic> map) {
 | 
			
		||||
    return Group(
 | 
			
		||||
@@ -224,7 +248,7 @@ class GroupsHelper {
 | 
			
		||||
        following: map["following"],
 | 
			
		||||
        timeCreate: map["time_create"],
 | 
			
		||||
        description: map["description"],
 | 
			
		||||
        url: map["url"],
 | 
			
		||||
        url: map["url"] == "null" ? "" : map["url"],
 | 
			
		||||
        likes: map["number_likes"],
 | 
			
		||||
        userLike: map["is_liking"],
 | 
			
		||||
      );
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,7 @@ enum GroupPostCreationLevel { MODERATORS, MEMBERS }
 | 
			
		||||
 | 
			
		||||
class Group {
 | 
			
		||||
  final int id;
 | 
			
		||||
  final String name;
 | 
			
		||||
  String name;
 | 
			
		||||
  final String iconURL;
 | 
			
		||||
  final int numberMembers;
 | 
			
		||||
  GroupMembershipLevel membershipLevel;
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,9 @@ import 'package:comunic/models/advanced_group_info.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/async_screen_widget.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/safe_state.dart';
 | 
			
		||||
import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart';
 | 
			
		||||
import 'package:comunic/utils/intl_utils.dart';
 | 
			
		||||
import 'package:comunic/utils/ui_utils.dart';
 | 
			
		||||
import 'package:flutter/material.dart';
 | 
			
		||||
import 'package:settings_ui/settings_ui.dart';
 | 
			
		||||
 | 
			
		||||
@@ -25,10 +27,23 @@ class GroupSettingsScreen extends StatefulWidget {
 | 
			
		||||
class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
 | 
			
		||||
  AdvancedGroupInfo _groupSettings;
 | 
			
		||||
 | 
			
		||||
  final _key = GlobalKey<AsyncScreenWidgetState>();
 | 
			
		||||
 | 
			
		||||
  Future<void> _refresh() async {
 | 
			
		||||
    _groupSettings = await GroupsHelper().getSettings(widget.groupID);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Future<void> _updateSettings() async {
 | 
			
		||||
    try {
 | 
			
		||||
      await GroupsHelper.setSettings(_groupSettings);
 | 
			
		||||
    } catch (e, stack) {
 | 
			
		||||
      print("Could not update group settings! $e\n$stack");
 | 
			
		||||
      showSimpleSnack(context, tr("Could not update group settings!"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    _key.currentState.refresh();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    return Scaffold(
 | 
			
		||||
@@ -42,9 +57,11 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
 | 
			
		||||
 | 
			
		||||
  Widget _buildBody() {
 | 
			
		||||
    return AsyncScreenWidget(
 | 
			
		||||
      key: _key,
 | 
			
		||||
      onReload: _refresh,
 | 
			
		||||
      onBuild: _buildContent,
 | 
			
		||||
      errorMessage: tr("Could not get group settings!"),
 | 
			
		||||
      showOldDataWhileUpdating: true,
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -58,10 +75,20 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
 | 
			
		||||
    return SettingsSection(
 | 
			
		||||
      title: tr("General information"),
 | 
			
		||||
      tiles: [
 | 
			
		||||
        // Group ID
 | 
			
		||||
        SettingsTile(
 | 
			
		||||
          title: tr("Group ID"),
 | 
			
		||||
          subtitle: _groupSettings.id.toString(),
 | 
			
		||||
        ),
 | 
			
		||||
 | 
			
		||||
        // Group name
 | 
			
		||||
        TextEditSettingsTile(
 | 
			
		||||
            title: tr("Group name"),
 | 
			
		||||
            currValue: _groupSettings.name,
 | 
			
		||||
            onChanged: (s) {
 | 
			
		||||
              _groupSettings.name = s;
 | 
			
		||||
              _updateSettings();
 | 
			
		||||
            }),
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								lib/utils/map_utils.dart
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								lib/utils/map_utils.dart
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
/// Map utilities
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
/// Invert the values of a map with their keys
 | 
			
		||||
Map<V, K> invertMap<K, V>(Map<K, V> m) => m.map((k, v) => MapEntry(v, k));
 | 
			
		||||
		Reference in New Issue
	
	Block a user