Fetch and persist collections on startup
This commit is contained in:
parent
c66eb60d02
commit
c252e03185
1 changed files with 32 additions and 2 deletions
|
@ -3,18 +3,26 @@ import 'dart:io';
|
|||
import 'package:dio/dio.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:photos/core/configuration.dart';
|
||||
import 'package:photos/db/collections_db.dart';
|
||||
import 'package:photos/models/collection.dart';
|
||||
|
||||
class CollectionsService {
|
||||
final _logger = Logger("CollectionsService");
|
||||
|
||||
CollectionsService._privateConstructor() {}
|
||||
CollectionsDB _db;
|
||||
|
||||
CollectionsService._privateConstructor() {
|
||||
_db = CollectionsDB.instance;
|
||||
}
|
||||
|
||||
static final CollectionsService instance =
|
||||
CollectionsService._privateConstructor();
|
||||
|
||||
Future<void> sync() async {
|
||||
// TODO
|
||||
final lastCollectionCreationTime =
|
||||
await _db.getLastCollectionCreationTime();
|
||||
final collections = await getCollections(lastCollectionCreationTime ?? 0);
|
||||
await _db.insert(collections);
|
||||
}
|
||||
|
||||
Future<Collection> getFolder(String path) async {
|
||||
|
@ -37,4 +45,26 @@ class CollectionsService {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<Collection>> getCollections(int sinceTime) {
|
||||
return Dio()
|
||||
.get(
|
||||
Configuration.instance.getHttpEndpoint() + "/collections/",
|
||||
queryParameters: {
|
||||
"sinceTime": sinceTime,
|
||||
},
|
||||
options:
|
||||
Options(headers: {"X-Auth-Token": Configuration.instance.getToken()}),
|
||||
)
|
||||
.then((response) {
|
||||
final collections = List<Collection>();
|
||||
if (response != null) {
|
||||
final c = response.data["collections"];
|
||||
for (final collection in c) {
|
||||
collections.add(Collection.fromMap(collection));
|
||||
}
|
||||
}
|
||||
return collections;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue