1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2025-06-19 08:15:16 +00:00

Replace identicon with jdenticon

This commit is contained in:
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/utils/account_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/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_settings_ui/flutter_settings_ui.dart';
import 'package:identicon/identicon.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:random_string/random_string.dart';
import '../../../utils/log_utils.dart';
import '../../../utils/ui_utils.dart';
@ -167,7 +166,7 @@ class _AccountImageSettingsScreenState
/// Generate a random account image
void _generateRandomAccountImage() async {
// Generate emoticon
final bytes = Identicon().generate(randomString(10));
final bytes = await genIdenticon(context);
if (!await SettingsHelper.uploadAccountImageFromMemory(bytes)) {
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/text_settings_edit_tile.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/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.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
///
@ -313,12 +312,12 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
)
.toList()
.cast<SettingsTile>()
..add(
SettingsTile(
title: tr("Create a new conversation"),
onPressed: _createNewGroupConversation,
),
),
..add(
SettingsTile(
title: tr("Create a new conversation"),
onPressed: _createNewGroupConversation,
),
),
);
void _createNewGroupConversation(BuildContext context) async {
@ -432,8 +431,7 @@ class _GroupSettingsScreenState extends SafeState<GroupSettingsScreen> {
/// Generate a new random logo for the group
void _generateRandomLogo() async {
try {
final newLogo =
Identicon(rows: 10, cols: 10).generate(randomString(20), size: 100);
final newLogo = await genIdenticon(context);
await _doUploadLogo(newLogo);
} catch (e, 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);
}