1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-12-26 04:48:51 +00:00

Can generate a random account image

This commit is contained in:
Pierre HUBERT 2020-04-17 08:44:57 +02:00
parent 7bb805bffd
commit 7e01947da9
6 changed files with 84 additions and 3 deletions

View File

@ -40,8 +40,9 @@ class APIHelper {
final data = FormData.fromMap(request.args);
// Process files (if required)
if (multipart)
for (var key in request.files.keys) {
if (multipart) {
// Process filesystem files
for (final key in request.files.keys) {
var v = request.files[key];
data.files.add(MapEntry(
key,
@ -49,6 +50,16 @@ class APIHelper {
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
final response = await Dio().post(
url.toString(),
@ -64,9 +75,10 @@ class APIHelper {
return APIResponse(response.statusCode, null);
return APIResponse(response.statusCode, response.data);
} catch (e) {
} catch (e, stack) {
print(e.toString());
print("Could not execute a request!");
print(stack);
return APIResponse(-1, null);
}
}

View File

@ -36,6 +36,13 @@ class SettingsHelper {
.execWithFiles())
.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
static Future<bool> setAccountImageVisibilityLevel(
AccountImageVisibilityLevels level) async =>

View File

@ -10,11 +10,19 @@ import 'package:meta/meta.dart';
///
/// @author Pierre HUBERT
class BytesFile {
final String filename;
final List<int> bytes;
BytesFile(this.filename, this.bytes);
}
class APIRequest {
final String uri;
final bool needLogin;
Map<String, String> args;
Map<String, File> files = Map();
Map<String, BytesFile> bytesFiles = Map();
APIRequest({@required this.uri, this.needLogin = false, this.args})
: assert(uri != null),
@ -42,6 +50,11 @@ class APIRequest {
return this;
}
APIRequest addBytesFile(String name, BytesFile file) {
this.bytesFiles[name] = file;
return this;
}
void addArgs(Map<String, String> newArgs) => args.addAll(newArgs);
/// Execute the request

View File

@ -6,6 +6,8 @@ import 'package:comunic/utils/files_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.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';
/// Account image settings section
@ -69,11 +71,18 @@ class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
width: 40,
),
),
// Upload new account image
SettingsTile(
title: tr("Upload an account image"),
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(),
),
// Generate a random account image
SettingsTile(
title: tr("Generate a random account image"),
onTap: () => _generateRandomAccountImage(),
),
// Change account image visibility
SettingsTile(
title: tr("Change account image visibility"),
@ -131,6 +146,20 @@ class __AccountImageSettingsBodyState extends State<_AccountImageSettingsBody> {
_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
void _chooseAccountImageVisibility() async {
final newLevel = await showDialog<AccountImageVisibilityLevels>(

View File

@ -142,6 +142,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
identicon:
dependency: "direct main"
description:
name: identicon
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1"
image:
dependency: transitive
description:
@ -233,6 +240,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
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:
dependency: "direct main"
description:

View File

@ -57,6 +57,12 @@ dependencies:
# Build settings UI
settings_ui: ^0.2.0
# Generate identicons
identicon: ^0.1.1
# Generate random strings
random_string: ^2.0.1
dev_dependencies:
flutter_test:
sdk: flutter