This commit is contained in:
Mehul Ahal 2022-12-20 20:29:46 +01:00
parent 193ae3b0ea
commit 3e374d24f6
13 changed files with 126 additions and 3 deletions

View file

@ -1,6 +1,6 @@
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:mc_gallery/features/home/views/gallery_view.dart';
import 'package:mc_gallery/features/home/views/gallery/gallery_view.dart';
import '../../views/error_page_view.dart';
import 'routes.dart';

View file

@ -0,0 +1,9 @@
abstract class ConstValues {
static const String httpsScheme = 'https';
static const String backendHost = 'source.unsplash.com';
static const List<String> backendUrlPathSegments = ['user', 'c_v_r'];
static const int numberOfImages = 20;
static const int minImageSize = 100;
static const int maxImageSize = 250;
}

View file

@ -0,0 +1,11 @@
import 'dart:async';
import '../data/models/image_model.dart';
/// Interface for implementing image-fetching strategies, specific to a resource location on the internet.
///
/// Since I used a site that was more obscure than the ones in the examples, this (otherwise pointless
/// and convoluting) interface is for adding a bit of flexibility to change strategy to some other site.
abstract class ImagesApi {
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token});
}

View file

@ -0,0 +1,34 @@
import 'dart:async';
import 'dart:math';
import '/features/core/data/constants/const_values.dart';
import '/l10n/generated/l10n.dart';
import '../abstracts/images_api.dart';
import '../data/models/image_model.dart';
class UnsplashImagesApi implements ImagesApi {
@override
FutureOr<Iterable<ImageModel>> fetchImageUri({required String token}) {
final random = Random();
return Iterable<int>.generate(ConstValues.numberOfImages).map((final imageIndex) {
// Drawing from a normal distribution
final imageSide = ConstValues.minImageSize +
random.nextInt((ConstValues.maxImageSize + 1) - ConstValues.minImageSize);
final imageUri = _imageUrlGenerator(imageSide: imageSide);
return ImageModel<int>(
comparableIndex: imageIndex,
uri: imageUri,
imageName: Strings.current.image,
);
});
}
Uri _imageUrlGenerator({required int imageSide}) => Uri(
scheme: ConstValues.httpsScheme,
host: ConstValues.backendHost,
pathSegments: ConstValues.backendUrlPathSegments..add('${imageSide}x$imageSide'),
);
}

View file

@ -0,0 +1,18 @@
class ImageModel<T extends Comparable> {
const ImageModel({
required this.uri,
required this.comparableIndex,
required this.imageName,
});
/// An image's target [Uri].
///
/// Storing an image's [ByteData] is more expensive, memory-wise.
final Uri uri;
/// A unique identifier that can be used for indexing the image.
final T comparableIndex;
/// Given name of the image.
final String imageName;
}

View file

@ -0,0 +1,26 @@
import 'package:mc_gallery/features/home/data/models/image_model.dart';
import 'package:mc_gallery/locator.dart';
import '../abstracts/images_api.dart';
/// Handles fetching and storing of Images.
///
/// 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].
class ImagesService {
ImagesService({required ImagesApi imagesApi}) : _imagesApi = imagesApi {
_init();
}
final ImagesApi _imagesApi;
late final Iterable<ImageModel> _imageModels;
Future<void> _init() async {
_imageModels = await _imagesApi.fetchImageUri(token: '');
Locator.instance().signalReady(this);
}
static ImagesService get locate => Locator.locate();
}

View file

@ -22,6 +22,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"image": MessageLookupByLibrary.simpleMessage("Image"),
"somethingWentWrong":
MessageLookupByLibrary.simpleMessage("Something went wrong")
};

View file

@ -59,6 +59,16 @@ class Strings {
args: [],
);
}
/// `Image`
String get image {
return Intl.message(
'Image',
name: 'image',
desc: '',
args: [],
);
}
}
class AppLocalizationDelegate extends LocalizationsDelegate<Strings> {

View file

@ -1,4 +1,6 @@
{
"@@locale": "en",
"somethingWentWrong": "Something went wrong"
"somethingWentWrong": "Something went wrong",
"image": "Image"
}

View file

@ -4,6 +4,8 @@ import 'package:internet_connection_checker/internet_connection_checker.dart';
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/navigation_service.dart';
import 'package:mc_gallery/features/home/api/unsplash_images_api.dart';
import 'package:mc_gallery/features/home/services/images_service.dart';
import 'features/core/services/connection_service.dart';
import 'features/core/services/overlay_service.dart';
@ -26,7 +28,11 @@ class Locator {
_registerSingletons();
}
static void _registerAPIs() {}
static void _registerAPIs() {
instance().registerFactory(
() => UnsplashImagesApi(),
);
}
static void _registerViewModels() {}
@ -51,6 +57,12 @@ class Locator {
),
dispose: (param) => param.dispose(),
);
it.registerSingleton(
ImagesService(
imagesApi: UnsplashImagesApi(),
),
signalsReady: true,
);
}
static _registerRepos(GetIt locator) {}