Skip to content

Commit

Permalink
Add note controls, improve server list
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 10, 2024
1 parent d00a699 commit d9418fb
Show file tree
Hide file tree
Showing 15 changed files with 424 additions and 246 deletions.
27 changes: 24 additions & 3 deletions api/lib/src/models/server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,37 @@ import 'package:dart_mappable/dart_mappable.dart';
part 'server.mapper.dart';

@MappableClass()
final class GameServer with GameServerMappable {
final String name, address;
sealed class GameServer with GameServerMappable {
final String address;
final bool secure;

GameServer({
this.name = '',
required this.address,
this.secure = true,
});

String get display => address;
}

@MappableClass()
sealed class LanGameServer extends GameServer with LanGameServerMappable {
LanGameServer({
required super.address,
super.secure,
});
}

@MappableClass()
final class ListGameServer extends GameServer with ListGameServerMappable {
final String name;

ListGameServer({
this.name = '',
required super.address,
super.secure = true,
});

@override
String get display => name.isEmpty ? address : name;
}

Expand Down
190 changes: 155 additions & 35 deletions api/lib/src/models/server.mapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ class GameServerMapper extends ClassMapperBase<GameServer> {
static GameServerMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = GameServerMapper._());
LanGameServerMapper.ensureInitialized();
ListGameServerMapper.ensureInitialized();
}
return _instance!;
}

@override
final String id = 'GameServer';

static String _$name(GameServer v) => v.name;
static const Field<GameServer, String> _f$name =
Field('name', _$name, opt: true, def: '');
static String _$address(GameServer v) => v.address;
static const Field<GameServer, String> _f$address =
Field('address', _$address);
Expand All @@ -32,16 +31,12 @@ class GameServerMapper extends ClassMapperBase<GameServer> {

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

static GameServer _instantiate(DecodingData data) {
return GameServer(
name: data.dec(_f$name),
address: data.dec(_f$address),
secure: data.dec(_f$secure));
throw MapperException.missingConstructor('GameServer');
}

@override
Expand All @@ -57,56 +52,181 @@ class GameServerMapper extends ClassMapperBase<GameServer> {
}

mixin GameServerMappable {
String toJson();
Map<String, dynamic> toMap();
GameServerCopyWith<GameServer, GameServer, GameServer> get copyWith;
}

abstract class GameServerCopyWith<$R, $In extends GameServer, $Out>
implements ClassCopyWith<$R, $In, $Out> {
$R call({String? address, bool? secure});
GameServerCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}

class LanGameServerMapper extends ClassMapperBase<LanGameServer> {
LanGameServerMapper._();

static LanGameServerMapper? _instance;
static LanGameServerMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = LanGameServerMapper._());
GameServerMapper.ensureInitialized();
}
return _instance!;
}

@override
final String id = 'LanGameServer';

static String _$address(LanGameServer v) => v.address;
static const Field<LanGameServer, String> _f$address =
Field('address', _$address);
static bool _$secure(LanGameServer v) => v.secure;
static const Field<LanGameServer, bool> _f$secure =
Field('secure', _$secure, opt: true, def: true);

@override
final MappableFields<LanGameServer> fields = const {
#address: _f$address,
#secure: _f$secure,
};

static LanGameServer _instantiate(DecodingData data) {
throw MapperException.missingConstructor('LanGameServer');
}

@override
final Function instantiate = _instantiate;

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

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

mixin LanGameServerMappable {
String toJson();
Map<String, dynamic> toMap();
LanGameServerCopyWith<LanGameServer, LanGameServer, LanGameServer>
get copyWith;
}

abstract class LanGameServerCopyWith<$R, $In extends LanGameServer, $Out>
implements GameServerCopyWith<$R, $In, $Out> {
@override
$R call({String? address, bool? secure});
LanGameServerCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
}

