Skip to content

Commit

Permalink
work on character pages (#427)
Browse files Browse the repository at this point in the history
* charatcer tile

* work on character br tile

* character drawer tile

* character browser stuff

* character browser
  • Loading branch information
danemadsen committed Mar 12, 2024
1 parent aaa68f9 commit 7e6914a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 228 deletions.
19 changes: 19 additions & 0 deletions lib/providers/character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class Character extends ChangeNotifier {
Key _key = UniqueKey();
File _profile = File("assets/defaultCharacter.png");
String _name = "Maid";
String _description = "";
Expand Down Expand Up @@ -58,6 +59,22 @@ class Character extends ChangeNotifier {
}
}

void from(Character character) {
_key = character.key;
_profile = character.profile;
_name = character.name;
_description = character.description;
_personality = character.personality;
_scenario = character.scenario;
_useGreeting = character.useGreeting;
_greetings = character.greetings;
_system = character.system;
_useExamples = character.useExamples;
_examples = character.examples;

notifyListeners();
}

void fromMap(Map<String, dynamic> inputJson) async {
if (inputJson["profile"] != null) {
_profile = File(inputJson["profile"]);
Expand Down Expand Up @@ -221,6 +238,8 @@ class Character extends ChangeNotifier {
notifyListeners();
}

Key get key => _key;

File get profile => _profile;

String get name => _name;
Expand Down
65 changes: 33 additions & 32 deletions lib/ui/mobile/pages/character/character_browser_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
import 'package:maid/providers/character.dart';
import 'package:maid/ui/mobile/widgets/appbars/generic_app_bar.dart';
import 'package:maid/ui/mobile/widgets/tiles/character_browser_tile.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class CharacterBrowserPage extends StatefulWidget {
Expand All @@ -15,54 +14,56 @@ class CharacterBrowserPage extends StatefulWidget {
}

class _CharacterBrowserPageState extends State<CharacterBrowserPage> {
final _characters = <String, dynamic>{};
// Changed from Map to List of Character
final List<Character> _characters = [];

@override
void initState() {
super.initState();
SharedPreferences.getInstance().then((prefs) {
final loadedCharacters =
json.decode(prefs.getString("characters") ?? "{}");
_characters.addAll(loadedCharacters);
_loadCharacters();
}

Future<void> _loadCharacters() async {
final prefs = await SharedPreferences.getInstance();
final String charactersJson = prefs.getString("characters") ?? '[]';
final List charactersList = json.decode(charactersJson);

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

setState(() {
_characters.clear();
_characters.add(Character.fromMap(lastCharacterMap));
for (var characterMap in charactersList) {
_characters.add(Character.fromMap(characterMap));
}
});
}

@override
void dispose() {
SharedPreferences.getInstance().then((prefs) {
prefs.setString("characters", json.encode(_characters));
});

_saveCharacters();
super.dispose();
}

Future<void> _saveCharacters() async {
final prefs = await SharedPreferences.getInstance();
final String charactersJson = json.encode(_characters.map((character) => character.toMap()).toList());
await prefs.setString("characters", charactersJson);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: const GenericAppBar(title: "Character Browser"),
body: Consumer<Character>(
builder: (context, character, child) {
_characters[character.name] = character.toMap();

SharedPreferences.getInstance().then((prefs) {
prefs.setString("last_character", json.encode(character.toMap()));
});

return GridView(
padding: const EdgeInsets.all(8.0),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 6.0,
mainAxisSpacing: 6.0,
childAspectRatio: 3/2,
),
children: [
for (var character in _characters.entries)
CharacterBrowserTile(character: Character.fromMap(character.value))
],
);
body: ListView.builder(
padding: const EdgeInsets.all(8),
itemCount: _characters.length,
itemBuilder: (context, index) {
final character = _characters[index];
return CharacterBrowserTile(character: character);
},
),
);
}
}
}
157 changes: 1 addition & 156 deletions lib/ui/mobile/pages/character/character_customization_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:maid/providers/character.dart';
import 'package:maid/providers/session.dart';
import 'package:maid/static/logger.dart';
import 'package:maid/ui/mobile/pages/character/character_browser_page.dart';
import 'package:maid/ui/mobile/widgets/appbars/generic_app_bar.dart';
import 'package:maid/ui/mobile/widgets/dialogs.dart';
Expand All @@ -19,8 +18,6 @@ class CharacterCustomizationPage extends StatefulWidget {
}

class _CharacterCustomizationPageState extends State<CharacterCustomizationPage> {
late Map<String, dynamic> _characters;

late TextEditingController _nameController;
late TextEditingController _descriptionController;
late TextEditingController _personalityController;
Expand All @@ -29,31 +26,9 @@ class _CharacterCustomizationPageState extends State<CharacterCustomizationPage>
late List<TextEditingController> _greetingControllers;
late List<TextEditingController> _exampleControllers;

@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
final prefs = await SharedPreferences.getInstance();
final loadedCharacters =
json.decode(prefs.getString("characters") ?? "{}");
_characters.addAll(loadedCharacters);
setState(() {});
});
}

@override
void dispose() {
SharedPreferences.getInstance().then((prefs) {
prefs.setString("characters", json.encode(_characters));
});

super.dispose();
}

@override
Widget build(BuildContext context) {
final character = context.read<Character>();
_characters = {character.name: character.toMap()};

_nameController = TextEditingController(text: character.name);
_descriptionController = TextEditingController(text: character.description);
Expand All @@ -76,7 +51,6 @@ class _CharacterCustomizationPageState extends State<CharacterCustomizationPage>
appBar: const GenericAppBar(title: "Character Customization"),
body: Consumer<Character>(
builder: (context, character, child) {
_characters[character.name] = character.toMap();

SharedPreferences.getInstance().then((prefs) {
prefs.setString("last_character", json.encode(character.toMap()));
Expand Down Expand Up @@ -205,17 +179,7 @@ class _CharacterCustomizationPageState extends State<CharacterCustomizationPage>
),
controller: _nameController,
onChanged: (value) {
if (_characters.keys.contains(value)) {
character.fromMap(_characters[value] ?? {});
Logger.log(
"Character Set: ${character.name}");
} else if (value.isNotEmpty) {
String oldName = character.name;
Logger.log(
"Updating character $oldName ====> $value");
character.name = value;
_characters.remove(oldName);
}
character.name = value;
},
),
),
Expand Down Expand Up @@ -382,123 +346,4 @@ class _CharacterCustomizationPageState extends State<CharacterCustomizationPage>
},
));
}

