mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 12:14:11 +00:00 
			
		
		
		
	Can generate a random account image
This commit is contained in:
		@@ -40,8 +40,9 @@ class APIHelper {
 | 
				
			|||||||
      final data = FormData.fromMap(request.args);
 | 
					      final data = FormData.fromMap(request.args);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Process files (if required)
 | 
					      // Process files (if required)
 | 
				
			||||||
      if (multipart)
 | 
					      if (multipart) {
 | 
				
			||||||
        for (var key in request.files.keys) {
 | 
					        // Process filesystem files
 | 
				
			||||||
 | 
					        for (final key in request.files.keys) {
 | 
				
			||||||
          var v = request.files[key];
 | 
					          var v = request.files[key];
 | 
				
			||||||
          data.files.add(MapEntry(
 | 
					          data.files.add(MapEntry(
 | 
				
			||||||
              key,
 | 
					              key,
 | 
				
			||||||
@@ -49,6 +50,16 @@ class APIHelper {
 | 
				
			|||||||
                  filename: v.path.split("/").last)));
 | 
					                  filename: v.path.split("/").last)));
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Process in-memory files
 | 
				
			||||||
 | 
					        for (final key in request.bytesFiles.keys) {
 | 
				
			||||||
 | 
					          var v = request.bytesFiles[key];
 | 
				
			||||||
 | 
					          data.files.add(MapEntry(
 | 
				
			||||||
 | 
					              key,
 | 
				
			||||||
 | 
					              MultipartFile.fromBytes(v.bytes,
 | 
				
			||||||
 | 
					                  filename: v.filename.split("/").last)));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Execute the request
 | 
					      // Execute the request
 | 
				
			||||||
      final response = await Dio().post(
 | 
					      final response = await Dio().post(
 | 
				
			||||||
        url.toString(),
 | 
					        url.toString(),
 | 
				
			||||||
@@ -64,9 +75,10 @@ class APIHelper {
 | 
				
			|||||||
        return APIResponse(response.statusCode, null);
 | 
					        return APIResponse(response.statusCode, null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      return APIResponse(response.statusCode, response.data);
 | 
					      return APIResponse(response.statusCode, response.data);
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e, stack) {
 | 
				
			||||||
      print(e.toString());
 | 
					      print(e.toString());
 | 
				
			||||||
      print("Could not execute a request!");
 | 
					      print("Could not execute a request!");
 | 
				
			||||||
 | 
					      print(stack);
 | 
				
			||||||
      return APIResponse(-1, null);
 | 
					      return APIResponse(-1, null);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -36,6 +36,13 @@ class SettingsHelper {
 | 
				
			|||||||
              .execWithFiles())
 | 
					              .execWithFiles())
 | 
				
			||||||
          .isOK;
 | 
					          .isOK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Upload a new account image from memory
 | 
				
			||||||
 | 
					  static Future<bool> uploadAccountImageFromMemory(List<int> bytes) async =>
 | 
				
			||||||
 | 
					      (await APIRequest(uri: "settings/upload_account_image", needLogin: true)
 | 
				
			||||||
 | 
					              .addBytesFile("picture", BytesFile("accountImage.png", bytes))
 | 
				
			||||||
 | 
					              .execWithFiles())
 | 
				
			||||||
 | 
					          .isOK;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Change account image visibility level
 | 
					  /// Change account image visibility level
 | 
				
			||||||
  static Future<bool> setAccountImageVisibilityLevel(
 | 
					  static Future<bool> setAccountImageVisibilityLevel(
 | 
				
			||||||
          AccountImageVisibilityLevels level) async =>
 | 
					          AccountImageVisibilityLevels level) async =>
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,11 +10,19 @@ import 'package:meta/meta.dart';
 | 
				
			|||||||
///
 | 
					///
 | 
				
			||||||
/// @author Pierre HUBERT
 | 
					/// @author Pierre HUBERT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class BytesFile {
 | 
				
			||||||
 | 
					  final String filename;
 | 
				
			||||||
 | 
					  final List<int> bytes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  BytesFile(this.filename, this.bytes);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class APIRequest {
 | 
					class APIRequest {
 | 
				
			||||||
  final String uri;
 | 
					  final String uri;
 | 
				
			||||||
  final bool needLogin;
 | 
					  final bool needLogin;
 | 
				
			||||||
  Map<String, String> args;
 | 
					  Map<String, String> args;
 | 
				
			||||||
  Map<String, File> files = Map();
 | 
					  Map<String, File> files = Map();
 | 
				
			||||||
 | 
					  Map<String, BytesFile> bytesFiles = Map();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  APIRequest({@required this.uri, this.needLogin = false, this.args})
 | 
					  APIRequest({@required this.uri, this.needLogin = false, this.args})
 | 
				
			||||||
      : assert(uri != null),
 | 
					      : assert(uri != null),
 | 
				
			||||||
@@ -42,6 +50,11 @@ class APIRequest {
 | 
				
			|||||||
    return this;
 | 
					    return this;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  APIRequest addBytesFile(String name, BytesFile file) {
 | 
				
			||||||
 | 
					    this.bytesFiles[name] = file;
 | 
				
			||||||
 | 
					    return this;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void addArgs(Map<String, String> newArgs) => args.addAll(newArgs);
 | 
					  void addArgs(Map<String, String> newArgs) => args.addAll(newArgs);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Execute the request
 | 
					  /// Execute the request
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,6 +6,8 @@ import 'package:comunic/utils/files_utils.dart';
 | 
				
			|||||||
import 'package:comunic/utils/intl_utils.dart';
 | 
					import 'package:comunic/utils/intl_utils.dart';
 | 
				
			||||||
import 'package:comunic/utils/ui_utils.dart';
 | 
					import 'package:comunic/utils/ui_utils.dart';
 | 
				
			||||||
import 'package:flutter/material.dart';
 | 
					import 'package:flutter/material.dart';
 | 
				
			||||||
 | 
					import 'package:identicon/identicon.dart';
 | 
				
			||||||
 | 
					import 'package:random_string/random_string.dart';
 | 
				
			||||||
import 'package:settings_ui/settings_ui.dart';
 | 
					import 'package:settings_ui/settings_ui.dart';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Account image settings section
 | 
					/// Account image settings section
 | 
				
			||||||
@@ -69,11 +71,18 @@ class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
 | 
				
			|||||||
          width: 40,
 | 
					          width: 40,
 | 
				
			||||||
        ),
 | 
					        ),
 | 
				
			||||||
      ),
 | 
					      ),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Upload new account image
 | 
					      // Upload new account image
 | 
				
			||||||
      SettingsTile(
 | 
					      SettingsTile(
 | 
				
			||||||
        title: tr("Upload an account image"),
 | 
					        title: tr("Upload an account image"),
 | 
				
			||||||
        onTap: () => _uploadAccountImage(),
 | 
					        onTap: () => _uploadAccountImage(),
 | 
				
			||||||
      ),
 | 
					      ),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // Generate a random account image
 | 
				
			||||||
 | 
					      SettingsTile(
 | 
				
			||||||
 | 
					        title: tr("Generate a random account image"),
 | 
				
			||||||
 | 
					        onTap: () => _generateRandomAccountImage(),
 | 
				
			||||||
 | 
					      ),
 | 
				
			||||||
    ];
 | 
					    ];
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -95,6 +104,12 @@ class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
 | 
				
			|||||||
        onTap: () => _uploadAccountImage(),
 | 
					        onTap: () => _uploadAccountImage(),
 | 
				
			||||||
      ),
 | 
					      ),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      // Generate a random account image
 | 
				
			||||||
 | 
					      SettingsTile(
 | 
				
			||||||
 | 
					        title: tr("Generate a random account image"),
 | 
				
			||||||
 | 
					        onTap: () => _generateRandomAccountImage(),
 | 
				
			||||||
 | 
					      ),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Change account image visibility
 | 
					      // Change account image visibility
 | 
				
			||||||
      SettingsTile(
 | 
					      SettingsTile(
 | 
				
			||||||
          title: tr("Change account image visibility"),
 | 
					          title: tr("Change account image visibility"),
 | 
				
			||||||
@@ -131,6 +146,20 @@ class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
 | 
				
			|||||||
    _key.currentState.refresh();
 | 
					    _key.currentState.refresh();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Generate a random account image
 | 
				
			||||||
 | 
					  void _generateRandomAccountImage() async {
 | 
				
			||||||
 | 
					    // Generate emoticon
 | 
				
			||||||
 | 
					    final bytes = Identicon().generate(randomString(10));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!await SettingsHelper.uploadAccountImageFromMemory(bytes)) {
 | 
				
			||||||
 | 
					      showSimpleSnack(
 | 
				
			||||||
 | 
					          context, tr("Could not upload your generated account image!"));
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    _key.currentState.refresh();
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Change account image visibility
 | 
					  /// Change account image visibility
 | 
				
			||||||
  void _chooseAccountImageVisibility() async {
 | 
					  void _chooseAccountImageVisibility() async {
 | 
				
			||||||
    final newLevel = await showDialog<AccountImageVisibilityLevels>(
 | 
					    final newLevel = await showDialog<AccountImageVisibilityLevels>(
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										14
									
								
								pubspec.lock
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								pubspec.lock
									
									
									
									
									
								
							@@ -142,6 +142,13 @@ packages:
 | 
				
			|||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "3.1.3"
 | 
					    version: "3.1.3"
 | 
				
			||||||
 | 
					  identicon:
 | 
				
			||||||
 | 
					    dependency: "direct main"
 | 
				
			||||||
 | 
					    description:
 | 
				
			||||||
 | 
					      name: identicon
 | 
				
			||||||
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
 | 
					    source: hosted
 | 
				
			||||||
 | 
					    version: "0.1.1"
 | 
				
			||||||
  image:
 | 
					  image:
 | 
				
			||||||
    dependency: transitive
 | 
					    dependency: transitive
 | 
				
			||||||
    description:
 | 
					    description:
 | 
				
			||||||
@@ -233,6 +240,13 @@ packages:
 | 
				
			|||||||
      url: "https://pub.dartlang.org"
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
    source: hosted
 | 
					    source: hosted
 | 
				
			||||||
    version: "2.0.5"
 | 
					    version: "2.0.5"
 | 
				
			||||||
 | 
					  random_string:
 | 
				
			||||||
 | 
					    dependency: "direct main"
 | 
				
			||||||
 | 
					    description:
 | 
				
			||||||
 | 
					      name: random_string
 | 
				
			||||||
 | 
					      url: "https://pub.dartlang.org"
 | 
				
			||||||
 | 
					    source: hosted
 | 
				
			||||||
 | 
					    version: "2.0.1"
 | 
				
			||||||
  settings_ui:
 | 
					  settings_ui:
 | 
				
			||||||
    dependency: "direct main"
 | 
					    dependency: "direct main"
 | 
				
			||||||
    description:
 | 
					    description:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -57,6 +57,12 @@ dependencies:
 | 
				
			|||||||
  # Build settings UI
 | 
					  # Build settings UI
 | 
				
			||||||
  settings_ui: ^0.2.0
 | 
					  settings_ui: ^0.2.0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Generate identicons
 | 
				
			||||||
 | 
					  identicon: ^0.1.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Generate random strings
 | 
				
			||||||
 | 
					  random_string: ^2.0.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
dev_dependencies:
 | 
					dev_dependencies:
 | 
				
			||||||
  flutter_test:
 | 
					  flutter_test:
 | 
				
			||||||
    sdk: flutter
 | 
					    sdk: flutter
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user