Skip to content

Commit

Permalink
Fix upload button loading indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Jun 17, 2024
1 parent d421b66 commit 7fbe1d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
18 changes: 5 additions & 13 deletions lib/src/accounts/controllers/folder_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,7 @@ class CreateFolderController extends _$CreateFolderController {
@riverpod
class UploadDeleteController extends _$UploadDeleteController {
@override
FutureOr<List<String>> build() {
final folders = ref.watch(folderProvider);

return List.generate(
folders.length,
(index) => folders[index].folderId,
);
}
FutureOr<void> build() {}

Future<void> upload(
FolderModel folderModel,
Expand All @@ -57,13 +50,12 @@ class UploadDeleteController extends _$UploadDeleteController {
final folderNotifier = ref.read(folderProvider.notifier);

state = const AsyncLoading();
state = await AsyncValue.guard(() {
folderNotifier.upload(
state = await AsyncValue.guard(
() => folderNotifier.upload(
folderModel,
filePath,
);
return Future.value([]);
});
),
);
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/src/accounts/controllers/folder_controller.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 24 additions & 26 deletions lib/src/home/views/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class _HomeViewState extends ConsumerState<HomeView> {

@override
void initState() {
final folders = ref.watch(folderProvider).toList();
final folders = ref.read(folderProvider).toList();
_watchers = folders.map((e) => DirectoryWatcher(e.folderPath)).toList();
super.initState();
}
Expand Down Expand Up @@ -90,11 +90,12 @@ class _HomeViewState extends ConsumerState<HomeView> {
final folderInfo = ref.watch(folderProvider);
final folderNotifier = ref.read(folderProvider.notifier);
final uploadDeleteController = ref.watch(uploadDeleteControllerProvider);
final progressVisibleList = useState(List.generate(
folderInfo.length,
(index) => false,
growable: true,
));
// final progressVisibleList = useState(List.generate(
// folderInfo.length,
// (index) => false,
// growable: true,
// ));
final currentLoadingIndex = useState(0);

ref.listen<AsyncValue>(
uploadDeleteControllerProvider,
Expand Down Expand Up @@ -149,7 +150,7 @@ class _HomeViewState extends ConsumerState<HomeView> {
// print(await openAppSettings());
// }

progressVisibleList.value = [...progressVisibleList.value, false];
// progressVisibleList.value = [...progressVisibleList.value, false];
if (context.mounted) {
await showDialog(
context: context,
Expand Down Expand Up @@ -195,7 +196,8 @@ class _HomeViewState extends ConsumerState<HomeView> {
trailing: Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Visibility(
visible: uploadDeleteController.isLoading,
visible: uploadDeleteController.isLoading &&
currentLoadingIndex.value == index,
child: const SizedBox(
width: 20,
height: 20,
Expand Down Expand Up @@ -253,11 +255,12 @@ class _HomeViewState extends ConsumerState<HomeView> {
child: TextButton(
child: const Icon(Icons.sync),
onPressed: () async {
// progressVisibleList.value = [
// ...progressVisibleList.value
// ..removeAt(index)
// ..insert(index, true)
// ];
if (uploadDeleteController
.isLoading) {
return;
}
currentLoadingIndex.value = index;

if (!uploadDeleteController
.isLoading) {
await ref
Expand All @@ -266,11 +269,6 @@ class _HomeViewState extends ConsumerState<HomeView> {
.notifier,
)
.upload(e, none());
// progressVisibleList.value = [
// ...progressVisibleList.value
// ..removeAt(index)
// ..insert(index, false)
// ];

if (context.mounted) {
context.showSuccessSnackBar(
Expand Down Expand Up @@ -310,14 +308,14 @@ class _HomeViewState extends ConsumerState<HomeView> {
content: 'Deleted folder',
action: none(),
);
progressVisibleList.value =
[
...progressVisibleList
.value
..removeAt(
folderInfo.indexOf(e),
)
];
// progressVisibleList.value =
// [
// ...progressVisibleList
// .value
// ..removeAt(
// folderInfo.indexOf(e),
// )
// ];
},
);
}
Expand Down

0 comments on commit 7fbe1d8

Please sign in to comment.