Skip to content

Commit

Permalink
Refactor away from AsyncValue.when
Browse files Browse the repository at this point in the history
Into Dart 3 patterns as per riverpod recommendations
  • Loading branch information
r52 committed Jul 16, 2023
1 parent bd719b4 commit 9f58e0f
Show file tree
Hide file tree
Showing 16 changed files with 979 additions and 1,004 deletions.
72 changes: 35 additions & 37 deletions lib/local/archive_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,56 +102,54 @@ class ArchiveReaderWidget extends ConsumerWidget {
strtitle = title!;
}

return pages.when(
skipLoadingOnReload: true,
data: (result) {
if (result.isEmpty) {
switch (pages) {
case AsyncValue(:final error?, :final stackTrace?):
final messenger = ScaffoldMessenger.of(context);
Styles.showErrorSnackBar(messenger, '$error');
logger.e("_getArchivePagesProvider($path) failed", error, stackTrace);

return Scaffold(
appBar: AppBar(
leading: const BackButton(),
),
body: Styles.errorColumn(error, stackTrace),
);
case AsyncValue(:final value?):
if (value.isEmpty) {
return const Center(
child: Text("This archive contains no images!"),
);
}

return ReaderWidget(
pages: result,
pageCount: result.length,
pages: value,
pageCount: value.length,
title: strtitle,
isLongStrip: false, // TODO longstrip
link: link,
onLinkPressed: onLinkPressed,
);
},
loading: () => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Extracting archive...",
style: TextStyle(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.normal,
fontSize: 18,
decoration: TextDecoration.none,
case _:
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Extracting archive...",
style: TextStyle(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.normal,
fontSize: 18,
decoration: TextDecoration.none,
),
),
),
const SizedBox(
height: 20,
),
const CircularProgressIndicator()
],
),
),
error: (err, stackTrace) {
final messenger = ScaffoldMessenger.of(context);
Styles.showErrorSnackBar(messenger, '$err');
logger.e("_getArchivePagesProvider($path) failed", err, stackTrace);

return Scaffold(
appBar: AppBar(
leading: const BackButton(),
const SizedBox(
height: 20,
),
const CircularProgressIndicator()
],
),
body: Styles.errorColumn(err, stackTrace),
);
},
);
}
}
}
72 changes: 35 additions & 37 deletions lib/local/directory_reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,56 +75,54 @@ class DirectoryReaderWidget extends ConsumerWidget {
strtitle = title!;
}

return pages.when(
skipLoadingOnReload: true,
data: (result) {
if (result.isEmpty) {
switch (pages) {
case AsyncValue(:final error?, :final stackTrace?):
final messenger = ScaffoldMessenger.of(context);
Styles.showErrorSnackBar(messenger, '$error');
logger.e("_getDirectoryPagesProvider($path) failed", error, stackTrace);

return Scaffold(
appBar: AppBar(
leading: const BackButton(),
),
body: Styles.errorColumn(error, stackTrace),
);
case AsyncValue(:final value?):
if (value.isEmpty) {
return const Center(
child: Text("This archive contains no images!"),
);
}

return ReaderWidget(
pages: result,
pageCount: result.length,
pages: value,
pageCount: value.length,
title: strtitle,
isLongStrip: false, // TODO longstrip
link: link,
onLinkPressed: onLinkPressed,
);
},
loading: () => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Loading directory...",
style: TextStyle(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.normal,
fontSize: 18,
decoration: TextDecoration.none,
case _:
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Loading directory...",
style: TextStyle(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.normal,
fontSize: 18,
decoration: TextDecoration.none,
),
),
),
const SizedBox(
height: 20,
),
const CircularProgressIndicator()
],
),
),
error: (err, stackTrace) {
final messenger = ScaffoldMessenger.of(context);
Styles.showErrorSnackBar(messenger, '$err');
logger.e("_getDirectoryPagesProvider($path) failed", err, stackTrace);

return Scaffold(
appBar: AppBar(
leading: const BackButton(),
const SizedBox(
height: 20,
),
const CircularProgressIndicator()
],
),
body: Styles.errorColumn(err, stackTrace),
);
},
);
}
}
}
62 changes: 30 additions & 32 deletions lib/local/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,12 @@ class LocalLibraryHome extends HookConsumerWidget {
);
}

return result.when(
skipLoadingOnReload: false,
data: (top) {
currentItem.value ??= top;
switch (result) {
case AsyncData(:final value):
currentItem.value ??= value;
Widget child;

if (top.children.isEmpty) {
if (value.children.isEmpty) {
child = Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
Expand Down Expand Up @@ -184,40 +183,39 @@ class LocalLibraryHome extends HookConsumerWidget {
},
child: child,
);
},
loading: () => Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Scanning library...",
style: TextStyle(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.normal,
fontSize: 18,
decoration: TextDecoration.none,
),
),
const SizedBox(
height: 20,
),
const CircularProgressIndicator()
],
),
),
error: (err, stackTrace) {
case AsyncError(:final error, :final stackTrace):
final messenger = ScaffoldMessenger.of(context);
Styles.showErrorSnackBar(messenger, '$err');
logger.e("localLibraryProvider failed", err, stackTrace);
Styles.showErrorSnackBar(messenger, '$error');
logger.e("localLibraryProvider failed", error, stackTrace);

return RefreshIndicator(
onRefresh: () async {
return await ref.refresh(localLibraryProvider.future);
},
child: Styles.errorList(err, stackTrace),
child: Styles.errorList(error, stackTrace),
);
},
);
case _:
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Scanning library...",
style: TextStyle(
color: theme.colorScheme.onSurface,
fontWeight: FontWeight.normal,
fontSize: 18,
decoration: TextDecoration.none,
),
),
const SizedBox(
height: 20,
),
const CircularProgressIndicator()
],
),
);
}
}(),
);
}
Expand Down
Loading

0 comments on commit 9f58e0f

Please sign in to comment.