Skip to content

Commit

Permalink
Add page switch system
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 6, 2024
1 parent 9f7709b commit caa44f3
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 4 deletions.
2 changes: 2 additions & 0 deletions api/lib/src/models/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class QuokkaData extends ArchiveData<QuokkaData> {
utf8.encode(table.toJson()),
);

Iterable<String> getTables() => getAssets(kGameTablePath, true);

FileMetadata? getMetadata() {
final data = getAsset(kPackMetadataPath);
if (data == null) {
Expand Down
7 changes: 7 additions & 0 deletions app/lib/bloc/world/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ class WorldBloc extends Bloc<PlayableWorldEvent, ClientWorldState> {
switchCellOnMove: event.value,
));
});
on<TableSwitched>((event, emit) {
emit(state.copyWith(
table: state.data.getTableOrDefault(event.name),
tableName: event.name,
data: state.data.setTable(state.table, state.tableName),
));
});
}

Future<void> save() async {
Expand Down
7 changes: 7 additions & 0 deletions app/lib/bloc/world/local.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ final class SwitchCellOnMoveChanged extends LocalWorldEvent

SwitchCellOnMoveChanged(this.value);
}

@MappableClass()
final class TableSwitched extends LocalWorldEvent with TableSwitchedMappable {
final String name;

TableSwitched([this.name = '']);
}
115 changes: 115 additions & 0 deletions app/lib/bloc/world/local.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,118 @@ class _SwitchCellOnMoveChangedCopyWithImpl<$R, $Out>
$chain<$R2, $Out2>(Then<$Out2, $R2> t) =>
_SwitchCellOnMoveChangedCopyWithImpl($value, $cast, t);
}

class TableSwitchedMapper extends SubClassMapperBase<TableSwitched> {
TableSwitchedMapper._();

static TableSwitchedMapper? _instance;
static TableSwitchedMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = TableSwitchedMapper._());
LocalWorldEventMapper.ensureInitialized().addSubMapper(_instance!);
}
return _instance!;
}

@override
final String id = 'TableSwitched';

static String _$name(TableSwitched v) => v.name;
static const Field<TableSwitched, String> _f$name =
Field('name', _$name, opt: true, def: '');

@override
final MappableFields<TableSwitched> fields = const {
#name: _f$name,
};

@override
final String discriminatorKey = 'type';
@override
final dynamic discriminatorValue = 'TableSwitched';
@override
late final ClassMapperBase superMapper =
LocalWorldEventMapper.ensureInitialized();

static TableSwitched _instantiate(DecodingData data) {
return TableSwitched(data.dec(_f$name));
}

@override
final Function instantiate = _instantiate;

static TableSwitched fromMap(Map<String, dynamic> map) {
return ensureInitialized().decodeMap<TableSwitched>(map);
}

static TableSwitched fromJson(String json) {
return ensureInitialized().decodeJson<TableSwitched>(json);
}
}

mixin TableSwitchedMappable {
String toJson() {
return TableSwitchedMapper.ensureInitialized()
.encodeJson<TableSwitched>(this as TableSwitched);
}

Map<String, dynamic> toMap() {
return TableSwitchedMapper.ensureInitialized()
.encodeMap<TableSwitched>(this as TableSwitched);
}

TableSwitchedCopyWith<TableSwitched, TableSwitched, TableSwitched>
get copyWith => _TableSwitchedCopyWithImpl(
this as TableSwitched, $identity, $identity);
@override
String toString() {
return TableSwitchedMapper.ensureInitialized()
.stringifyValue(this as TableSwitched);
}

@override
bool operator ==(Object other) {
return TableSwitchedMapper.ensureInitialized()
.equalsValue(this as TableSwitched, other);
}

@override
int get hashCode {
return TableSwitchedMapper.ensureInitialized()
.hashValue(this as TableSwitched);
}
}

extension TableSwitchedValueCopy<$R, $Out>
on ObjectCopyWith<$R, TableSwitched, $Out> {
TableSwitchedCopyWith<$R, TableSwitched, $Out> get $asTableSwitched =>
$base.as((v, t, t2) => _TableSwitchedCopyWithImpl(v, t, t2));
}

abstract class TableSwitchedCopyWith<$R, $In extends TableSwitched, $Out>
implements LocalWorldEventCopyWith<$R, $In, $Out> {
@override
$R call({String? name});
TableSwitchedCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}

