Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Aug 9, 2024
1 parent 1a4984e commit e6007fc
Show file tree
Hide file tree
Showing 25 changed files with 355 additions and 310 deletions.
178 changes: 0 additions & 178 deletions lib/classes/providers/app_data.dart

This file was deleted.

5 changes: 5 additions & 0 deletions lib/classes/providers/artificial_intelligence.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class ArtificialIntelligence extends ChangeNotifier {
await Future.wait(futures);
}

void reset() {
llm.reset();
notify();
}

void notify() {
save().then((_) => notifyListeners());
}
Expand Down
4 changes: 2 additions & 2 deletions lib/classes/providers/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:crypto/crypto.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/characters.dart';
import 'package:maid/classes/static/logger.dart';
import 'package:image/image.dart';
import 'package:maid/classes/static/utilities.dart';
Expand All @@ -32,7 +32,7 @@ class Character extends ChangeNotifier {

Map<String, dynamic> _cachedJson = {};

static Character of(BuildContext context) => AppData.of(context).currentCharacter;
static Character of(BuildContext context) => CharacterCollection.of(context).current;

Character(VoidCallback? listener) : _key = UniqueKey() {
if (listener != null) {
Expand Down
105 changes: 105 additions & 0 deletions lib/classes/providers/characters.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:maid/classes/providers/character.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CharacterCollection extends ChangeNotifier {
static CharacterCollection of(BuildContext context) => Provider.of<CharacterCollection>(context, listen: false);

CharacterCollection(this._characters, this._current) {
_current.addListener(notify);
}

final List<Character> _characters;
Character _current;

List<Character> get list {
_characters.removeWhere((element) => element.key == _current.key);

return List.from([_current, ..._characters]);
}

Character get current => _current;

set current(Character character) {
_characters.insert(0, _current);

_current = character;

_current.addListener(notify);

save().then((value) => notifyListeners());
}

static Future<CharacterCollection> get last async {
final prefs = await SharedPreferences.getInstance();

String? charactersString = prefs.getString("characters");

List<Character> characters = [];

if (charactersString != null) {
characters = (json.decode(charactersString) as List).map((e) => Character.fromMap(null, e)).toList();
}

final character = await Character.last;

return CharacterCollection(
characters,
character
);
}

Future<void> save() async {
final prefs = await SharedPreferences.getInstance();

_characters.removeWhere((element) => element.key == _current.key);
final characterMaps = _characters.map((e) => e.toMap()).toList();

final futures = [
prefs.setString("characters", json.encode(characterMaps)),
_current.save(),
];

await Future.wait(futures);
}

void newCharacter() {
current = Character(notify);
}

void addCharacter(Character character) {
final index = _characters.indexWhere((element) => element.key == character.key);

if (index.isNegative) {
_characters.insert(0, character);
notifyListeners();
}
}

void removeCharacter(Character character) {
final index = _characters.indexWhere((element) => element.key == character.key);

if (!index.isNegative) {
_characters.removeAt(index);
notifyListeners();
}
}

void clearCharacters() {
_characters.clear();
notifyListeners();
}

void reset() {
clearCharacters();
_current = Character(notify);
notifyListeners();
}

void notify() {
save().then((value) => notifyListeners());
}
}
4 changes: 2 additions & 2 deletions lib/classes/providers/session.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:flutter/material.dart';
import 'package:maid/classes/chat_node_tree.dart';
import 'package:maid/classes/providers/app_preferences.dart';
import 'package:maid/classes/providers/large_language_model.dart';
import 'package:maid/classes/providers/sessions.dart';
import 'package:maid/enumerators/chat_role.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/character.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/classes/static/logger.dart';
Expand Down Expand Up @@ -39,7 +39,7 @@ class Session extends ChangeNotifier {
notifyListeners();
}

static Session of(BuildContext context) => AppData.of(context).currentSession;
static Session of(BuildContext context) => Sessions.of(context).current;

Session(VoidCallback? listener, int index) {
if (listener != null) {
Expand Down
Loading

0 comments on commit e6007fc

Please sign in to comment.