class ListGameServerMapper extends ClassMapperBase<ListGameServer> {
ListGameServerMapper._();

static ListGameServerMapper? _instance;
static ListGameServerMapper ensureInitialized() {
if (_instance == null) {
MapperContainer.globals.use(_instance = ListGameServerMapper._());
GameServerMapper.ensureInitialized();
}
return _instance!;
}

@override
final String id = 'ListGameServer';

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

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

static ListGameServer _instantiate(DecodingData data) {
return ListGameServer(
name: data.dec(_f$name),
address: data.dec(_f$address),
secure: data.dec(_f$secure));
}

@override
final Function instantiate = _instantiate;

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

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

mixin ListGameServerMappable {
String toJson() {
return GameServerMapper.ensureInitialized()
.encodeJson<GameServer>(this as GameServer);
return ListGameServerMapper.ensureInitialized()
.encodeJson<ListGameServer>(this as ListGameServer);
}

Map<String, dynamic> toMap() {
return GameServerMapper.ensureInitialized()
.encodeMap<GameServer>(this as GameServer);
return ListGameServerMapper.ensureInitialized()
.encodeMap<ListGameServer>(this as ListGameServer);
}

GameServerCopyWith<GameServer, GameServer, GameServer> get copyWith =>
_GameServerCopyWithImpl(this as GameServer, $identity, $identity);
ListGameServerCopyWith<ListGameServer, ListGameServer, ListGameServer>
get copyWith => _ListGameServerCopyWithImpl(
this as ListGameServer, $identity, $identity);
@override
String toString() {
return GameServerMapper.ensureInitialized()
.stringifyValue(this as GameServer);
return ListGameServerMapper.ensureInitialized()
.stringifyValue(this as ListGameServer);
}

@override
bool operator ==(Object other) {
return GameServerMapper.ensureInitialized()
.equalsValue(this as GameServer, other);
return ListGameServerMapper.ensureInitialized()
.equalsValue(this as ListGameServer, other);
}

@override
int get hashCode {
return GameServerMapper.ensureInitialized().hashValue(this as GameServer);
return ListGameServerMapper.ensureInitialized()
.hashValue(this as ListGameServer);
}
}

extension GameServerValueCopy<$R, $Out>
on ObjectCopyWith<$R, GameServer, $Out> {
GameServerCopyWith<$R, GameServer, $Out> get $asGameServer =>
$base.as((v, t, t2) => _GameServerCopyWithImpl(v, t, t2));
extension ListGameServerValueCopy<$R, $Out>
on ObjectCopyWith<$R, ListGameServer, $Out> {
ListGameServerCopyWith<$R, ListGameServer, $Out> get $asListGameServer =>
$base.as((v, t, t2) => _ListGameServerCopyWithImpl(v, t, t2));
}

abstract class GameServerCopyWith<$R, $In extends GameServer, $Out>
implements ClassCopyWith<$R, $In, $Out> {
abstract class ListGameServerCopyWith<$R, $In extends ListGameServer, $Out>
implements GameServerCopyWith<$R, $In, $Out> {
@override
$R call({String? name, String? address, bool? secure});
GameServerCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(Then<$Out2, $R2> t);
ListGameServerCopyWith<$R2, $In, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t);
}

class _GameServerCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, GameServer, $Out>
implements GameServerCopyWith<$R, GameServer, $Out> {
_GameServerCopyWithImpl(super.value, super.then, super.then2);
class _ListGameServerCopyWithImpl<$R, $Out>
extends ClassCopyWithBase<$R, ListGameServer, $Out>
implements ListGameServerCopyWith<$R, ListGameServer, $Out> {
_ListGameServerCopyWithImpl(super.value, super.then, super.then2);

@override
late final ClassMapperBase<GameServer> $mapper =
GameServerMapper.ensureInitialized();
late final ClassMapperBase<ListGameServer> $mapper =
ListGameServerMapper.ensureInitialized();
@override
$R call({String? name, String? address, bool? secure}) =>
$apply(FieldCopyWithData({
Expand All @@ -115,15 +235,15 @@ class _GameServerCopyWithImpl<$R, $Out>
if (secure != null) #secure: secure
}));
@override
GameServer $make(CopyWithData data) => GameServer(
ListGameServer $make(CopyWithData data) => ListGameServer(
name: data.get(#name, or: $value.name),
address: data.get(#address, or: $value.address),
secure: data.get(#secure, or: $value.secure));

@override
GameServerCopyWith<$R2, GameServer, $Out2> $chain<$R2, $Out2>(
ListGameServerCopyWith<$R2, ListGameServer, $Out2> $chain<$R2, $Out2>(
Then<$Out2, $R2> t) =>
_GameServerCopyWithImpl($value, $cast, t);
_ListGameServerCopyWithImpl($value, $cast, t);
}

class GamePropertyMapper extends ClassMapperBase<GameProperty> {
Expand Down
35 changes: 13 additions & 22 deletions app/lib/bloc/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
final String? lastVersion;
@override
final bool nativeTitleBar;
final bool showConnectOfficial, showConnectCustom, showConnectOnlyFavorites;
final bool showConnectYour, showConnectNetwork;
final GameProperty gameProperty;
final List<GameServer> servers;
final List<ListGameServer> servers;

const QuokkaSettings({
this.localeTag = '',
this.theme = ThemeMode.system,
this.design = '',
this.dataDirectory = '',
this.nativeTitleBar = false,
this.showConnectOfficial = true,
this.showConnectCustom = true,
this.showConnectOnlyFavorites = false,
this.showConnectYour = true,
this.showConnectNetwork = true,
this.lastVersion,
this.gameProperty = const GameProperty(),
this.servers = const [],
Expand All @@ -53,17 +52,15 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
dataDirectory: prefs.getString('dataDirectory') ?? '',
nativeTitleBar: prefs.getBool('nativeTitleBar') ?? false,
localeTag: prefs.getString('locale') ?? '',
showConnectOfficial: prefs.getBool('showConnectOfficial') ?? true,
showConnectCustom: prefs.getBool('showConnectCustom') ?? true,
showConnectOnlyFavorites:
prefs.getBool('showConnectOnlyFavorites') ?? false,
showConnectYour: prefs.getBool('showConnectYour') ?? true,
showConnectNetwork: prefs.getBool('showConnectNetwork') ?? true,
lastVersion: prefs.getString('lastVersion'),
gameProperty: prefs.containsKey('gameProperty')
? GamePropertyMapper.fromJson(prefs.getString('gameProperty')!)
: GameProperty.defaultProperty,
servers: prefs
.getStringList('servers')
?.map((e) => GameServerMapper.fromJson(e))
?.map((e) => ListGameServerMapper.fromJson(e))
.toList() ??
[],
);
Expand All @@ -75,9 +72,8 @@ class QuokkaSettings with QuokkaSettingsMappable implements LeapSettings {
await prefs.setString('dataDirectory', dataDirectory);
await prefs.setBool('nativeTitleBar', nativeTitleBar);
await prefs.setString('locale', localeTag);
await prefs.setBool('showConnectOfficial', showConnectOfficial);
await prefs.setBool('showConnectCustom', showConnectCustom);
await prefs.setBool('showConnectOnlyFavorites', showConnectOnlyFavorites);
await prefs.setBool('showConnectYour', showConnectYour);
await prefs.setBool('showConnectNetwork', showConnectNetwork);
if (lastVersion == null) {
if (prefs.containsKey('last_version')) {
await prefs.remove('last_version');
Expand Down Expand Up @@ -126,18 +122,13 @@ class SettingsCubit extends Cubit<QuokkaSettings>
return save();
}

Future<void> changeShowConnectOfficial(bool value) {
emit(state.copyWith(showConnectOfficial: value));
Future<void> changeShowConnectNetwork(bool value) {
emit(state.copyWith(showConnectNetwork: value));
return save();
}

Future<void> changeShowConnectCustom(bool value) {
emit(state.copyWith(showConnectCustom: value));
return save();
}

Future<void> changeShowConnectOnlyFavorites(bool value) {
emit(state.copyWith(showConnectOnlyFavorites: value));
Future<void> changeShowConnectYour(bool value) {
emit(state.copyWith(showConnectYour: value));
return save();
}

Expand Down
Loading

0 comments on commit d9418fb

Please sign in to comment.