void switchCharacter() {
showDialog(
context: context,
builder: (BuildContext context) {
return Consumer<Character>(
builder: (context, character, child) {
return AlertDialog(
title: const Text(
"Switch Character",
textAlign: TextAlign.center,
),
content: SizedBox(
height: 200,
width: 200,
child: ListView.builder(
itemCount: _characters.keys.length,
itemBuilder: (BuildContext context,
int index) {
final item = _characters.keys
.elementAt(index);

return Padding(
padding:
const EdgeInsets.all(8.0),
child: Dismissible(
key: ValueKey(item),
background: Container(
color: Colors.red),
onDismissed: (direction) {
setState(() {
_characters.remove(item);
if (character.name ==
item) {
character.fromMap(
_characters.values
.lastOrNull ??
{});
}
});
Logger.log(
"Character Removed: $item");
Navigator.of(context).pop();
},
child: Container(
decoration: BoxDecoration(
color: character.name ==
item
? Theme.of(context)
.colorScheme
.tertiary
: Theme.of(context)
.colorScheme
.primary,
borderRadius:
const BorderRadius
.all(
Radius.circular(
15.0)),
),
child: ListTile(
title: Text(
item,
textAlign:
TextAlign.center,
),
onTap: () {
character.fromMap(
_characters[item]);
Logger.log(
"Character Set: ${character.name}");
Navigator.of(context)
.pop();
},
),
),
),
);
},
),
),
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(20.0)),
),
actions: [
FilledButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(
"Close",
style: Theme.of(context)
.textTheme
.labelLarge,
),
),
FilledButton(
onPressed: () {
_characters[character.name] =
character.toMap();
character.newCharacter();
_characters[character.name] =
character.toMap();
character.notify();
},
child: Text(
"New Preset",
style: Theme.of(context)
.textTheme
.labelLarge,
),
),
],
);
},
);
});
}
}

0 comments on commit 7e6914a

Please sign in to comment.