Skip to content

Commit

Permalink
Cleanup providers and add Thememode dropdown (#503)
Browse files Browse the repository at this point in the history
* work oncharacter browser page

* work on character tile

* character card

* alignment

* improvements to character browser page

* clean up providers

* cleanup user provider

* theme mode dropdown
  • Loading branch information
danemadsen authored Apr 21, 2024
1 parent 5014c76 commit 95bc234
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 198 deletions.
51 changes: 25 additions & 26 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:maid/providers/user.dart';
import 'package:maid/ui/mobile/pages/home_page.dart';
import 'package:maid/providers/session.dart';
import 'package:maid/providers/character.dart';
import 'package:maid/static/themes.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();

final prefs = await SharedPreferences.getInstance();

String? lastUserString = prefs.getString("last_user");
Map<String, dynamic> lastUser = json.decode(lastUserString ?? "{}");
User user = User.fromMap(lastUser);

String? lastCharacterString = prefs.getString("last_character");
Map<String, dynamic> lastCharacter = json.decode(lastCharacterString ?? "{}");
Character character = Character.fromMap(lastCharacter);

String? lastSessionString = prefs.getString("last_session");
Map<String, dynamic> lastSession = json.decode(lastSessionString ?? "{}");
Session session = Session.fromMap(lastSession);

runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MainProvider()),
ChangeNotifierProvider(create: (context) => User()),
ChangeNotifierProvider(create: (context) => Character()),
ChangeNotifierProvider(create: (context) => Session()),
ChangeNotifierProvider(create: (context) => user),
ChangeNotifierProvider(create: (context) => character),
ChangeNotifierProvider(create: (context) => session),
],
child: const MaidApp(),
),
Expand All @@ -24,26 +41,15 @@ void main() {

class MainProvider extends ChangeNotifier {
ThemeMode _themeMode = ThemeMode.dark;
bool _initialised = false;

ThemeMode get themeMode => _themeMode;

bool get isDarkMode => _themeMode == ThemeMode.dark;

bool get initialised => _initialised;

void toggleTheme() {
_themeMode =
_themeMode == ThemeMode.dark ? ThemeMode.light : ThemeMode.dark;
set themeMode(ThemeMode value) {
_themeMode = value;
notifyListeners();
}

void init() {
_initialised = true;
}

void reset() {
_initialised = false;
notifyListeners();
}
}
Expand All @@ -58,15 +64,8 @@ class MaidApp extends StatefulWidget {
class MaidAppState extends State<MaidApp> {
@override
Widget build(BuildContext context) {
return Consumer4<MainProvider, User, Character, Session>(
builder: (context, mainProvider, user, character, session, child) {
if (!mainProvider.initialised) {
mainProvider.init();
user.init();
character.init();
session.init();
}

return Consumer<MainProvider>(
builder: (context, mainProvider, child) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Maid',
Expand Down
30 changes: 1 addition & 29 deletions lib/providers/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:maid/static/logger.dart';
import 'package:image/image.dart';
import 'package:maid/static/utilities.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Character extends ChangeNotifier {
Key _key = UniqueKey();
Expand All @@ -29,39 +28,17 @@ class Character extends ChangeNotifier {
Map<String, dynamic> _cachedJson = {};

Character() {
init();
reset();
}

Character.fromMap(Map<String, dynamic> inputJson) {
fromMap(inputJson);
}

void newCharacter() {
final key = UniqueKey().toString();
_name = "New Character $key";
reset();
}

void notify() {
notifyListeners();
}

void init() async {
Logger.log("Character Initialised");

final prefs = await SharedPreferences.getInstance();

Map<String, dynamic> lastCharacter =
json.decode(prefs.getString("last_character") ?? "{}");

if (lastCharacter.isNotEmpty) {
Logger.log(lastCharacter.toString());
fromMap(lastCharacter);
} else {
await reset();
}
}

Character copy() {
Character newCharacter = Character();
newCharacter.from(this);
Expand Down Expand Up @@ -92,10 +69,6 @@ class Character extends ChangeNotifier {
_profile = await Utilities.fileFromAssetImage("defaultCharacter.png");
}

if (inputJson.isEmpty) {
await reset();
}

if (inputJson["spec"] == "mcf_v1") {
Logger.log("Character loaded from MCF");
fromMCFMap(inputJson);
Expand All @@ -112,7 +85,6 @@ class Character extends ChangeNotifier {
await reset();
}

Logger.log("Character created with name: ${inputJson["name"]}");
_useExamples = _examples.isNotEmpty;
notifyListeners();
}
Expand Down
23 changes: 6 additions & 17 deletions lib/providers/session.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:maid_llm/src/chat_node_tree.dart';
import 'package:maid/classes/google_gemini_model.dart';
import 'package:maid/classes/large_language_model.dart';
import 'package:maid/classes/llama_cpp_model.dart';
Expand All @@ -11,7 +10,7 @@ import 'package:maid/classes/open_ai_model.dart';
import 'package:maid/providers/character.dart';
import 'package:maid/providers/user.dart';
import 'package:maid/static/logger.dart';
import 'package:maid_llm/src/chat_node.dart';
import 'package:maid_llm/maid_llm.dart';
import 'package:maid/static/utilities.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
Expand Down Expand Up @@ -40,21 +39,6 @@ class Session extends ChangeNotifier {
notifyListeners();
}

void init() async {
Logger.log("Session Initialised");

final prefs = await SharedPreferences.getInstance();

Map<String, dynamic> lastSession =
json.decode(prefs.getString("last_session") ?? "{}") ?? {};

if (lastSession.isNotEmpty) {
fromMap(lastSession);
} else {
newSession();
}
}

void notify() {
notifyListeners();
}
Expand Down Expand Up @@ -91,6 +75,11 @@ class Session extends ChangeNotifier {
}

void fromMap(Map<String, dynamic> inputJson) {
if (inputJson.isEmpty) {
newSession();
return;
}

chat.root = ChatNode.fromMap(inputJson['chat'] ?? {});

final type = LargeLanguageModelType.values[inputJson['llm_type'] ?? LargeLanguageModelType.llamacpp.index];
Expand Down
38 changes: 20 additions & 18 deletions lib/providers/user.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import 'dart:io';
import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:maid/static/utilities.dart';
import 'package:shared_preferences/shared_preferences.dart';

class User extends ChangeNotifier {
File? _profile;
String _name = "User";

User() {
reset();
}

User.from(User user) {
_profile = profileFile;
_name = user.name;
}

User.fromMap(Map<String, dynamic> inputMap) {
fromMap(inputMap);
}

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

File? get profileFile => _profile;
String get name => _name;

set profile(Future<File> value) {
Expand All @@ -27,29 +39,19 @@ class User extends ChangeNotifier {
notifyListeners();
}

void init() async {
final prefs = await SharedPreferences.getInstance();

Map<String, dynamic> lastUser =
json.decode(prefs.getString("last_user") ?? "{}") ?? {};

if (lastUser.isNotEmpty) {
fromMap(lastUser);
} else {
void fromMap(Map<String, dynamic> inputMap) async {
if (inputMap.isEmpty) {
reset();
return;
}

notifyListeners();
}

void fromMap(Map<String, dynamic> inputJson) async {
if (inputJson["profile"] != null) {
_profile = File(inputJson["profile"]);
if (inputMap["profile"] != null) {
_profile = File(inputMap["profile"]);
} else {
_profile ??= await Utilities.fileFromAssetImage("chadUser.png");
}

_name = inputJson["name"];
_name = inputMap["name"];
notifyListeners();
}

Expand Down
107 changes: 46 additions & 61 deletions lib/ui/mobile/pages/character/character_browser_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,71 +54,56 @@ class _CharacterBrowserPageState extends State<CharacterBrowserPage> {

@override
Widget build(BuildContext context) {
return Consumer<Character>(
builder: (context, character, child) {
// If characters contains a character where character.key == current,
// then insert a copy of character at index 0
current = character.key;

var contains = false;

for (var element in characters) {
if (element.key == current) {
contains = true;
break;
}
}

if (!contains) {
characters.insert(0, character.copy());
}

return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.background,
foregroundColor: Theme.of(context).colorScheme.onPrimary,
elevation: 0.0,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();
},
),
title: const Text("Character Browser"),
actions: [
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.background,
foregroundColor: Theme.of(context).colorScheme.onPrimary,
elevation: 0.0,
leading: IconButton(
icon: const Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();
},
),
title: const Text("Character Browser"),
actions: [
IconButton(
icon: const Icon(Icons.add),
onPressed: () {
setState(() {
final newCharacter = Character();
characters.add(newCharacter);
context.read<Character>().from(newCharacter);
});
},
),
],
),
body: SessionBusyOverlay(
child: GridView.builder(
itemCount: characters.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(
8.0), // Adjust the padding value as needed
child: CharacterBrowserTile(
character: characters[index],
onDelete: () {
setState(() {
final newCharacter = Character();
characters.add(newCharacter);
character.from(newCharacter);
characters.removeAt(index);
});
},
),
],
),
body: SessionBusyOverlay(
child: ListView.builder(
itemCount: characters.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.all(
8.0), // Adjust the padding value as needed
child: CharacterBrowserTile(
character: characters[index],
onDelete: () {
setState(() {
characters.removeAt(index);
});
},
),
);
},
)
)
);
},
);
},
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
maxCrossAxisExtent: 200,
crossAxisSpacing: 8.0,
mainAxisSpacing: 8.0,
childAspectRatio: 0.75
)
)
)
);
}
}
Loading

0 comments on commit 95bc234

Please sign in to comment.