|
| 1 | +import 'package:collection/collection.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_hooks/flutter_hooks.dart'; |
| 4 | +import 'package:flutter_zustand/flutter_zustand.dart'; |
| 5 | +import 'package:iris/models/storages/local.dart'; |
| 6 | +import 'package:iris/models/storages/storage.dart'; |
| 7 | +import 'package:iris/models/store/storage_state.dart'; |
| 8 | +import 'package:iris/store/use_storage_store.dart'; |
| 9 | +import 'package:iris/utils/get_localizations.dart'; |
| 10 | +import 'package:path/path.dart' as p; |
| 11 | + |
| 12 | +class Favorites extends HookWidget { |
| 13 | + const Favorites({super.key}); |
| 14 | + |
| 15 | + @override |
| 16 | + Widget build(BuildContext context) { |
| 17 | + final t = getLocalizations(context); |
| 18 | + |
| 19 | + final favorites = |
| 20 | + useStorageStore().select(context, (state) => state.favorites); |
| 21 | + |
| 22 | + final localStoragesFuture = |
| 23 | + useMemoized(() async => await getLocalStorages(context), []); |
| 24 | + final localStorages = useFuture(localStoragesFuture).data ?? []; |
| 25 | + |
| 26 | + return favorites.isEmpty |
| 27 | + ? const SizedBox() |
| 28 | + : Column( |
| 29 | + children: [ |
| 30 | + Container( |
| 31 | + alignment: Alignment.centerLeft, |
| 32 | + padding: const EdgeInsets.symmetric(horizontal: 16), |
| 33 | + child: Text( |
| 34 | + t.favorites, |
| 35 | + style: TextStyle(fontSize: 20), |
| 36 | + ), |
| 37 | + ), |
| 38 | + GridView.builder( |
| 39 | + gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent( |
| 40 | + maxCrossAxisExtent: 200, |
| 41 | + mainAxisExtent: 140, |
| 42 | + mainAxisSpacing: 8, |
| 43 | + crossAxisSpacing: 8, |
| 44 | + ), |
| 45 | + shrinkWrap: true, |
| 46 | + padding: const EdgeInsets.all(16), |
| 47 | + itemCount: favorites.length, |
| 48 | + itemBuilder: (context, index) { |
| 49 | + final favorite = favorites[index]; |
| 50 | + return _FavoriteCard( |
| 51 | + favorite: favorite, |
| 52 | + localStorages: localStorages, |
| 53 | + ); |
| 54 | + }, |
| 55 | + ), |
| 56 | + ], |
| 57 | + ); |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class _FavoriteCard extends HookWidget { |
| 62 | + final Favorite favorite; |
| 63 | + final List<LocalStorage> localStorages; |
| 64 | + |
| 65 | + const _FavoriteCard({ |
| 66 | + required this.favorite, |
| 67 | + required this.localStorages, |
| 68 | + }); |
| 69 | + |
| 70 | + String _getSubtitle(BuildContext context) { |
| 71 | + Storage? storage = useStorageStore().findById(favorite.storageId); |
| 72 | + if (storage == null && favorite.storageId == localStorageId) { |
| 73 | + storage = localStorages.firstWhereOrNull( |
| 74 | + (element) => element.basePath[0] == favorite.path[0]); |
| 75 | + } |
| 76 | + if (storage == null) return ''; |
| 77 | + |
| 78 | + if (storage is LocalStorage) { |
| 79 | + final subtitle = p.normalize(favorite.path.join('/')); |
| 80 | + if (favorite.path.last == subtitle) { |
| 81 | + return ''; |
| 82 | + } |
| 83 | + return subtitle; |
| 84 | + } else if (storage is WebDAVStorage) { |
| 85 | + return 'http${storage.https ? 's' : ''}://${storage.host}${favorite.path.join('/')}'; |
| 86 | + } else if (storage is FTPStorage) { |
| 87 | + return 'ftp://${storage.username.isNotEmpty ? '${storage.username}@' : ''}${storage.host}:${storage.port}${favorite.path.join('/').replaceFirst('//', '/')}'; |
| 88 | + } else { |
| 89 | + return ''; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @override |
| 94 | + Widget build(BuildContext context) { |
| 95 | + final t = getLocalizations(context); |
| 96 | + final subtitle = _getSubtitle(context); |
| 97 | + |
| 98 | + return Card( |
| 99 | + clipBehavior: Clip.hardEdge, |
| 100 | + child: InkWell( |
| 101 | + onTap: () { |
| 102 | + Storage? storage = useStorageStore().findById(favorite.storageId); |
| 103 | + if (storage == null && favorite.storageId == localStorageId) { |
| 104 | + storage = localStorages.firstWhereOrNull( |
| 105 | + (element) => element.basePath[0] == favorite.path[0]); |
| 106 | + } |
| 107 | + if (storage == null) return; |
| 108 | + useStorageStore().updateCurrentPath(favorite.path); |
| 109 | + useStorageStore().updateCurrentStorage(storage); |
| 110 | + }, |
| 111 | + child: Padding( |
| 112 | + padding: const EdgeInsets.all(12.0), |
| 113 | + child: Column( |
| 114 | + crossAxisAlignment: CrossAxisAlignment.start, |
| 115 | + children: [ |
| 116 | + Row( |
| 117 | + mainAxisAlignment: MainAxisAlignment.spaceBetween, |
| 118 | + children: [ |
| 119 | + Icon( |
| 120 | + Icons.star_rounded, |
| 121 | + size: 32, |
| 122 | + color: Theme.of(context).colorScheme.primary, |
| 123 | + ), |
| 124 | + SizedBox( |
| 125 | + width: 40, |
| 126 | + height: 40, |
| 127 | + child: PopupMenuButton<StorageOptions>( |
| 128 | + tooltip: t.menu, |
| 129 | + icon: const Icon(Icons.more_vert_rounded), |
| 130 | + clipBehavior: Clip.hardEdge, |
| 131 | + color: |
| 132 | + Theme.of(context).colorScheme.surface.withAlpha(250), |
| 133 | + onSelected: (value) { |
| 134 | + switch (value) { |
| 135 | + case StorageOptions.remove: |
| 136 | + useStorageStore().removeFavorite(favorite); |
| 137 | + break; |
| 138 | + default: |
| 139 | + break; |
| 140 | + } |
| 141 | + }, |
| 142 | + itemBuilder: (BuildContext context) => [ |
| 143 | + PopupMenuItem( |
| 144 | + value: StorageOptions.remove, |
| 145 | + child: Text(t.remove), |
| 146 | + ), |
| 147 | + ], |
| 148 | + ), |
| 149 | + ), |
| 150 | + ], |
| 151 | + ), |
| 152 | + const Spacer(), |
| 153 | + Text( |
| 154 | + favorite.path.last, |
| 155 | + style: Theme.of(context).textTheme.titleMedium, |
| 156 | + maxLines: 1, |
| 157 | + overflow: TextOverflow.ellipsis, |
| 158 | + ), |
| 159 | + if (subtitle.isNotEmpty) |
| 160 | + Text( |
| 161 | + subtitle, |
| 162 | + style: Theme.of(context).textTheme.bodySmall, |
| 163 | + maxLines: 1, |
| 164 | + overflow: TextOverflow.ellipsis, |
| 165 | + ), |
| 166 | + ], |
| 167 | + ), |
| 168 | + ), |
| 169 | + ), |
| 170 | + ); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +enum StorageOptions { |
| 175 | + edit, |
| 176 | + remove, |
| 177 | +} |
0 commit comments