1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-22 12:59:21 +00:00

Replace identicon with jdenticon

This commit is contained in:
Pierre HUBERT 2021-12-28 16:34:14 +01:00
parent 7ae50e21a4
commit e180f0bc13
5 changed files with 104 additions and 38 deletions

View File

@ -8,13 +8,12 @@ import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart'; import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart';
import 'package:comunic/utils/account_utils.dart'; import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/files_utils.dart'; import 'package:comunic/utils/files_utils.dart';
import 'package:comunic/utils/identicon_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:flutter_settings_ui/flutter_settings_ui.dart'; import 'package:flutter_settings_ui/flutter_settings_ui.dart';
import 'package:identicon/identicon.dart';
import 'package:image_cropper/image_cropper.dart'; import 'package:image_cropper/image_cropper.dart';
import 'package:random_string/random_string.dart';
import '../../../utils/log_utils.dart'; import '../../../utils/log_utils.dart';
import '../../../utils/ui_utils.dart'; import '../../../utils/ui_utils.dart';
@ -167,7 +166,7 @@ class _AccountImageSettingsScreenState
/// Generate a random account image /// Generate a random account image
void _generateRandomAccountImage() async { void _generateRandomAccountImage() async {
// Generate emoticon // Generate emoticon
final bytes = Identicon().generate(randomString(10)); final bytes = await genIdenticon(context);
if (!await SettingsHelper.uploadAccountImageFromMemory(bytes)) { if (!await SettingsHelper.uploadAccountImageFromMemory(bytes)) {
showSimpleSnack( showSimpleSnack(

View File

@ -19,14 +19,13 @@ import 'package:comunic/ui/widgets/settings/header_spacer_section.dart';
import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart'; import 'package:comunic/ui/widgets/settings/multi_choices_settings_tile.dart';
import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart'; import 'package:comunic/ui/widgets/settings/text_settings_edit_tile.dart';
import 'package:comunic/utils/files_utils.dart'; import 'package:comunic/utils/files_utils.dart';
import 'package:comunic/utils/identicon_utils.dart';
import 'package:comunic/utils/input_utils.dart'; import 'package:comunic/utils/input_utils.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart'; import 'package:comunic/utils/log_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:flutter_settings_ui/flutter_settings_ui.dart'; import 'package:flutter_settings_ui/flutter_settings_ui.dart';
import 'package:identicon/identicon.dart';
import 'package:random_string/random_string.dart';
/// Groups settings screen /// Groups settings screen
/// ///
@ -432,8 +431,7 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
/// Generate a new random logo for the group /// Generate a new random logo for the group
void _generateRandomLogo() async { void _generateRandomLogo() async {
try { try {
final newLogo = final newLogo = await genIdenticon(context);
Identicon(rows: 10, cols: 10).generate(randomString(20), size: 100);
await _doUploadLogo(newLogo); await _doUploadLogo(newLogo);
} catch (e, stack) { } catch (e, stack) {
print("Could not generate new logo! $e\n$stack"); print("Could not generate new logo! $e\n$stack");

View File

@ -0,0 +1,52 @@
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:jdenticon_dart/jdenticon_dart.dart';
import 'package:random_string/random_string.dart';
/// Identicon utilities
///
/// @author Pierre Hubert
// Based on https://stackoverflow.com/a/63215502/3781411
Future<Uint8List> svgToPng(BuildContext context, String svgString,
{int svgWidth, int svgHeight}) async {
DrawableRoot svgDrawableRoot = await svg.fromSvgString(svgString, null);
// to have a nice rendering it is important to have the exact original height and width,
// the easier way to retrieve it is directly from the svg string
// but be careful, this is an ugly fix for a flutter_svg problem that works
// with my images
String temp = svgString.substring(svgString.indexOf('height="') + 8);
int originalHeight =
svgHeight ?? int.parse(temp.substring(0, temp.indexOf('p')));
temp = svgString.substring(svgString.indexOf('width="') + 7);
int originalWidth =
svgWidth ?? int.parse(temp.substring(0, temp.indexOf('p')));
// toPicture() and toImage() don't seem to be pixel ratio aware, so we calculate the actual sizes here
double devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
double width = originalHeight *
devicePixelRatio; // where 32 is your SVG's original width
double height = originalWidth * devicePixelRatio; // same thing
// Convert to ui.Picture
final picture = svgDrawableRoot.toPicture(size: Size(width, height));
// Convert to ui.Image. toImage() takes width and height as parameters
// you need to find the best size to suit your needs and take into account the screen DPI
final image = await picture.toImage(width.toInt(), height.toInt());
ByteData bytes = await image.toByteData(format: ImageByteFormat.png);
return bytes.buffer.asUint8List();
}
Future<Uint8List> genIdenticon(BuildContext context, {int size: 100}) async {
final identicon = Jdenticon.toSvg(randomString(25), size: size);
return svgToPng(context, identicon, svgHeight: size, svgWidth: size);
}

View File

@ -7,14 +7,14 @@ packages:
name: archive name: archive
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.13" version: "3.1.6"
args: args:
dependency: transitive dependency: transitive
description: description:
name: args name: args
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.6.0" version: "2.3.0"
async: async:
dependency: transitive dependency: transitive
description: description:
@ -113,13 +113,6 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.1" version: "2.0.1"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
cross_file: cross_file:
dependency: transitive dependency: transitive
description: description:
@ -133,7 +126,7 @@ packages:
name: crypto name: crypto
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.5" version: "3.0.1"
csslib: csslib:
dependency: transitive dependency: transitive
description: description:
@ -285,7 +278,7 @@ packages:
name: flutter_launcher_icons name: flutter_launcher_icons
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.8.1" version: "0.9.2"
flutter_plugin_android_lifecycle: flutter_plugin_android_lifecycle:
dependency: transitive dependency: transitive
description: description:
@ -300,6 +293,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.1" version: "2.0.1"
flutter_svg:
dependency: "direct main"
description:
name: flutter_svg
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
flutter_test: flutter_test:
dependency: "direct dev" dependency: "direct dev"
description: flutter description: flutter
@ -338,20 +338,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "4.0.0" version: "4.0.0"
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:
name: image name: image
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.19" version: "3.1.0"
image_cropper: image_cropper:
dependency: "direct main" dependency: "direct main"
description: description:
@ -387,6 +380,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.17.0" version: "0.17.0"
jdenticon_dart:
dependency: "direct main"
description:
name: jdenticon_dart
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
js: js:
dependency: transitive dependency: transitive
description: description:
@ -443,6 +443,20 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.8.0" version: "1.8.0"
path_drawing:
dependency: transitive
description:
name: path_drawing
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
path_parsing:
dependency: transitive
description:
name: path_parsing
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
path_provider: path_provider:
dependency: "direct main" dependency: "direct main"
description: description:
@ -519,7 +533,7 @@ packages:
name: petitparser name: petitparser
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.1.0" version: "4.4.0"
photo_view: photo_view:
dependency: "direct main" dependency: "direct main"
description: description:
@ -762,7 +776,7 @@ packages:
name: uuid name: uuid
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.2.2" version: "3.0.5"
vector_math: vector_math:
dependency: transitive dependency: transitive
description: description:
@ -867,14 +881,14 @@ packages:
name: xml name: xml
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "4.5.1" version: "5.3.1"
yaml: yaml:
dependency: transitive dependency: transitive
description: description:
name: yaml name: yaml
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.2.1" version: "3.1.0"
sdks: sdks:
dart: ">=2.15.0 <3.0.0" dart: ">=2.15.0 <3.0.0"
flutter: ">=2.5.0" flutter: ">=2.5.0"

View File

@ -59,7 +59,10 @@ dependencies:
flutter_settings_ui: ^2.0.1 flutter_settings_ui: ^2.0.1
# Generate identicons # Generate identicons
identicon: ^0.1.1 jdenticon_dart: ^2.0.0
# Render SVG images
flutter_svg: ^1.0.0
# Generate random strings # Generate random strings
random_string: ^2.0.1 random_string: ^2.0.1
@ -138,7 +141,7 @@ dev_dependencies:
sdk: flutter sdk: flutter
# Generate iOS application icons # Generate iOS application icons
flutter_launcher_icons: ^0.8.1 flutter_launcher_icons: ^0.9.2
flutter_icons: flutter_icons:
android: false android: false