first implementation finished
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
import 'package:riverpod/riverpod.dart';
|
||||
|
||||
extension AsyncExtension<T> on Result<T> {
|
||||
AsyncValue<T> toAsyncValue() {
|
||||
return switch (this) {
|
||||
Success(value: final value) => AsyncValue.data(value),
|
||||
// TODO: Handle this case.
|
||||
Failure(
|
||||
error: final error,
|
||||
) =>
|
||||
AsyncError(error.message, error.stackTrace ?? StackTrace.empty),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import 'package:exceptions/exceptions.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:universal_io/io.dart';
|
||||
|
||||
ErrorMessage handleHttpError(
|
||||
Exception exception,
|
||||
StackTrace stackTrace,
|
||||
) {
|
||||
return switch (exception) {
|
||||
SocketException() => const ErrorMessage(
|
||||
source: 'http',
|
||||
message: 'Could not contact remote service',
|
||||
),
|
||||
HttpException() => const ErrorMessage(
|
||||
source: 'http',
|
||||
message: 'Web request returned error',
|
||||
),
|
||||
FormatException() => const ErrorMessage(
|
||||
source: 'http',
|
||||
message: 'Bad response format',
|
||||
),
|
||||
ClientException() => const ErrorMessage(
|
||||
source: 'http',
|
||||
message: 'Could not contact remote service',
|
||||
),
|
||||
_ => ErrorMessage.fromException(
|
||||
exception,
|
||||
stackTrace,
|
||||
)
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import 'package:logger/logger.dart';
|
||||
|
||||
final logger = Logger();
|
||||
@@ -0,0 +1,11 @@
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:kagi_bang_bang/core/routing/routes.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'providers.g.dart';
|
||||
|
||||
@Riverpod(keepAlive: true)
|
||||
GoRouter router(RouterRef ref) => GoRouter(
|
||||
debugLogDiagnostics: true,
|
||||
routes: $appRoutes,
|
||||
);
|
||||
@@ -0,0 +1,24 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'providers.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$routerHash() => r'4ad45f430b1f43e427e21122d16bd761ca04ae0e';
|
||||
|
||||
/// See also [router].
|
||||
@ProviderFor(router)
|
||||
final routerProvider = Provider<GoRouter>.internal(
|
||||
router,
|
||||
name: r'routerProvider',
|
||||
debugGetCreateSourceHash:
|
||||
const bool.fromEnvironment('dart.vm.product') ? null : _$routerHash,
|
||||
dependencies: null,
|
||||
allTransitiveDependencies: null,
|
||||
);
|
||||
|
||||
typedef RouterRef = ProviderRef<GoRouter>;
|
||||
// ignore_for_file: type=lint
|
||||
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
|
||||
@@ -0,0 +1,43 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:kagi_bang_bang/features/search_browser/presentation/screens/browser.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/data/repositories/settings_repository.dart';
|
||||
import 'package:kagi_bang_bang/features/settings/presentation/screens/settings.dart';
|
||||
|
||||
part 'routes.g.dart';
|
||||
|
||||
@TypedGoRoute<KagiRoute>(
|
||||
path: '/',
|
||||
)
|
||||
class KagiRoute extends GoRouteData {
|
||||
KagiRoute();
|
||||
|
||||
@override
|
||||
Future<String?> redirect(BuildContext context, GoRouterState state) async {
|
||||
final hasKagiSession = await ProviderScope.containerOf(context)
|
||||
.read(settingsRepositoryProvider.future)
|
||||
.then((value) => value.kagiSession?.isNotEmpty ?? false);
|
||||
|
||||
if (!hasKagiSession) {
|
||||
return SettingsRoute().location;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, GoRouterState state) {
|
||||
return const KagiScreen();
|
||||
}
|
||||
}
|
||||
|
||||
@TypedGoRoute<SettingsRoute>(
|
||||
path: '/settings',
|
||||
)
|
||||
class SettingsRoute extends GoRouteData {
|
||||
@override
|
||||
Widget build(BuildContext context, GoRouterState state) {
|
||||
return const SettingsScreen();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'routes.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// GoRouterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
List<RouteBase> get $appRoutes => [
|
||||
$kagiRoute,
|
||||
$settingsRoute,
|
||||
];
|
||||
|
||||
RouteBase get $kagiRoute => GoRouteData.$route(
|
||||
path: '/',
|
||||
factory: $KagiRouteExtension._fromState,
|
||||
);
|
||||
|
||||
extension $KagiRouteExtension on KagiRoute {
|
||||
static KagiRoute _fromState(GoRouterState state) => KagiRoute();
|
||||
|
||||
String get location => GoRouteData.$location(
|
||||
'/',
|
||||
);
|
||||
|
||||
void go(BuildContext context) => context.go(location);
|
||||
|
||||
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
|
||||
|
||||
void pushReplacement(BuildContext context) =>
|
||||
context.pushReplacement(location);
|
||||
|
||||
void replace(BuildContext context) => context.replace(location);
|
||||
}
|
||||
|
||||
RouteBase get $settingsRoute => GoRouteData.$route(
|
||||
path: '/settings',
|
||||
factory: $SettingsRouteExtension._fromState,
|
||||
);
|
||||
|
||||
extension $SettingsRouteExtension on SettingsRoute {
|
||||
static SettingsRoute _fromState(GoRouterState state) => SettingsRoute();
|
||||
|
||||
String get location => GoRouteData.$location(
|
||||
'/settings',
|
||||
);
|
||||
|
||||
void go(BuildContext context) => context.go(location);
|
||||
|
||||
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
|
||||
|
||||
void pushReplacement(BuildContext context) =>
|
||||
context.pushReplacement(location);
|
||||
|
||||
void replace(BuildContext context) => context.replace(location);
|
||||
}
|
||||
Reference in New Issue
Block a user