Compare commits
1 commit
678690ed1d
...
49c97192c2
Author | SHA1 | Date | |
---|---|---|---|
|
49c97192c2 |
7 changed files with 60 additions and 84 deletions
|
@ -15,7 +15,7 @@ class McgRouter {
|
||||||
key: state.pageKey,
|
key: state.pageKey,
|
||||||
child: ErrorPageView(error: state.error),
|
child: ErrorPageView(error: state.error),
|
||||||
),
|
),
|
||||||
//todo(mehul): Add Redirect
|
// TODO Add Redirect
|
||||||
routes: [
|
routes: [
|
||||||
GoRoute(
|
GoRoute(
|
||||||
path: Routes.home.routePath,
|
path: Routes.home.routePath,
|
||||||
|
|
|
@ -1,37 +1,29 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
|
||||||
import 'package:mc_gallery/locator.dart';
|
|
||||||
|
|
||||||
import '/features/core/data/constants/const_values.dart';
|
import '/features/core/data/constants/const_values.dart';
|
||||||
import '/l10n/generated/l10n.dart';
|
import '/l10n/generated/l10n.dart';
|
||||||
import '../abstracts/images_api.dart';
|
import '../abstracts/images_api.dart';
|
||||||
import '../data/models/image_model.dart';
|
import '../data/models/image_model.dart';
|
||||||
|
|
||||||
class UnsplashImagesApi with LoggingService implements ImagesApi {
|
class UnsplashImagesApi implements ImagesApi {
|
||||||
@override
|
@override
|
||||||
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token}) {
|
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token}) {
|
||||||
final random = Random();
|
final random = Random();
|
||||||
|
|
||||||
try {
|
return Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
|
||||||
return Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
|
// Drawing from a normal distribution
|
||||||
// Drawing from a normal distribution
|
final imageSide = ConstValues.minImageSize +
|
||||||
final imageSide = ConstValues.minImageSize +
|
random.nextInt((ConstValues.maxImageSize + 1) - ConstValues.minImageSize);
|
||||||
random.nextInt((ConstValues.maxImageSize + 1) - ConstValues.minImageSize);
|
|
||||||
|
|
||||||
final imageUri = _imageUrlGenerator(imageSide: imageSide);
|
final imageUri = _imageUrlGenerator(imageSide: imageSide);
|
||||||
|
|
||||||
return ImageModel(
|
return ImageModel(
|
||||||
imageIndex: imageIndex,
|
imageIndex: imageIndex,
|
||||||
uri: imageUri,
|
uri: imageUri,
|
||||||
imageName: Strings.current.image,
|
imageName: Strings.current.image,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
} on Exception catch (ex, stackTrace) {
|
|
||||||
handleException(ex, stackTrace);
|
|
||||||
return const Iterable.empty();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Uri _imageUrlGenerator({required int imageSide}) => Uri(
|
Uri _imageUrlGenerator({required int imageSide}) => Uri(
|
||||||
|
@ -39,6 +31,4 @@ class UnsplashImagesApi with LoggingService implements ImagesApi {
|
||||||
host: ConstValues.backendHost,
|
host: ConstValues.backendHost,
|
||||||
pathSegments: [...ConstValues.backendUrlPathSegments, '${imageSide}x$imageSide'],
|
pathSegments: [...ConstValues.backendUrlPathSegments, '${imageSide}x$imageSide'],
|
||||||
);
|
);
|
||||||
|
|
||||||
static UnsplashImagesApi get locate => Locator.locate();
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
import 'dart:ui';
|
|
||||||
|
|
||||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
||||||
import 'package:mc_gallery/features/core/services/app_lifecycle_service.dart';
|
|
||||||
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
|
||||||
import 'package:mc_gallery/locator.dart';
|
|
||||||
|
|
||||||
class ImageCacheManagerService with LoggingService {
|
|
||||||
ImageCacheManagerService({
|
|
||||||
required AppLifecycleService appLifecycleService,
|
|
||||||
}) : _appLifecycleService = appLifecycleService {
|
|
||||||
_init();
|
|
||||||
}
|
|
||||||
|
|
||||||
final AppLifecycleService _appLifecycleService;
|
|
||||||
|
|
||||||
Future<void> emptyCache() async => await DefaultCacheManager().emptyCache();
|
|
||||||
|
|
||||||
Future<void> _init() 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:
|
|
||||||
info('Discarding cached images');
|
|
||||||
await DefaultCacheManager().emptyCache();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
static ImageCacheManagerService get locate => Locator.locate();
|
|
||||||
}
|
|
|
@ -1,4 +1,3 @@
|
||||||
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
|
||||||
import 'package:mc_gallery/features/home/data/models/image_model.dart';
|
import 'package:mc_gallery/features/home/data/models/image_model.dart';
|
||||||
import 'package:mc_gallery/locator.dart';
|
import 'package:mc_gallery/locator.dart';
|
||||||
|
|
||||||
|
@ -9,28 +8,18 @@ import '../abstracts/images_api.dart';
|
||||||
/// Since this is very simple use-case, this is the only interface. For complex (actual CRUD-based) I/O,
|
/// Since this is very simple use-case, this is the only interface. For complex (actual CRUD-based) I/O,
|
||||||
/// an additional Repository layer interface can be used between [ImagesService] and [ImagesApi].
|
/// an additional Repository layer interface can be used between [ImagesService] and [ImagesApi].
|
||||||
class ImagesService {
|
class ImagesService {
|
||||||
ImagesService({
|
ImagesService({required ImagesApi imagesApi}) : _imagesApi = imagesApi {
|
||||||
required ImagesApi imagesApi,
|
|
||||||
required LoggingService loggingService,
|
|
||||||
}) : _imagesApi = imagesApi,
|
|
||||||
_loggingService = loggingService {
|
|
||||||
_init();
|
_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
final ImagesApi _imagesApi;
|
final ImagesApi _imagesApi;
|
||||||
final LoggingService _loggingService;
|
|
||||||
|
|
||||||
late final Iterable<ImageModel> _imageModels;
|
late final Iterable<ImageModel> _imageModels;
|
||||||
Iterable<ImageModel> get imageModels => _imageModels;
|
Iterable<ImageModel> get imageModels => _imageModels;
|
||||||
|
|
||||||
Future<void> _init() async {
|
Future<void> _init() async {
|
||||||
_loggingService.info('Fetching and creating image models...');
|
|
||||||
_imageModels = await _imagesApi.fetchImageUri(token: '');
|
_imageModels = await _imagesApi.fetchImageUri(token: '');
|
||||||
|
|
||||||
_imageModels.isNotEmpty
|
|
||||||
? _loggingService.good("Created ${_imageModels.length} images' models")
|
|
||||||
: _loggingService.warning('No images found');
|
|
||||||
|
|
||||||
Locator.instance().signalReady(this);
|
Locator.instance().signalReady(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||||
import 'package:mc_gallery/features/home/services/image_cache_manager_service.dart';
|
import 'package:mc_gallery/features/core/services/app_lifecycle_service.dart';
|
||||||
|
|
||||||
import '/features/core/abstracts/base_view_model.dart';
|
import '/features/core/abstracts/base_view_model.dart';
|
||||||
import '/features/core/services/logging_service.dart';
|
import '/features/core/services/logging_service.dart';
|
||||||
|
@ -15,16 +15,16 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
GalleryViewModel({
|
GalleryViewModel({
|
||||||
required ImagesService imagesService,
|
required ImagesService imagesService,
|
||||||
required NavigationService navigationService,
|
required NavigationService navigationService,
|
||||||
required ImageCacheManagerService imageCacheManagerService,
|
required AppLifecycleService appLifecycleService,
|
||||||
required LoggingService loggingService,
|
required LoggingService loggingService,
|
||||||
}) : _imagesService = imagesService,
|
}) : _imagesService = imagesService,
|
||||||
_navigationService = navigationService,
|
_navigationService = navigationService,
|
||||||
_imageCacheManagerService = imageCacheManagerService,
|
_appLifecycleService = appLifecycleService,
|
||||||
_loggingService = loggingService;
|
_loggingService = loggingService;
|
||||||
|
|
||||||
final ImagesService _imagesService;
|
final ImagesService _imagesService;
|
||||||
final NavigationService _navigationService;
|
final NavigationService _navigationService;
|
||||||
final ImageCacheManagerService _imageCacheManagerService;
|
final AppLifecycleService _appLifecycleService;
|
||||||
final LoggingService _loggingService;
|
final LoggingService _loggingService;
|
||||||
|
|
||||||
final ValueNotifier<bool> _isDisplayingPressingPrompt = ValueNotifier(true);
|
final ValueNotifier<bool> _isDisplayingPressingPrompt = ValueNotifier(true);
|
||||||
|
@ -32,11 +32,27 @@ class GalleryViewModel extends BaseViewModel {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> initialise(bool Function() mounted, [arguments]) async {
|
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);
|
super.initialise(mounted, arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> dispose() async {
|
Future<void> dispose() async {
|
||||||
|
await _appLifecycleService.removeListener(tag: runtimeType.toString());
|
||||||
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
import 'dart:ui';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_cache_manager/flutter_cache_manager.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/abstracts/base_view_model.dart';
|
||||||
import '/features/core/services/logging_service.dart';
|
import '/features/core/services/logging_service.dart';
|
||||||
|
@ -13,13 +16,16 @@ class ImageCarouselViewModel extends BaseViewModel {
|
||||||
ImageCarouselViewModel({
|
ImageCarouselViewModel({
|
||||||
required ImagesService imagesService,
|
required ImagesService imagesService,
|
||||||
required NavigationService navigationService,
|
required NavigationService navigationService,
|
||||||
|
required AppLifecycleService appLifecycleService,
|
||||||
required LoggingService loggingService,
|
required LoggingService loggingService,
|
||||||
}) : _imagesService = imagesService,
|
}) : _imagesService = imagesService,
|
||||||
_navigationService = navigationService,
|
_navigationService = navigationService,
|
||||||
|
_appLifecycleService = appLifecycleService,
|
||||||
_loggingService = loggingService;
|
_loggingService = loggingService;
|
||||||
|
|
||||||
final ImagesService _imagesService;
|
final ImagesService _imagesService;
|
||||||
final NavigationService _navigationService;
|
final NavigationService _navigationService;
|
||||||
|
final AppLifecycleService _appLifecycleService;
|
||||||
final LoggingService _loggingService;
|
final LoggingService _loggingService;
|
||||||
|
|
||||||
late final ValueNotifier<ImageModel> _currentImageModel;
|
late final ValueNotifier<ImageModel> _currentImageModel;
|
||||||
|
@ -27,6 +33,19 @@ class ImageCarouselViewModel extends BaseViewModel {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> initialise(bool Function() mounted, [arguments]) async {
|
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
|
_currentImageModel = ValueNotifier(_imagesService.imageModels
|
||||||
.elementAt((arguments! as ImageCarouselViewArguments).imageIndexKey));
|
.elementAt((arguments! as ImageCarouselViewArguments).imageIndexKey));
|
||||||
|
|
||||||
|
@ -35,6 +54,8 @@ class ImageCarouselViewModel extends BaseViewModel {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> dispose() async {
|
Future<void> dispose() async {
|
||||||
|
await _appLifecycleService.removeListener(tag: runtimeType.toString());
|
||||||
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import 'package:mc_gallery/features/core/abstracts/router/app_router.dart';
|
||||||
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
import 'package:mc_gallery/features/core/services/logging_service.dart';
|
||||||
import 'package:mc_gallery/features/core/services/navigation_service.dart';
|
import 'package:mc_gallery/features/core/services/navigation_service.dart';
|
||||||
import 'package:mc_gallery/features/home/api/unsplash_images_api.dart';
|
import 'package:mc_gallery/features/home/api/unsplash_images_api.dart';
|
||||||
import 'package:mc_gallery/features/home/services/image_cache_manager_service.dart';
|
|
||||||
import 'package:mc_gallery/features/home/services/images_service.dart';
|
import 'package:mc_gallery/features/home/services/images_service.dart';
|
||||||
import 'package:mc_gallery/features/home/views/gallery/gallery_view_model.dart';
|
import 'package:mc_gallery/features/home/views/gallery/gallery_view_model.dart';
|
||||||
import 'package:mc_gallery/features/home/views/image_carousel/image_carousel_view_model.dart';
|
import 'package:mc_gallery/features/home/views/image_carousel/image_carousel_view_model.dart';
|
||||||
|
@ -45,7 +44,7 @@ class Locator {
|
||||||
() => GalleryViewModel(
|
() => GalleryViewModel(
|
||||||
imagesService: ImagesService.locate,
|
imagesService: ImagesService.locate,
|
||||||
navigationService: NavigationService.locate,
|
navigationService: NavigationService.locate,
|
||||||
imageCacheManagerService: ImageCacheManagerService.locate,
|
appLifecycleService: AppLifecycleService.locate,
|
||||||
loggingService: LoggingService.locate,
|
loggingService: LoggingService.locate,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -53,6 +52,7 @@ class Locator {
|
||||||
() => ImageCarouselViewModel(
|
() => ImageCarouselViewModel(
|
||||||
imagesService: ImagesService.locate,
|
imagesService: ImagesService.locate,
|
||||||
navigationService: NavigationService.locate,
|
navigationService: NavigationService.locate,
|
||||||
|
appLifecycleService: AppLifecycleService.locate,
|
||||||
loggingService: LoggingService.locate,
|
loggingService: LoggingService.locate,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
@ -86,13 +86,10 @@ class Locator {
|
||||||
dispose: (param) async => await param.dispose(),
|
dispose: (param) async => await param.dispose(),
|
||||||
);
|
);
|
||||||
it.registerSingleton(
|
it.registerSingleton(
|
||||||
ImagesService(imagesApi: UnsplashImagesApi.locate, loggingService: LoggingService.locate),
|
ImagesService(
|
||||||
signalsReady: true,
|
imagesApi: UnsplashImagesApi(),
|
||||||
);
|
|
||||||
it.registerSingleton(
|
|
||||||
ImageCacheManagerService(
|
|
||||||
appLifecycleService: AppLifecycleService.locate,
|
|
||||||
),
|
),
|
||||||
|
signalsReady: true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue