2021-04-06 00:40:23 +00:00
|
|
|
import 'dart:io';
|
2021-02-21 17:31:03 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'package:imagini/core/imagini_application.dart';
|
|
|
|
import 'package:imagini/api/imagini_api_repository.dart';
|
|
|
|
import 'package:imagini/graphql/imagini_graphql.dart';
|
2021-02-22 03:36:25 +00:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2021-02-21 17:31:03 +00:00
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
import 'package:photo_manager/photo_manager.dart';
|
|
|
|
|
2021-02-21 17:31:03 +00:00
|
|
|
class HomeBloc{
|
|
|
|
|
|
|
|
final ImaginiApplication _application;
|
|
|
|
ImaginiAPIRepository _imaginiAPI;
|
|
|
|
|
|
|
|
final _loginController = StreamController<bool>.broadcast();
|
|
|
|
Stream<bool> get loginResult => _loginController.stream;
|
|
|
|
|
|
|
|
final _authenticatedController = StreamController<bool>.broadcast();
|
|
|
|
Stream<bool> get authenticatedResult => _authenticatedController.stream;
|
|
|
|
|
2021-03-01 00:23:41 +00:00
|
|
|
Map<int, List<MediaItems$Query$MediaItemResponse$MediaItem>> _pagedMediaItemListCache =
|
|
|
|
Map<int, List<MediaItems$Query$MediaItemResponse$MediaItem>>();
|
|
|
|
|
|
|
|
int totalMediaItems;
|
|
|
|
int _pageSize = 50;
|
|
|
|
|
2021-02-21 17:31:03 +00:00
|
|
|
HomeBloc(this._application){
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init(){
|
|
|
|
_imaginiAPI = _application.imaginiAPI;
|
2021-04-06 00:40:23 +00:00
|
|
|
_init_photo_manager();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _init_photo_manager() async {
|
|
|
|
var result = await PhotoManager.requestPermission();
|
|
|
|
if (!result) {
|
|
|
|
PhotoManager.openSetting();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gets list of "Albums"
|
|
|
|
List<AssetPathEntity> list = await PhotoManager.getAssetPathList();
|
|
|
|
|
|
|
|
// iOS - Select Gallery (predefined settings?)
|
|
|
|
// TODO: Filters: https://pub.dev/packages/photo_manager#filteroption
|
|
|
|
final assetList = await list[0].getAssetListPaged(0, 50);
|
|
|
|
|
|
|
|
// Example entity. This should be done through a loop
|
|
|
|
AssetEntity currEntity = assetList[0];
|
|
|
|
|
|
|
|
// File, Create, Modified, Title
|
|
|
|
File file = await currEntity.file;
|
|
|
|
DateTime createDt = currEntity.createDateTime;
|
|
|
|
DateTime modifiedDt = currEntity.modifiedDateTime;
|
|
|
|
String title = await currEntity.titleAsync;
|
2021-02-21 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dispose(){
|
|
|
|
_loginController.close();
|
|
|
|
_authenticatedController.close();
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:40:23 +00:00
|
|
|
CachedNetworkImage getMedia(int index, derivedContentWidth) {
|
2021-03-01 00:23:41 +00:00
|
|
|
MediaItems$Query$MediaItemResponse$MediaItem mediaDetails = await getMediaDetails(index);
|
|
|
|
if (mediaDetails == null)
|
|
|
|
return null;
|
|
|
|
int derivedContentHeight = (mediaDetails.height / mediaDetails.width * derivedContentWidth).ceil();
|
|
|
|
return _imaginiAPI.getImage(mediaDetails.fileName, derivedContentWidth, derivedContentHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<MediaItems$Query$MediaItemResponse$MediaItem> getMediaDetails(int index) async {
|
|
|
|
int itemPage = (index / _pageSize).ceil();
|
|
|
|
int indexOnPage = index % _pageSize;
|
|
|
|
|
|
|
|
if (!_pagedMediaItemListCache.containsKey(itemPage))
|
|
|
|
await _cachePage(itemPage);
|
|
|
|
|
|
|
|
return _pagedMediaItemListCache[itemPage][indexOnPage];
|
2021-02-21 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 00:23:41 +00:00
|
|
|
_cachePage(int itemPage) async {
|
|
|
|
MediaItems$Query newItems = await _imaginiAPI.mediaItems(Page(page: itemPage));
|
|
|
|
totalMediaItems = newItems.mediaItems.page.total;
|
|
|
|
_pagedMediaItemListCache[itemPage] = newItems.mediaItems.data;
|
2021-02-21 17:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkAuthentication(){
|
|
|
|
_authenticatedController.addStream(_imaginiAPI.isAuthenticated());
|
|
|
|
}
|
|
|
|
|
|
|
|
attemptHome(String username, password, server){
|
|
|
|
_loginController.addStream(_imaginiAPI.login(username, password, server));
|
|
|
|
}
|
|
|
|
}
|