Skip to content

Commit

Permalink
Small fixes to character loading (#505)
Browse files Browse the repository at this point in the history
* bump version number

* small issues with character cards

* hashing
  • Loading branch information
danemadsen authored Apr 22, 2024
1 parent 95bc234 commit 86c82cf
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
31 changes: 21 additions & 10 deletions lib/providers/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<String> _greetings = [];
String _system = "";

bool _useExamples = true;
List<Map<String, dynamic>> _examples = [];
Expand All @@ -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;
Expand Down Expand Up @@ -138,21 +137,17 @@ class Character extends ChangeNotifier {
Map<String, dynamic> toMap() {
Map<String, dynamic> 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;
}
Expand Down Expand Up @@ -275,12 +270,28 @@ class Character extends ChangeNotifier {
notifyListeners();
}

Key get key => _key;

Future<File> get profile async {
return _profile ??= await Utilities.fileFromAssetImage("defaultCharacter.png");
}

String get hash {
List<String> 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;
Expand Down
15 changes: 0 additions & 15 deletions lib/ui/mobile/pages/character/character_browser_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class CharacterBrowserPage extends StatefulWidget {
class _CharacterBrowserPageState extends State<CharacterBrowserPage> {
// Changed from Map to List of Character
final List<Character> characters = [];
Key current = UniqueKey();

@override
void initState() {
Expand All @@ -38,20 +37,6 @@ class _CharacterBrowserPageState extends State<CharacterBrowserPage> {
});
}

@override
void dispose() {
_saveCharacters();
super.dispose();
}

Future<void> _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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ class _CharacterCustomizationPageState extends State<CharacterCustomizationPage>
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 =
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ packages:
source: hosted
version: "0.3.4+1"
crypto:
dependency: transitive
dependency: "direct main"
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
Expand Down
5 changes: 3 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 86c82cf

Please sign in to comment.