Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
refactor(anime_page): better state & error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Oct 29, 2021
1 parent 698292e commit 72828ad
Show file tree
Hide file tree
Showing 40 changed files with 1,143 additions and 712 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ You can download the latest version of app from the [releases](https://github.co
- [GitHub](https://github.com/yukino-app/yukino/)
- [Patreon](https://patreon.com/zyrouge/)

For any queries, contact me at [zyrouge@hotmail.com](mailto:zyrouge@hotmail.com).

## Branding

### Colors
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/windows/run/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { spawn } from "../../../spawn";
import { config } from "../../../config";
import { Logger } from "../../../logger";

const logger = new Logger("run:windows");
const logger = new Logger("windows:run");

export const debug = async () => {
logger.log("Running debug command...");
Expand Down
20 changes: 10 additions & 10 deletions lib/modules/app/lifecycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ abstract class AppLifecycle {
);
}

try {
await AppState.initialize();
Logger.of('AppState').info('Finished "initialize"');
} catch (err, trace) {
Logger.of('AppState').error(
'"initialize" failed: $err',
trace,
);
}

final TranslationSentences? settingsLocale =
AppState.settings.value.locale != null
? Translator.tryGetTranslation(AppState.settings.value.locale!)
Expand Down Expand Up @@ -128,16 +138,6 @@ abstract class AppLifecycle {
);
}

try {
await AppState.initialize();
Logger.of('AppState').info('Finished "initialize"');
} catch (err, trace) {
Logger.of('AppState').error(
'"initialize" failed: $err',
trace,
);
}

try {
await ProtocolHandler.register();
Logger.of('ProtocolHandler').info('Finished "register"');
Expand Down
5 changes: 2 additions & 3 deletions lib/modules/app/state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import '../database/database.dart';
import '../state/eventer.dart';

abstract class AppState {
static final ReactiveEventer<SettingsSchema> settings =
ReactiveEventer<SettingsSchema>();
static late final ReactiveEventer<SettingsSchema> settings;

static Future<void> initialize() async {
AppState.settings.value = SettingsBox.get();
settings = ReactiveEventer<SettingsSchema>(SettingsBox.get());
}

static bool get isDesktop =>
Expand Down
22 changes: 17 additions & 5 deletions lib/modules/database/objectbox/objectbox-model.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,31 @@
},
{
"id": "2:2",
"lastPropertyId": "1:588855148000686305",
"lastPropertyId": "3:6160269151672726852",
"name": "CredentialsSchema",
"properties": [
{
"id": "1:588855148000686305",
"name": "id",
"type": 6,
"flags": 1
},
{
"id": "2:9043178301598374257",
"name": "anilist_",
"type": 9
},
{
"id": "3:6160269151672726852",
"name": "myanimelist_",
"type": 9
}
],
"relations": []
},
{
"id": "3:3",
"lastPropertyId": "2:1921708900049052479",
"lastPropertyId": "3:5523022497379988698",
"name": "PreferencesSchema",
"properties": [
{
Expand All @@ -58,8 +68,8 @@
"flags": 1
},
{
"id": "2:1921708900049052479",
"name": "lastSelectedSearchType",
"id": "3:5523022497379988698",
"name": "lastSelectedSearch_",
"type": 9
}
],
Expand Down Expand Up @@ -163,7 +173,9 @@
"modelVersionParserMinimum": 5,
"retiredEntityUids": [],
"retiredIndexUids": [],
"retiredPropertyUids": [],
"retiredPropertyUids": [
1921708900049052479
],
"retiredRelationUids": [],
"version": 1
}
2 changes: 1 addition & 1 deletion lib/modules/database/schemas/cache/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class CacheBox {
final int cachedTime,
) async {
await save(
CacheSchema()
(get(key) ?? CacheSchema())
..key = key
..value = value
..cachedTime = cachedTime,
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/database/schemas/credentials/credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ abstract class CredentialsBox {
static Box<CredentialsSchema> get box =>
Box<CredentialsSchema>(DatabaseManager.store);

static CredentialsSchema get() => box.get(0) ?? CredentialsSchema();
static CredentialsSchema get() =>
box.query().build().findUnique() ?? CredentialsSchema();

static Future<void> save(final CredentialsSchema value) async {
await box.putAsync(value);
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/database/schemas/preferences/preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ abstract class PreferencesBox {
static Box<PreferencesSchema> get box =>
Box<PreferencesSchema>(DatabaseManager.store);

static PreferencesSchema get() => box.get(0) ?? PreferencesSchema();
static PreferencesSchema get() =>
box.query().build().findUnique() ?? PreferencesSchema();

static Future<void> save(final PreferencesSchema value) async {
await box.putAsync(value);
Expand Down
27 changes: 20 additions & 7 deletions lib/modules/database/schemas/preferences/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:extensions/extensions.dart';
import 'package:objectbox/objectbox.dart';

class LastSelectedSearchPlugin {
LastSelectedSearchPlugin({
const LastSelectedSearchPlugin({
this.lastSelectedType,
this.lastSelectedAnimePlugin,
this.lastSelectedMangaPlugin,
Expand All @@ -20,9 +20,22 @@ class LastSelectedSearchPlugin {
lastSelectedMangaPlugin: json['lastSelectedMangaPlugin'] as String?,
);

ExtensionType? lastSelectedType;
String? lastSelectedAnimePlugin;
String? lastSelectedMangaPlugin;
final ExtensionType? lastSelectedType;
final String? lastSelectedAnimePlugin;
final String? lastSelectedMangaPlugin;

LastSelectedSearchPlugin copyWith({
final ExtensionType? lastSelectedType,
final String? lastSelectedAnimePlugin,
final String? lastSelectedMangaPlugin,
}) =>
LastSelectedSearchPlugin(
lastSelectedType: lastSelectedType ?? this.lastSelectedType,
lastSelectedAnimePlugin:
lastSelectedAnimePlugin ?? this.lastSelectedAnimePlugin,
lastSelectedMangaPlugin:
lastSelectedMangaPlugin ?? this.lastSelectedMangaPlugin,
);

bool get isEmpty =>
lastSelectedType == null &&
Expand Down Expand Up @@ -50,9 +63,9 @@ class PreferencesSchema {
? LastSelectedSearchPlugin.fromJson(
json.decode(lastSelectedSearch_!) as Map<dynamic, dynamic>,
)
: LastSelectedSearchPlugin();
: const LastSelectedSearchPlugin();

set lastSelectedSearch(final LastSelectedSearchPlugin? val) {
lastSelectedSearch_ = val != null ? json.encode(val.toJson()) : null;
set lastSelectedSearch(final LastSelectedSearchPlugin val) {
lastSelectedSearch_ = json.encode(val.toJson());
}
}
3 changes: 2 additions & 1 deletion lib/modules/database/schemas/settings/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ abstract class SettingsBox {
static Box<SettingsSchema> get box =>
Box<SettingsSchema>(DatabaseManager.store);

static SettingsSchema get() => box.get(0) ?? SettingsSchema();
static SettingsSchema get() =>
box.query().build().findUnique() ?? SettingsSchema();

static Future<void> save(final SettingsSchema value) async {
await box.putAsync(value);
Expand Down
30 changes: 30 additions & 0 deletions lib/modules/helpers/faces.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
abstract class KawaiiFaces {
// Source: https://kawaiiface.net/sad-kawaii-faces/
static const List<String> sad = <String>[
'ಥ_ಥ',
'o(╥﹏╥)o',
r'( /)w(\✿)',
'(╯︵╰,)',
'(︶︹︺)',
'(◡﹏◡✿)',
'v( ‘.’ )v',
'(ㄒoㄒ)',
'┐(‘~`;)┌',
'╮(─▽─)╭',
'ಥ‿ಥ',
'(╥╥)',
'(∩︵∩)',
'(。-_-。)',
'(╯_╰)',
"<('.'<)",
'ಠ,ಥ',
'(◕︵◕)',
'(◕﹏◕✿)',
'(+_+)',
'★~(◠︿◕✿)',
'(*´д`*)',
'(ノ_・。)',
'┐(‘~`;)┌',
'(つд`)',
];
}
21 changes: 21 additions & 0 deletions lib/modules/helpers/ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ abstract class ResponsiveSizes {
static const int md = 768;
static const int lg = 1024;
static const int xl = 1280;

static bool isXs(final double size) => size > xs;
static bool isSm(final double size) => size > sm;
static bool isMd(final double size) => size > md;
static bool isLg(final double size) => size > lg;
static bool isXl(final double size) => size > xl;
}

class ResponsiveSizeInfo {
const ResponsiveSizeInfo(this.size);

factory ResponsiveSizeInfo.fromContext(final BuildContext context) =>
ResponsiveSizeInfo(MediaQuery.of(context).size.width);

final double size;

bool get isXs => ResponsiveSizes.isXs(size);
bool get isSm => ResponsiveSizes.isSm(size);
bool get isMd => ResponsiveSizes.isMd(size);
bool get isLg => ResponsiveSizes.isLg(size);
bool get isXl => ResponsiveSizes.isXl(size);
}

abstract class Palette {
Expand Down
16 changes: 3 additions & 13 deletions lib/modules/state/eventer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ class Eventer<T> {
typedef ReactiveEventerSubscriber<T> = void Function(T current, T previous);

class ReactiveEventer<T> {
ReactiveEventer([final T? value]) {
if (value != null) {
_value = value;
}
}
ReactiveEventer(this._value);

T? _value;
T _value;
final List<ReactiveEventerSubscriber<T>> subscribers =
<ReactiveEventerSubscriber<T>>[];

Expand All @@ -41,13 +37,7 @@ class ReactiveEventer<T> {
_value = modified;
}

T get value {
if (_value == null) {
throw StateError('Missing initialization of value');
}

return _value!;
}
T get value => _value;

void Function() subscribe(final ReactiveEventerSubscriber<T> sub) {
subscribers.add(sub);
Expand Down
8 changes: 0 additions & 8 deletions lib/modules/state/holder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,4 @@ class StatefulValueHolder<T> {
void fail(final T value) {
_state(value, ReactiveStates.failed);
}

bool get isWaiting => state == ReactiveStates.waiting;
bool get isResolving => state == ReactiveStates.resolving;
bool get hasResolved => state == ReactiveStates.resolved;
bool get hasFailed => state == ReactiveStates.failed;
bool get hasValue => state == ReactiveStates.resolved;
bool get hasEnded =>
state == ReactiveStates.resolved || state == ReactiveStates.failed;
}
2 changes: 1 addition & 1 deletion lib/modules/state/hooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ mixin HooksMixin {
}

Future<void> maybeEmitReady() async {
if (hookState.ready != ReactiveStates.waiting &&
if (hookState.ready == ReactiveStates.waiting &&
hookState.onReady != null) {
hookState.ready = ReactiveStates.resolving;
try {
Expand Down
9 changes: 9 additions & 0 deletions lib/modules/state/states.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ enum ReactiveStates {
resolved,
failed,
}

extension ReactiveStatesUtils on ReactiveStates {
bool get isWaiting => this == ReactiveStates.waiting;
bool get isResolving => this == ReactiveStates.resolving;
bool get hasResolved => this == ReactiveStates.resolved;
bool get hasFailed => this == ReactiveStates.failed;
bool get hasEnded =>
this == ReactiveStates.resolved || this == ReactiveStates.failed;
}
4 changes: 2 additions & 2 deletions lib/modules/trackers/myanimelist/handlers/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class MyAnimeListHomeContent {

String get thumbnail {
final RegExpMatch? match =
RegExp(r'\/images.*\.(jpg|jpeg|png|webp)').firstMatch(url);
RegExp(r'\/images.*\.(jpg|jpeg|png|webp)').firstMatch(rawThumbnail);

if (match != null) {
return 'https://cdn.myanimelist.net${match.group(0)}';
}

return url;
return rawThumbnail;
}

int get id => int.parse(
Expand Down
1 change: 1 addition & 0 deletions lib/modules/translator/translations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,5 @@ abstract class TranslationSentences {
String volumesCompleted();
String nsfw();
String restartAppForChangesToTakeEffect();
String copyError();
}
5 changes: 4 additions & 1 deletion lib/modules/translator/translations/en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Sentences extends TranslationSentences {
String noResultsFound() => 'No results were found.';

@override
String failedToGetResults() => 'No results were found.';
String failedToGetResults() => 'Failed to get results.';

@override
String preferences() => 'Preferences';
Expand Down Expand Up @@ -387,4 +387,7 @@ class Sentences extends TranslationSentences {
@override
String restartAppForChangesToTakeEffect() =>
'Restart app for the changes to take effect';

@override
String copyError() => 'Copy error';
}
6 changes: 6 additions & 0 deletions lib/modules/utils/error.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ErrorInfo {
ErrorInfo(this.error, [this.stack]);

final Object error;
final StackTrace? stack;
}

0 comments on commit 72828ad

Please sign in to comment.