Skip to content

Commit

Permalink
Merge pull request #24 from p2panda/sighting-profile
Browse files Browse the repository at this point in the history
Basic styling and local name edit field in "Sighting Profile" screen
  • Loading branch information
sandreae committed Oct 17, 2023
2 parents 286a39c + d147c8a commit bd2e83d
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 106 deletions.
3 changes: 3 additions & 0 deletions packages/app/lib/io/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import 'package:path_provider/path_provider.dart' as provider;

/// Blobs base path.
const String BLOBS_BASE_PATH = 'http://localhost:2020/blobs';

Future<String> get applicationSupportDirectory async {
final directory = await provider.getApplicationSupportDirectory();
return directory.path;
Expand Down
2 changes: 2 additions & 0 deletions packages/app/lib/io/p2panda/node.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ Future<void> startNode() async {
// peer id will be a hashed version of the public key and it will not leak
final _keyPair = await keyPair;

// @TODO: Better logging here
print(relayAddresses);

// Start node in background thread
p2panda.startNode(
keyPair: _keyPair,
Expand Down
1 change: 1 addition & 0 deletions packages/app/lib/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"paginationListError": "Error occurred while requesting data: {errorMessage}",
"paginationListLoadMore": "Load More",
"paginationListNoResults": "No entries given yet",
"sightingScreenTitle": "Sighting",
"sightingUnspecified": "Unknown species",
"speciesScreenTitle": "Species"
}
45 changes: 40 additions & 5 deletions packages/app/lib/models/local_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,39 @@ import 'package:app/models/base.dart';
import 'package:app/models/schema_ids.dart';

class LocalName {
final String id;
final String name;
final DocumentId id;
DocumentViewId viewId;

LocalName({required this.id, required this.name});
String name;

LocalName({required this.id, required this.viewId, required this.name});

factory LocalName.fromJson(Map<String, dynamic> result) {
return LocalName(
id: result['meta']['documentId'] as String,
id: result['meta']['documentId'] as DocumentId,
viewId: result['meta']['viewId'] as DocumentViewId,
name: result['fields']['name'] as String);
}

static Future<LocalName> create({required String name}) async {
DocumentViewId viewId = await createLocalName(name: name);
return LocalName(
id: viewId,
viewId: viewId,
name: name,
);
}

Future<DocumentViewId> update({required String name}) async {
this.viewId = await updateLocalName(this.viewId, name: name);
this.name = name;
return this.viewId;
}

Future<DocumentViewId> delete() async {
this.viewId = await deleteLocalName(this.viewId);
return this.viewId;
}
}

String get localNameFields {
Expand Down Expand Up @@ -49,9 +72,21 @@ String searchLocalNamesQuery(String query) {
''';
}

Future<DocumentViewId> createLocalName(String name) async {
Future<DocumentViewId> createLocalName({required String name}) async {
List<(String, OperationValue)> fields = [
("name", OperationValue.string(name)),
];
return await create(SchemaIds.bee_local_name, fields);
}

Future<DocumentViewId> updateLocalName(DocumentViewId viewId,
{required String name}) async {
List<(String, OperationValue)> fields = [
("name", OperationValue.string(name)),
];
return await update(viewId, SchemaIds.bee_local_name, fields);
}

Future<DocumentViewId> deleteLocalName(DocumentViewId viewId) async {
return await delete(viewId, SchemaIds.bee_local_name);
}
122 changes: 84 additions & 38 deletions packages/app/lib/models/sightings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ import 'package:app/models/species.dart';

class Sighting {
final DocumentId id;
final DateTime datetime;
final double latitude;
final double longitude;
final List<Blob> images;
final Species? species;
final LocalName? localName;
final String comment;
DocumentViewId viewId;

DateTime datetime;
double latitude;
double longitude;
List<Blob> images;
Species? species;
LocalName? localName;
String comment;

Sighting(
{required this.id,
required this.viewId,
required this.datetime,
required this.latitude,
required this.longitude,
Expand Down Expand Up @@ -50,6 +53,7 @@ class Sighting {

return Sighting(
id: result['meta']['documentId'] as DocumentId,
viewId: result['meta']['viewId'] as DocumentViewId,
datetime: DateTime.parse(result['fields']['datetime'] as String),
latitude: result['fields']['latitude'] as double,
longitude: result['fields']['latitude'] as double,
Expand All @@ -58,6 +62,59 @@ class Sighting {
species: species.firstOrNull,
localName: localNames.firstOrNull);
}

Future<DocumentViewId> update(
{DateTime? datetime,
double? latitude,
double? longitude,
String? comment,
List<Species>? species,
List<LocalName>? localNames}) async {
if (datetime != null) {
this.datetime = datetime;
}

if (latitude != null) {
this.latitude = latitude;
}

if (longitude != null) {
this.longitude = longitude;
}

List<DocumentId>? speciesIds;
if (species != null && species.isEmpty) {
this.species = null;
speciesIds = [];
} else if (species != null) {
this.species = species.first;
speciesIds = [species.first.id];
}

List<DocumentId>? localNameIds;
if (localNames != null && localNames.isEmpty) {
this.localName = null;
localNameIds = [];
} else if (localNames != null) {
this.localName = localNames.first;
localNameIds = [localNames.first.id];
}

this.viewId = await updateSighting(this.viewId,
datetime: datetime,
latitude: latitude,
longitude: longitude,
comment: comment,
speciesIds: speciesIds,
localNameIds: localNameIds);

return this.viewId;
}

Future<DocumentViewId> delete() async {
this.viewId = await deleteSighting(this.viewId);
return this.viewId;
}
}

class SightingPaginator extends Paginator<Sighting> {
Expand Down Expand Up @@ -158,45 +215,34 @@ String lastSightingQuery(DocumentId speciesId) {
}

Future<DocumentViewId> createSighting(
DateTime datetime,
double latitude,
double longitude,
String comment,
List<DocumentId> imageIds,
DocumentId? speciesId,
DocumentId? localNameId) async {
{required DateTime datetime,
double latitude = 0.0,
double longitude = 0.0,
String comment = '',
List<DocumentId> imageIds = const [],
List<DocumentId> speciesIds = const [],
List<DocumentId> localNameIds = const []}) async {
List<(String, OperationValue)> fields = [
("datetime", OperationValue.string(datetime.toString())),
("datetime", OperationValue.string(datetime.toIso8601String())),
("latitude", OperationValue.float(latitude)),
("longitude", OperationValue.float(longitude)),
("images", OperationValue.relationList(imageIds)),
("species", OperationValue.relationList(speciesIds)),
("local_names", OperationValue.relationList(localNameIds)),
("comment", OperationValue.string(comment))
];

if (speciesId != null) {
fields.add(("species", OperationValue.relationList([speciesId])));
} else {
fields.add(("species", OperationValue.relationList([])));
}

if (localNameId != null) {
fields.add(("local_names", OperationValue.relationList([localNameId])));
} else {
fields.add(("local_names", OperationValue.relationList([])));
}

return await create(SchemaIds.bee_sighting, fields);
}

Future<DocumentViewId> updateSighting(
DocumentViewId viewId,
DateTime? datetime,
Future<DocumentViewId> updateSighting(DocumentViewId viewId,
{DateTime? datetime,
double? latitude,
double? longitude,
String? comment,
List<DocumentId>? imageIds,
DocumentId? speciesId,
DocumentId? localNameId) async {
List<DocumentId>? speciesIds,
List<DocumentId>? localNameIds}) async {
List<(String, OperationValue)> fields = [];

if (datetime != null) {
Expand All @@ -215,12 +261,12 @@ Future<DocumentViewId> updateSighting(
fields.add(("images", OperationValue.relationList(imageIds)));
}

if (speciesId != null) {
fields.add(("species", OperationValue.relationList([speciesId])));
if (speciesIds != null) {
fields.add(("species", OperationValue.relationList(speciesIds)));
}

if (localNameId != null) {
fields.add(("local_names", OperationValue.relationList([localNameId])));
if (localNameIds != null) {
fields.add(("local_names", OperationValue.relationList(localNameIds)));
}

if (comment != null) {
Expand All @@ -230,6 +276,6 @@ Future<DocumentViewId> updateSighting(
return await update(SchemaIds.bee_sighting, viewId, fields);
}

Future<void> deleteSighting(DocumentViewId viewId) async {
await delete(SchemaIds.bee_sighting, viewId);
Future<DocumentViewId> deleteSighting(DocumentViewId viewId) async {
return await delete(SchemaIds.bee_sighting, viewId);
}
12 changes: 8 additions & 4 deletions packages/app/lib/ui/screens/create_sighting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,22 @@ class _CreateSightingScreenState extends State<CreateSightingScreen> {
}

// Check if local name already exists, otherwise create a new one
DocumentId? localNameId;
List<DocumentId> localNameIds = [];
if (localName != null) {
if (localName!.documentId != null) {
localNameId = localName!.documentId;
localNameIds = [localName!.documentId!];
} else {
localNameId = await createLocalName(localName!.value);
localNameIds = [await createLocalName(name: localName!.value)];
}
}

// Publish the sighting
await createSighting(
datetime, latitude, longitude, '', imageIds, null, localNameId);
datetime: datetime,
latitude: latitude,
longitude: longitude,
imageIds: imageIds,
localNameIds: localNameIds);

// .. wait a little bit
await sleep(500);
Expand Down
Loading

0 comments on commit bd2e83d

Please sign in to comment.