Skip to content

Commit

Permalink
user refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
danemadsen committed Aug 9, 2024
1 parent 085d506 commit 7827a9e
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 49 deletions.
18 changes: 2 additions & 16 deletions lib/classes/providers/app_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/character.dart';
import 'package:maid/classes/providers/session.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

AppData(this._sessions, this._characters, this._currentSession, this._currentCharacter, this._user) {
AppData(this._sessions, this._characters, this._currentSession, this._currentCharacter) {
_currentSession.addListener(notify);
_currentCharacter.addListener(notify);
}
Expand All @@ -19,8 +18,6 @@ class AppData extends ChangeNotifier {
final List<Character> _characters;
Session _currentSession;
Character _currentCharacter;

User _user;

List<Session> get sessions {
_sessions.removeWhere((element) => element == _currentSession);
Expand All @@ -38,8 +35,6 @@ class AppData extends ChangeNotifier {

Character get currentCharacter => _currentCharacter;

User get user => _user;

set currentSession(Session session) {
if (!currentSession.chat.tail.finalised) return;

Expand All @@ -62,11 +57,6 @@ class AppData extends ChangeNotifier {
save().then((value) => notifyListeners());
}

set user(User user) {
_user = user;
notifyListeners();
}

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

Expand All @@ -86,14 +76,12 @@ class AppData extends ChangeNotifier {

final session = await Session.last;
final character = await Character.last;
final user = await User.last;

return AppData(
sessions,
characters,
session,
character,
user
character
);
}

Expand All @@ -111,7 +99,6 @@ class AppData extends ChangeNotifier {
prefs.setString("characters", json.encode(characterMaps)),
_currentSession.save(),
_currentCharacter.save(),
_user.save()
];

await Future.wait(futures);
Expand Down Expand Up @@ -182,7 +169,6 @@ class AppData extends ChangeNotifier {
clearCharacters();
_currentCharacter = Character(notify);
_currentSession = Session(notify, 0);
_user = User(notify);
notifyListeners();
}

Expand Down
10 changes: 3 additions & 7 deletions lib/classes/providers/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/static/logger.dart';
import 'package:maid/classes/static/utilities.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class User extends ChangeNotifier {
Expand All @@ -19,13 +19,9 @@ class User extends ChangeNotifier {
File? _profile;
String _name = "User";

static User of(BuildContext context) => AppData.of(context).user;

User(VoidCallback? listener) {
if (listener != null) {
addListener(listener);
}
static User of(BuildContext context, { bool listen = false }) => Provider.of<User>(context, listen: listen);

User() {
reset();
}

Expand Down
10 changes: 8 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/app_preferences.dart';
import 'package:maid/classes/providers/artificial_intelligence.dart';
import 'package:maid/classes/providers/huggingface_selection.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/ui/desktop/app.dart';
import 'package:maid/ui/mobile/app.dart';
import 'package:provider/provider.dart';
Expand All @@ -16,12 +17,14 @@ void main() async {
AppPreferences appPreferences = await AppPreferences.last;
AppData appData = await AppData.last;
ArtificialIntelligence ai = await ArtificialIntelligence.last;
User user = await User.last;

runApp(
MaidApp(
appPreferences: appPreferences,
appData: appData,
ai: ai
ai: ai,
user: user
)
);
}
Expand All @@ -30,12 +33,14 @@ class MaidApp extends StatelessWidget {
final AppPreferences appPreferences;
final AppData appData;
final ArtificialIntelligence ai;
final User user;

const MaidApp({
super.key,
required this.appPreferences,
required this.appData,
required this.ai,
required this.ai,
required this.user,
});

@override
Expand All @@ -45,6 +50,7 @@ class MaidApp extends StatelessWidget {
ChangeNotifierProvider(create: (context) => appPreferences),
ChangeNotifierProvider(create: (context) => appData),
ChangeNotifierProvider(create: (context) => ai),
ChangeNotifierProvider(create: (context) => user),
ChangeNotifierProvider(create: (context) => HuggingfaceSelection())
],
child: Selector<AppPreferences, bool>(
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/desktop/buttons/user_button.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/desktop_navigator.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/ui/shared/utilities/future_avatar.dart';
import 'package:provider/provider.dart';

Expand All @@ -9,15 +9,15 @@ class UserButton extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Consumer<AppData>(
return Consumer<User>(
builder: userBuilder,
);
}

Widget userBuilder(BuildContext context, AppData appData, Widget? child) {
Widget userBuilder(BuildContext context, User user, Widget? child) {
return FutureAvatar(
key: appData.user.key,
image: appData.user.profile,
key: user.key,
image: user.profile,
radius: 16,
onPressed: () {
DesktopNavigator.of(context).navigateSettingsPanel("/user-settings");
Expand Down
8 changes: 4 additions & 4 deletions lib/ui/mobile/dialogs/user_image_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ class UserImageDialog extends StatelessWidget {

@override
Widget build(BuildContext context) {
return Consumer<AppData>(
return Consumer<User>(
builder: dialogBuilder,
);
}

Widget dialogBuilder(BuildContext context, AppData appData, Widget? child) {
Widget dialogBuilder(BuildContext context, User user, Widget? child) {
return AlertDialog(
title: const Text(
"Change Profile Picture",
textAlign: TextAlign.center,
),
content: buildContent(context, appData.user),
content: buildContent(context, user),
actions: [
FilledButton(
onPressed: appData.user.loadImage,
onPressed: user.loadImage,
child: const Text(
"Load Custom"
),
Expand Down
9 changes: 5 additions & 4 deletions lib/ui/mobile/dialogs/user_name_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:provider/provider.dart';

class UserNameDialog extends StatelessWidget {
const UserNameDialog({super.key});

@override
Widget build(BuildContext context) {
return Consumer<AppData>(
return Consumer<User>(
builder: dialogBuilder,
);
}

Widget dialogBuilder(BuildContext context, AppData appData, Widget? child) {
final controller = TextEditingController(text: appData.user.name);
Widget dialogBuilder(BuildContext context, User user, Widget? child) {
final controller = TextEditingController(text: user.name);

return AlertDialog(
title: const Text(
Expand All @@ -35,7 +36,7 @@ class UserNameDialog extends StatelessWidget {
),
FilledButton(
onPressed: () {
appData.user.name = controller.text;
user.name = controller.text;
Navigator.of(context).pop();
},
child: const Text(
Expand Down
11 changes: 6 additions & 5 deletions lib/ui/mobile/tiles/user_tile.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/ui/mobile/dialogs/user_name_dialog.dart';
import 'package:maid/ui/mobile/dialogs/user_image_dialog.dart';
import 'package:maid/ui/shared/utilities/future_avatar.dart';
Expand All @@ -15,23 +16,23 @@ class UserTile extends StatefulWidget {
class _UserTileState extends State<UserTile> {
@override
Widget build(BuildContext context) {
return Consumer<AppData>(
return Consumer<User>(
builder: listTileBuilder,
);
}

Widget listTileBuilder(BuildContext context, AppData appData, Widget? child) {
Widget listTileBuilder(BuildContext context, User user, Widget? child) {
return ListTile(
title: Text(
appData.user.name,
user.name,
style: TextStyle(
color: Theme.of(context).colorScheme.onPrimary,
fontSize: 20,
),
),
leading: FutureAvatar(
key: appData.user.key,
image: appData.user.profile,
key: user.key,
image: user.profile,
radius: 20,
),
trailing: PopupMenuButton(
Expand Down
7 changes: 4 additions & 3 deletions lib/ui/shared/chat_widgets/chat_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:flutter/material.dart';
import 'package:maid/classes/chat_node.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/enumerators/chat_role.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/static/utilities.dart';
Expand Down Expand Up @@ -35,8 +36,8 @@ class ChatBody extends StatelessWidget {
}

Widget _buildChat() {
return Consumer<AppData>(
builder: (context, appData, child) {
return Consumer2<AppData, User>(
builder: (context, appData, user, child) {
final session = appData.currentSession;
final character = appData.currentCharacter;

Expand All @@ -52,7 +53,7 @@ class ChatBody extends StatelessWidget {
if (character.greetings[index].isNotEmpty) {
final message = ChatNode(
role: ChatRole.assistant,
content: Utilities.formatPlaceholders(character.greetings[index], appData.user.name, character.name),
content: Utilities.formatPlaceholders(character.greetings[index], user.name, character.name),
finalised: true
);

Expand Down
6 changes: 3 additions & 3 deletions lib/ui/shared/chat_widgets/chat_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:flutter/material.dart';
import 'package:babylon_tts/babylon_tts.dart';
import 'package:maid/classes/providers/large_language_model.dart';
import 'package:maid/classes/providers/user.dart';
import 'package:maid/enumerators/chat_role.dart';
import 'package:maid/classes/providers/app_data.dart';
import 'package:maid/classes/providers/session.dart';
Expand Down Expand Up @@ -32,9 +33,8 @@ class _ChatMessageWidgetState extends State<ChatMessageWidget> with SingleTicker

@override
Widget build(BuildContext context) {
return Consumer<AppData>(
builder: (context, appData, child) {
final user = appData.user;
return Consumer2<AppData, User>(
builder: (context, appData, user, child) {
final session = appData.currentSession;
final character = appData.currentCharacter;

Expand Down

0 comments on commit 7827a9e

Please sign in to comment.