class _TableSwitchedCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, TableSwitched, $Out>
implements TableSwitchedCopyWith<$R, TableSwitched, $Out> {
_TableSwitchedCopyWithImpl(super.value, super.then, super.then2);

@override
late final ClassMapperBase<TableSwitched> $mapper =
TableSwitchedMapper.ensureInitialized();
@override
$R call({String? name}) =>
$apply(FieldCopyWithData({if (name != null) #name: name}));
@override
TableSwitched $make(CopyWithData data) =>
TableSwitched(data.get(#name, or: $value.name));

@override
TableSwitchedCopyWith<$R2, TableSwitched, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_TableSwitchedCopyWithImpl($value, $cast, t);
}
5 changes: 4 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,8 @@
"switchCellOnMove": "Switch cell on move",
"addPack": "Add pack",
"removePack": "Remove pack",
"noTemplates": "There are no templates available"
"noTemplates": "There are no templates available",
"table": "Table",
"defaultTable": "Default table",
"switchTable": "Switch table"
}
107 changes: 104 additions & 3 deletions app/lib/pages/game/drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,108 @@ class GameDrawer extends StatelessWidget {
);
},
),
BlocBuilder<WorldBloc, ClientWorldState>(
buildWhen: (previous, current) =>
previous.tableName != current.tableName,
builder: (context, state) {
final bloc = context.read<WorldBloc>();
return ListTile(
leading: const Icon(PhosphorIconsLight.gridFour),
title: Text(AppLocalizations.of(context).table),
subtitle: Text(state.tableName.isEmpty
? AppLocalizations.of(context).defaultTable
: state.tableName),
onTap: () => showLeapBottomSheet(
context: context,
titleBuilder: (context) =>
Text(AppLocalizations.of(context).table),
actionsBuilder: (context) => [
IconButton(
icon: const Icon(
PhosphorIconsLight.arrowsLeftRight),
tooltip:
AppLocalizations.of(context).switchTable,
onPressed: () async {
String name = '';
final result = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: Text(AppLocalizations.of(context)
.switchTable),
content: TextField(
decoration: InputDecoration(
labelText:
AppLocalizations.of(context).name,
hintText: AppLocalizations.of(context)
.enterName,
filled: true,
),
onChanged: (value) => name = value,
onSubmitted: (value) =>
Navigator.of(context).pop(true),
autofocus: true,
),
actions: [
TextButton.icon(
onPressed: () =>
Navigator.of(context).pop(false),
label: Text(
AppLocalizations.of(context)
.cancel),
icon: const Icon(
PhosphorIconsLight.prohibit),
),
ElevatedButton.icon(
onPressed: () =>
Navigator.of(context).pop(true),
label: Text(
AppLocalizations.of(context)
.change),
icon: const Icon(
PhosphorIconsLight.check),
),
],
),
);
if (!(result ?? false)) return;
bloc.process(TableSwitched(name));
},
),
],
childrenBuilder: (context) => [
BlocBuilder<WorldBloc, WorldState>(
bloc: bloc,
buildWhen: (previous, current) =>
previous.tableName != current.tableName ||
previous.data != current.data,
builder: (context, state) {
final other = {
...state.data.getTables(),
state.tableName
}.where((e) => e.isNotEmpty).toList();
return Column(
children: [
ListTile(
title: Text(AppLocalizations.of(context)
.defaultTable),
selected: state.tableName == '',
onTap: () =>
bloc.process(TableSwitched()),
),
if (other.isNotEmpty) const Divider(),
...other.map((e) => ListTile(
title: Text(e),
selected: state.tableName == e,
onTap: () =>
bloc.process(TableSwitched(e)),
)),
],
);
},
)
]));
},
),
BlocBuilder<WorldBloc, ClientWorldState>(
buildWhen: (previous, current) =>
previous.table.background != current.table.background,
Expand Down Expand Up @@ -292,6 +394,7 @@ class GameDrawer extends StatelessWidget {
content: TextField(
decoration: InputDecoration(
labelText: AppLocalizations.of(context).name,
hintText: AppLocalizations.of(context).enterName,
filled: true,
),
onChanged: (value) => name = value,
Expand All @@ -304,9 +407,7 @@ class GameDrawer extends StatelessWidget {
child: Text(AppLocalizations.of(context).cancel),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(true);
},
onPressed: () => Navigator.of(context).pop(true),
child: Text(AppLocalizations.of(context).save),
),
],
Expand Down

0 comments on commit caa44f3

Please sign in to comment.