diff --git a/lib/providers/character.dart b/lib/providers/character.dart index c150cd64..ef133c6f 100644 --- a/lib/providers/character.dart +++ b/lib/providers/character.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'dart:async'; import 'dart:convert'; +import 'package:crypto/crypto.dart'; import 'package:file_picker/file_picker.dart'; import 'package:flutter/services.dart'; import 'package:flutter/material.dart'; @@ -11,16 +12,15 @@ import 'package:maid/static/utilities.dart'; import 'package:path_provider/path_provider.dart'; class Character extends ChangeNotifier { - Key _key = UniqueKey(); File? _profile; String _name = "Maid"; String _description = ""; String _personality = ""; String _scenario = ""; + String _system = ""; bool _useGreeting = false; List _greetings = []; - String _system = ""; bool _useExamples = true; List> _examples = []; @@ -46,7 +46,6 @@ class Character extends ChangeNotifier { } void from(Character character) async { - _key = character.key; _profile = await character.profile; _name = character.name; _description = character.description; @@ -138,21 +137,17 @@ class Character extends ChangeNotifier { Map toMap() { Map jsonCharacter = {}; + jsonCharacter["spec"] = "mcf_v1"; jsonCharacter["profile"] = _profile!.path; - jsonCharacter["name"] = _name; jsonCharacter["description"] = _description; jsonCharacter["personality"] = _personality; jsonCharacter["scenario"] = _scenario; jsonCharacter["use_greeting"] = _useGreeting; jsonCharacter["greetings"] = _greetings; - jsonCharacter["first_mes"] = _greetings.firstOrNull ?? ""; - jsonCharacter["alternate_greetings"] = _greetings.isNotEmpty ? _greetings.sublist(1) : []; jsonCharacter["system_prompt"] = _system; - jsonCharacter["use_examples"] = _useExamples; jsonCharacter["examples"] = _examples; - jsonCharacter["mes_example"] = examplesToString(); return jsonCharacter; } @@ -275,12 +270,28 @@ class Character extends ChangeNotifier { notifyListeners(); } - Key get key => _key; - Future get profile async { return _profile ??= await Utilities.fileFromAssetImage("defaultCharacter.png"); } + String get hash { + List hashList = [ + _name, + _description, + _personality, + _scenario, + _system, + _useGreeting.toString(), + _greetings.join(), + _useExamples.toString(), + _examples.join(), + ]; + + final bytes = utf8.encode(hashList.join()); + + return sha256.convert(bytes).toString(); + } + String get name => _name; String get description => _description; diff --git a/lib/ui/mobile/pages/character/character_browser_page.dart b/lib/ui/mobile/pages/character/character_browser_page.dart index 3b0c26c3..12a24451 100644 --- a/lib/ui/mobile/pages/character/character_browser_page.dart +++ b/lib/ui/mobile/pages/character/character_browser_page.dart @@ -17,7 +17,6 @@ class CharacterBrowserPage extends StatefulWidget { class _CharacterBrowserPageState extends State { // Changed from Map to List of Character final List characters = []; - Key current = UniqueKey(); @override void initState() { @@ -38,20 +37,6 @@ class _CharacterBrowserPageState extends State { }); } - @override - void dispose() { - _saveCharacters(); - super.dispose(); - } - - Future _saveCharacters() async { - final prefs = await SharedPreferences.getInstance(); - characters.removeWhere((character) => character.key == current); - final String charactersJson = - json.encode(characters.map((character) => character.toMap()).toList()); - await prefs.setString("characters", charactersJson); - } - @override Widget build(BuildContext context) { return Scaffold( diff --git a/lib/ui/mobile/pages/character/character_customization_page.dart b/lib/ui/mobile/pages/character/character_customization_page.dart index 948800e9..64020c3b 100644 --- a/lib/ui/mobile/pages/character/character_customization_page.dart +++ b/lib/ui/mobile/pages/character/character_customization_page.dart @@ -68,7 +68,11 @@ class _CharacterCustomizationPageState extends State return Character.fromMap(characterMap); }).toList(); - characters.removeWhere((listCharacter) => character.key == listCharacter.key); + characters.removeWhere((listCharacter) { + print("Character Hash: ${character.hash}"); + print("List Character Hash: ${listCharacter.hash}"); + return character.hash == listCharacter.hash; + }); characters.insert(0, character); final String newCharactersJson = diff --git a/pubspec.lock b/pubspec.lock index a03d5b41..400b4cb1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -98,7 +98,7 @@ packages: source: hosted version: "0.3.4+1" crypto: - dependency: transitive + dependency: "direct main" description: name: crypto sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab diff --git a/pubspec.yaml b/pubspec.yaml index d36c3ad7..7999d130 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.2.5+2 +version: 1.2.6+0 environment: sdk: '>=3.0.0 <4.0.0' @@ -30,6 +30,7 @@ environment: dependencies: flutter: sdk: flutter + crypto: ^3.0.3 image: ^4.1.7 path_provider: ^2.0.2 permission_handler: ^11.0.1 @@ -113,7 +114,7 @@ msix_config: display_name: Maid publisher_display_name: Dane Madsen identity_name: com.danemadsen.maid - msix_version: 1.2.5+2 + msix_version: 1.2.6+0 logo_path: assets/maid.png flutter_launcher_icons: android: "launcher_icon"