added image cache managing
This commit is contained in:
parent
b7045fc242
commit
2ff4d44d25
14 changed files with 188 additions and 80 deletions
|
@ -1,12 +1,12 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:mc_gallery/features/core/services/app_lifecycle_service.dart';
|
||||
|
||||
import '/features/core/abstracts/base_view_model.dart';
|
||||
import '/features/core/services/logging_service.dart';
|
||||
import '/features/core/services/navigation_service.dart';
|
||||
import '/features/home/data/models/image_model.dart';
|
||||
import '/features/home/services/image_cache_manager_service.dart';
|
||||
import '/features/home/services/images_service.dart';
|
||||
import '/features/home/views/image_carousel/image_carousel_view.dart';
|
||||
import '/locator.dart';
|
||||
|
@ -15,16 +15,16 @@ class GalleryViewModel extends BaseViewModel {
|
|||
GalleryViewModel({
|
||||
required ImagesService imagesService,
|
||||
required NavigationService navigationService,
|
||||
required AppLifecycleService appLifecycleService,
|
||||
required ImageCacheManagerService imageCacheManagerService,
|
||||
required LoggingService loggingService,
|
||||
}) : _imagesService = imagesService,
|
||||
_navigationService = navigationService,
|
||||
_appLifecycleService = appLifecycleService,
|
||||
_imageCacheManagerService = imageCacheManagerService,
|
||||
_loggingService = loggingService;
|
||||
|
||||
final ImagesService _imagesService;
|
||||
final NavigationService _navigationService;
|
||||
final AppLifecycleService _appLifecycleService;
|
||||
final ImageCacheManagerService _imageCacheManagerService;
|
||||
final LoggingService _loggingService;
|
||||
|
||||
final ValueNotifier<bool> _isDisplayingPressingPrompt = ValueNotifier(true);
|
||||
|
@ -32,27 +32,11 @@ class GalleryViewModel extends BaseViewModel {
|
|||
|
||||
@override
|
||||
Future<void> initialise(bool Function() mounted, [arguments]) async {
|
||||
_appLifecycleService.addListener(
|
||||
tag: runtimeType.toString(),
|
||||
listener: (final appLifecycleState) async {
|
||||
switch (appLifecycleState) {
|
||||
case AppLifecycleState.resumed:
|
||||
break;
|
||||
case AppLifecycleState.inactive:
|
||||
case AppLifecycleState.paused:
|
||||
case AppLifecycleState.detached:
|
||||
await DefaultCacheManager().emptyCache();
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
super.initialise(mounted, arguments);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await _appLifecycleService.removeListener(tag: runtimeType.toString());
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
import 'package:cached_network_image/cached_network_image.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_markdown/flutter_markdown.dart';
|
||||
import 'package:mc_gallery/features/core/data/constants/const_colors.dart';
|
||||
import 'package:mc_gallery/features/core/data/constants/const_text.dart';
|
||||
|
||||
import '/features/core/widgets/gap.dart';
|
||||
import '/features/core/widgets/mcg_scaffold.dart';
|
||||
import '/features/core/widgets/view_model_builder.dart';
|
||||
import '/features/home/views/image_carousel/image_carousel_view_model.dart';
|
||||
|
@ -27,15 +31,56 @@ class ImageCarouselView extends StatelessWidget {
|
|||
appBar: AppBar(
|
||||
title: Text(model.strings.imageCarousel),
|
||||
),
|
||||
body: Hero(
|
||||
tag: model.currentImageKey,
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: model.currentImageUrl,
|
||||
cacheKey: model.currentImageKey,
|
||||
progressIndicatorBuilder: (_, __, final progress) => CircularProgressIndicator(
|
||||
value: model.downloadProgressValue(progress: progress),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Card(
|
||||
elevation: 8,
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
Hero(
|
||||
tag: model.currentImageKey,
|
||||
child: CachedNetworkImage(
|
||||
imageUrl: model.currentImageUrl,
|
||||
cacheKey: model.currentImageKey,
|
||||
fit: BoxFit.fill,
|
||||
progressIndicatorBuilder: (_, __, final progress) =>
|
||||
CircularProgressIndicator(
|
||||
value: model.downloadProgressValue(progress: progress),
|
||||
),
|
||||
),
|
||||
),
|
||||
Center(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.chevron_left,
|
||||
color: model.hasPreviousImage ? ConstColours.white : ConstColours.black,
|
||||
),
|
||||
Text(
|
||||
model.currentImageName,
|
||||
style: ConstText.imageOverlayTextStyle(context),
|
||||
),
|
||||
Icon(
|
||||
Icons.chevron_right,
|
||||
color: model.hasNextImage ? ConstColours.white : ConstColours.black,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const Gap(24),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
child: MarkdownBody(data: model.strings.imageDetails),
|
||||
),
|
||||
const Gap(16),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
import 'dart:ui';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:mc_gallery/features/core/services/app_lifecycle_service.dart';
|
||||
|
||||
import '/features/core/abstracts/base_view_model.dart';
|
||||
import '/features/core/services/logging_service.dart';
|
||||
|
@ -16,16 +13,13 @@ class ImageCarouselViewModel extends BaseViewModel {
|
|||
ImageCarouselViewModel({
|
||||
required ImagesService imagesService,
|
||||
required NavigationService navigationService,
|
||||
required AppLifecycleService appLifecycleService,
|
||||
required LoggingService loggingService,
|
||||
}) : _imagesService = imagesService,
|
||||
_navigationService = navigationService,
|
||||
_appLifecycleService = appLifecycleService,
|
||||
_loggingService = loggingService;
|
||||
|
||||
final ImagesService _imagesService;
|
||||
final NavigationService _navigationService;
|
||||
final AppLifecycleService _appLifecycleService;
|
||||
final LoggingService _loggingService;
|
||||
|
||||
late final ValueNotifier<ImageModel> _currentImageModel;
|
||||
|
@ -33,19 +27,6 @@ class ImageCarouselViewModel extends BaseViewModel {
|
|||
|
||||
@override
|
||||
Future<void> initialise(bool Function() mounted, [arguments]) async {
|
||||
_appLifecycleService.addListener(
|
||||
tag: runtimeType.toString(),
|
||||
listener: (final appLifecycleState) async {
|
||||
switch (appLifecycleState) {
|
||||
case AppLifecycleState.resumed:
|
||||
break;
|
||||
case AppLifecycleState.inactive:
|
||||
case AppLifecycleState.paused:
|
||||
case AppLifecycleState.detached:
|
||||
await DefaultCacheManager().emptyCache();
|
||||
}
|
||||
});
|
||||
|
||||
_currentImageModel = ValueNotifier(_imagesService.imageModels
|
||||
.elementAt((arguments! as ImageCarouselViewArguments).imageIndexKey));
|
||||
|
||||
|
@ -54,15 +35,20 @@ class ImageCarouselViewModel extends BaseViewModel {
|
|||
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await _appLifecycleService.removeListener(tag: runtimeType.toString());
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
String get currentImageUrl => currentImageModel.value.uri.toString();
|
||||
String get currentImageKey => currentImageModel.value.imageIndex.toString();
|
||||
String get currentImageName => currentImageModel.value.imageName;
|
||||
|
||||
double? downloadProgressValue({required DownloadProgress progress}) =>
|
||||
progress.totalSize != null ? progress.downloaded / progress.totalSize! : null;
|
||||
|
||||
bool get hasPreviousImage =>
|
||||
currentImageModel.value.imageIndex > _imagesService.firstAvailableImageIndex;
|
||||
bool get hasNextImage =>
|
||||
currentImageModel.value.imageIndex < _imagesService.lastAvailableImageIndex;
|
||||
|
||||
static ImageCarouselViewModel get locate => Locator.locate();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue