location image search and population for new session
This commit is contained in:
parent
afe633e697
commit
5f628d6b48
@ -4,15 +4,21 @@ import 'package:sendtrain/database/database.dart';
|
|||||||
part 'media_items_dao.g.dart';
|
part 'media_items_dao.g.dart';
|
||||||
|
|
||||||
@DriftAccessor(tables: [MediaItems])
|
@DriftAccessor(tables: [MediaItems])
|
||||||
class MediaItemsDao extends DatabaseAccessor<AppDatabase> with _$MediaItemsDaoMixin {
|
class MediaItemsDao extends DatabaseAccessor<AppDatabase>
|
||||||
|
with _$MediaItemsDaoMixin {
|
||||||
MediaItemsDao(super.db);
|
MediaItemsDao(super.db);
|
||||||
|
|
||||||
|
Future createOrUpdate(MediaItemsCompanion mediaItem) =>
|
||||||
|
into(mediaItems).insertOnConflictUpdate(mediaItem);
|
||||||
|
|
||||||
Future<List<MediaItem>> all() async {
|
Future<List<MediaItem>> all() async {
|
||||||
return await select(mediaItems).get();
|
return await select(mediaItems).get();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<MediaItem> find(int id) async {
|
Future<MediaItem> find(int id) async {
|
||||||
return await (select(mediaItems)..where((mediaItem) => mediaItem.id.equals(id) )).getSingle();
|
return await (select(mediaItems)
|
||||||
|
..where((mediaItem) => mediaItem.id.equals(id)))
|
||||||
|
.getSingle();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<MediaItem>> fromActivity(Activity activity) async {
|
Future<List<MediaItem>> fromActivity(Activity activity) async {
|
||||||
@ -24,13 +30,11 @@ class MediaItemsDao extends DatabaseAccessor<AppDatabase> with _$MediaItemsDaoMi
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
..where(
|
..where(db.objectMediaItems.objectType.equals(ObjectType.activities.name))
|
||||||
db.objectMediaItems.objectType.equals(ObjectType.activities.name))
|
|
||||||
..where(db.objectMediaItems.objectId.equals(activity.id));
|
..where(db.objectMediaItems.objectId.equals(activity.id));
|
||||||
|
|
||||||
final mediaItems = (await result.get())
|
final mediaItems =
|
||||||
.map((e) => e.readTable(db.mediaItems))
|
(await result.get()).map((e) => e.readTable(db.mediaItems)).toList();
|
||||||
.toList();
|
|
||||||
|
|
||||||
return mediaItems;
|
return mediaItems;
|
||||||
}
|
}
|
||||||
@ -44,14 +48,31 @@ class MediaItemsDao extends DatabaseAccessor<AppDatabase> with _$MediaItemsDaoMi
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
..where(
|
..where(db.objectMediaItems.objectType.equals(ObjectType.sessions.name))
|
||||||
db.objectMediaItems.objectType.equals(ObjectType.sessions.name))
|
|
||||||
..where(db.objectMediaItems.objectId.equals(session.id));
|
..where(db.objectMediaItems.objectId.equals(session.id));
|
||||||
|
|
||||||
final mediaItems = (await result.get())
|
final mediaItems =
|
||||||
.map((e) => e.readTable(db.mediaItems))
|
(await result.get()).map((e) => e.readTable(db.mediaItems)).toList();
|
||||||
.toList();
|
|
||||||
|
|
||||||
return mediaItems;
|
return mediaItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Stream<List<MediaItem>> watchSessionMediaItems(int id) {
|
||||||
|
final query = select(db.objectMediaItems).join(
|
||||||
|
[
|
||||||
|
innerJoin(
|
||||||
|
db.mediaItems,
|
||||||
|
db.mediaItems.id.equalsExp(db.objectMediaItems.mediaId),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
..where(db.objectMediaItems.objectType.equals(ObjectType.sessions.name))
|
||||||
|
..where(db.objectMediaItems.objectId.equals(id));
|
||||||
|
|
||||||
|
return query.watch().map((rows) {
|
||||||
|
final mediaItems = (rows).map((e) => e.readTable(db.mediaItems)).toList();
|
||||||
|
|
||||||
|
return mediaItems;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
19
lib/daos/object_media_items_dao.dart
Normal file
19
lib/daos/object_media_items_dao.dart
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import 'package:drift/drift.dart';
|
||||||
|
import 'package:sendtrain/database/database.dart';
|
||||||
|
|
||||||
|
part 'object_media_items_dao.g.dart';
|
||||||
|
|
||||||
|
@DriftAccessor(tables: [ObjectMediaItems])
|
||||||
|
class ObjectMediaItemsDao extends DatabaseAccessor<AppDatabase> with _$ObjectMediaItemsDaoMixin {
|
||||||
|
ObjectMediaItemsDao(super.db);
|
||||||
|
|
||||||
|
Future createOrUpdate(ObjectMediaItemsCompanion objectMediaItem) => into(objectMediaItems).insertOnConflictUpdate(objectMediaItem);
|
||||||
|
|
||||||
|
Future<List<ObjectMediaItem>> all() async {
|
||||||
|
return await select(objectMediaItems).get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ObjectMediaItem> find(int id) async {
|
||||||
|
return await (select(objectMediaItems)..where((objectMediaItem) => objectMediaItem.id.equals(id) )).getSingle();
|
||||||
|
}
|
||||||
|
}
|
10
lib/daos/object_media_items_dao.g.dart
Normal file
10
lib/daos/object_media_items_dao.g.dart
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'object_media_items_dao.dart';
|
||||||
|
|
||||||
|
// ignore_for_file: type=lint
|
||||||
|
mixin _$ObjectMediaItemsDaoMixin on DatabaseAccessor<AppDatabase> {
|
||||||
|
$MediaItemsTable get mediaItems => attachedDatabase.mediaItems;
|
||||||
|
$ObjectMediaItemsTable get objectMediaItems =>
|
||||||
|
attachedDatabase.objectMediaItems;
|
||||||
|
}
|
@ -4,6 +4,7 @@ import 'package:sendtrain/daos/actions_dao.dart';
|
|||||||
import 'package:sendtrain/daos/activities_dao.dart';
|
import 'package:sendtrain/daos/activities_dao.dart';
|
||||||
import 'package:sendtrain/daos/activity_actions_dao.dart';
|
import 'package:sendtrain/daos/activity_actions_dao.dart';
|
||||||
import 'package:sendtrain/daos/media_items_dao.dart';
|
import 'package:sendtrain/daos/media_items_dao.dart';
|
||||||
|
import 'package:sendtrain/daos/object_media_items_dao.dart';
|
||||||
import 'package:sendtrain/daos/session_activities_dao.dart';
|
import 'package:sendtrain/daos/session_activities_dao.dart';
|
||||||
import 'package:sendtrain/daos/sessions_dao.dart';
|
import 'package:sendtrain/daos/sessions_dao.dart';
|
||||||
import 'package:sendtrain/database/seed.dart';
|
import 'package:sendtrain/database/seed.dart';
|
||||||
@ -22,6 +23,7 @@ part 'database.g.dart';
|
|||||||
SessionsDao,
|
SessionsDao,
|
||||||
ActivitiesDao,
|
ActivitiesDao,
|
||||||
MediaItemsDao,
|
MediaItemsDao,
|
||||||
|
ObjectMediaItemsDao,
|
||||||
SessionActivitiesDao,
|
SessionActivitiesDao,
|
||||||
ActivityActionsDao,
|
ActivityActionsDao,
|
||||||
ActionsDao
|
ActionsDao
|
||||||
|
@ -2398,14 +2398,6 @@ abstract class _$AppDatabase extends GeneratedDatabase {
|
|||||||
late final $MediaItemsTable mediaItems = $MediaItemsTable(this);
|
late final $MediaItemsTable mediaItems = $MediaItemsTable(this);
|
||||||
late final $ObjectMediaItemsTable objectMediaItems =
|
late final $ObjectMediaItemsTable objectMediaItems =
|
||||||
$ObjectMediaItemsTable(this);
|
$ObjectMediaItemsTable(this);
|
||||||
late final SessionsDao sessionsDao = SessionsDao(this as AppDatabase);
|
|
||||||
late final ActivitiesDao activitiesDao = ActivitiesDao(this as AppDatabase);
|
|
||||||
late final MediaItemsDao mediaItemsDao = MediaItemsDao(this as AppDatabase);
|
|
||||||
late final SessionActivitiesDao sessionActivitiesDao =
|
|
||||||
SessionActivitiesDao(this as AppDatabase);
|
|
||||||
late final ActivityActionsDao activityActionsDao =
|
|
||||||
ActivityActionsDao(this as AppDatabase);
|
|
||||||
late final ActionsDao actionsDao = ActionsDao(this as AppDatabase);
|
|
||||||
@override
|
@override
|
||||||
Iterable<TableInfo<Table, Object?>> get allTables =>
|
Iterable<TableInfo<Table, Object?>> get allTables =>
|
||||||
allSchemaEntities.whereType<TableInfo<Table, Object?>>();
|
allSchemaEntities.whereType<TableInfo<Table, Object?>>();
|
||||||
|
99
lib/services/apis/google_places_service.dart
Normal file
99
lib/services/apis/google_places_service.dart
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:http/http.dart';
|
||||||
|
import 'package:uuid/uuid.dart';
|
||||||
|
|
||||||
|
class GooglePlacesService {
|
||||||
|
final sessionToken = Uuid().v4();
|
||||||
|
final apiKey = "AIzaSyBCjMCEAyyNVpsnVYvZj6VL1mmB98Vd6AE";
|
||||||
|
final client = Client();
|
||||||
|
|
||||||
|
void finish() {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<Suggestion>?> fetchSuggestions(String input, String lang) async {
|
||||||
|
var headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'X-Goog-Api-Key': apiKey,
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
'X-Goog-FieldMask':
|
||||||
|
'places.displayName,places.id,places.formattedAddress,places.photos'
|
||||||
|
};
|
||||||
|
var request = Request('POST',
|
||||||
|
Uri.parse('https://places.googleapis.com/v1/places:searchText'));
|
||||||
|
request.body = json.encode({"textQuery": input});
|
||||||
|
request.headers.addAll(headers);
|
||||||
|
|
||||||
|
StreamedResponse response = await request.send();
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final result = json.decode(await response.stream.bytesToString());
|
||||||
|
|
||||||
|
if (result.isNotEmpty) {
|
||||||
|
return result['places']
|
||||||
|
.map<Suggestion>((p) => Suggestion(
|
||||||
|
placeId: p['id'],
|
||||||
|
description: p['displayName']['text'],
|
||||||
|
address: p['formattedAddress'],
|
||||||
|
imageReferences: p['photos']))
|
||||||
|
.toList();
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw Exception(response.reasonPhrase);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future fetchPhoto(String name) async {
|
||||||
|
var headers = {
|
||||||
|
"Access-Control-Allow-Origin": "*",
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = Request('GET',
|
||||||
|
Uri.parse('https://places.googleapis.com/v1/$name/media?key=$apiKey&maxWidthPx=800&skipHttpRedirect=true')
|
||||||
|
);
|
||||||
|
request.headers.addAll(headers);
|
||||||
|
|
||||||
|
StreamedResponse response = await request.send();
|
||||||
|
|
||||||
|
if (response.statusCode == 200) {
|
||||||
|
final result = json.decode(await response.stream.bytesToString());
|
||||||
|
|
||||||
|
if (result.isNotEmpty) {
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw Exception(response.reasonPhrase);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Suggestion {
|
||||||
|
final String placeId;
|
||||||
|
final String description;
|
||||||
|
final String address;
|
||||||
|
final List<dynamic>? imageReferences;
|
||||||
|
|
||||||
|
Suggestion(
|
||||||
|
{required this.placeId,
|
||||||
|
required this.description,
|
||||||
|
required this.address,
|
||||||
|
this.imageReferences});
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() {
|
||||||
|
return 'Suggestion(description: $description, placeId: $placeId)';
|
||||||
|
}
|
||||||
|
|
||||||
|
Map toJson() => {
|
||||||
|
'placeId': placeId,
|
||||||
|
'name': description,
|
||||||
|
'address': address,
|
||||||
|
'imageReferences': imageReferences
|
||||||
|
};
|
||||||
|
}
|
68
lib/services/functional/debouncer.dart
Normal file
68
lib/services/functional/debouncer.dart
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
typedef _Debounceable<S, T> = Future<S?> Function(T parameter);
|
||||||
|
|
||||||
|
class Debouncer {
|
||||||
|
Debouncer(this._duration, this._callback);
|
||||||
|
|
||||||
|
final Duration _duration;
|
||||||
|
final dynamic _callback;
|
||||||
|
late final _Debounceable<dynamic, String> _debouncedSearch = _debounce<dynamic, String>(_callback);
|
||||||
|
|
||||||
|
/// Returns a new function that is a debounced version of the given function.
|
||||||
|
///
|
||||||
|
/// This means that the original function will be called only after no calls
|
||||||
|
/// have been made for the given Duration.
|
||||||
|
_Debounceable<S, T> _debounce<S, T>(_Debounceable<S?, T> function) {
|
||||||
|
_DebounceTimer? debounceTimer;
|
||||||
|
|
||||||
|
return (T parameter) async {
|
||||||
|
if (debounceTimer != null && !debounceTimer!.isCompleted) {
|
||||||
|
debounceTimer!.cancel();
|
||||||
|
}
|
||||||
|
debounceTimer = _DebounceTimer(_duration);
|
||||||
|
try {
|
||||||
|
await debounceTimer!.future;
|
||||||
|
} on _CancelException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return function(parameter);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
process(data) {
|
||||||
|
return _debouncedSearch(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A wrapper around Timer used for debouncing.
|
||||||
|
class _DebounceTimer {
|
||||||
|
final Duration debounceDuration;
|
||||||
|
|
||||||
|
_DebounceTimer(
|
||||||
|
this.debounceDuration
|
||||||
|
) {
|
||||||
|
_timer = Timer(debounceDuration, _onComplete);
|
||||||
|
}
|
||||||
|
|
||||||
|
late final Timer _timer;
|
||||||
|
final Completer<void> _completer = Completer<void>();
|
||||||
|
|
||||||
|
void _onComplete() {
|
||||||
|
_completer.complete();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> get future => _completer.future;
|
||||||
|
|
||||||
|
bool get isCompleted => _completer.isCompleted;
|
||||||
|
|
||||||
|
void cancel() {
|
||||||
|
_timer.cancel();
|
||||||
|
_completer.completeError(const _CancelException());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// An exception indicating that the timer was canceled.
|
||||||
|
class _CancelException implements Exception {
|
||||||
|
const _CancelException();
|
||||||
|
}
|
@ -1,34 +1,17 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:http/http.dart';
|
import 'package:sendtrain/services/apis/google_places_service.dart';
|
||||||
|
import 'package:sendtrain/services/functional/debouncer.dart';
|
||||||
import 'package:sendtrain/widgets/generic/elements/form_text_input.dart';
|
import 'package:sendtrain/widgets/generic/elements/form_text_input.dart';
|
||||||
import 'package:uuid/uuid.dart';
|
|
||||||
|
|
||||||
class Suggestion {
|
|
||||||
final String placeId;
|
|
||||||
final String description;
|
|
||||||
final String address;
|
|
||||||
|
|
||||||
Suggestion(this.placeId, this.description, this.address);
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() {
|
|
||||||
return 'Suggestion(description: $description, placeId: $placeId)';
|
|
||||||
}
|
|
||||||
|
|
||||||
Map toJson() => {
|
|
||||||
'placeId': placeId,
|
|
||||||
'name': description,
|
|
||||||
'address': address,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
class FormSearchInput extends StatefulWidget {
|
class FormSearchInput extends StatefulWidget {
|
||||||
const FormSearchInput({super.key, required this.sessionController});
|
const FormSearchInput(
|
||||||
|
{super.key, required this.sessionController, this.optionalPayload});
|
||||||
|
|
||||||
final TextEditingController sessionController;
|
final TextEditingController sessionController;
|
||||||
|
final dynamic optionalPayload;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<FormSearchInput> createState() => _FormSearchInputState();
|
State<FormSearchInput> createState() => _FormSearchInputState();
|
||||||
@ -37,21 +20,22 @@ class FormSearchInput extends StatefulWidget {
|
|||||||
class _FormSearchInputState extends State<FormSearchInput> {
|
class _FormSearchInputState extends State<FormSearchInput> {
|
||||||
String? _currentQuery;
|
String? _currentQuery;
|
||||||
|
|
||||||
|
final service = GooglePlacesService();
|
||||||
|
|
||||||
// The most recent suggestions received from the API.
|
// The most recent suggestions received from the API.
|
||||||
late Iterable<Widget> _lastOptions = <Widget>[];
|
late Iterable<Widget> _lastOptions = <Widget>[];
|
||||||
|
late final Debouncer debouncer;
|
||||||
late final _Debounceable<Iterable<String>?, String> _debouncedSearch;
|
|
||||||
|
|
||||||
// Calls the "remote" API to search with the given query. Returns null when
|
// Calls the "remote" API to search with the given query. Returns null when
|
||||||
// the call has been made obsolete.
|
// the call has been made obsolete.
|
||||||
Future<Iterable<String>?> _search(String query) async {
|
Future<Iterable<Suggestion>?> _search(String query) async {
|
||||||
_currentQuery = query;
|
_currentQuery = query;
|
||||||
|
|
||||||
// In a real application, there should be some error handling here.
|
// In a real application, there should be some error handling here.
|
||||||
// final Iterable<String> options = await _FakeAPI.search(_currentQuery!);
|
// final Iterable<String> options = await _FakeAPI.search(_currentQuery!);
|
||||||
if (query.isNotEmpty) {
|
if (query.isNotEmpty) {
|
||||||
final List<Suggestion>? suggestions =
|
final List<Suggestion>? suggestions =
|
||||||
await fetchSuggestions(_currentQuery!, 'en');
|
await service.fetchSuggestions(_currentQuery!, 'en');
|
||||||
|
|
||||||
// If another search happened after this one, throw away these options.
|
// If another search happened after this one, throw away these options.
|
||||||
if (_currentQuery != query) {
|
if (_currentQuery != query) {
|
||||||
@ -59,50 +43,16 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
|||||||
}
|
}
|
||||||
_currentQuery = null;
|
_currentQuery = null;
|
||||||
|
|
||||||
return suggestions?.map((suggestion) => json.encode(suggestion));
|
return suggestions?.map((suggestion) => suggestion);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final sessionToken = Uuid().v4();
|
|
||||||
final apiKey = "AIzaSyBCjMCEAyyNVpsnVYvZj6VL1mmB98Vd6AE";
|
|
||||||
final client = Client();
|
|
||||||
|
|
||||||
Future<List<Suggestion>?> fetchSuggestions(String input, String lang) async {
|
|
||||||
var headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'X-Goog-Api-Key': apiKey,
|
|
||||||
"Access-Control-Allow-Origin": "*",
|
|
||||||
'X-Goog-FieldMask': 'places.displayName,places.id,places.formattedAddress'
|
|
||||||
};
|
|
||||||
var request = Request('POST',
|
|
||||||
Uri.parse('https://places.googleapis.com/v1/places:searchText'));
|
|
||||||
request.body = json.encode({"textQuery": input});
|
|
||||||
request.headers.addAll(headers);
|
|
||||||
|
|
||||||
StreamedResponse response = await request.send();
|
|
||||||
|
|
||||||
if (response.statusCode == 200) {
|
|
||||||
final result = json.decode(await response.stream.bytesToString());
|
|
||||||
|
|
||||||
if (result.isNotEmpty) {
|
|
||||||
return result['places']
|
|
||||||
.map<Suggestion>((p) => Suggestion(
|
|
||||||
p['id'], p['displayName']['text'], p['formattedAddress']))
|
|
||||||
.toList();
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw Exception(response.reasonPhrase);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_debouncedSearch = _debounce<Iterable<String>?, String>(_search);
|
debouncer = Debouncer(Duration(milliseconds: 50), _search);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -114,28 +64,37 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
|||||||
title: 'Location (optional)',
|
title: 'Location (optional)',
|
||||||
icon: Icon(Icons.search_rounded),
|
icon: Icon(Icons.search_rounded),
|
||||||
maxLines: 2,
|
maxLines: 2,
|
||||||
|
requiresValidation: false,
|
||||||
onTap: () {
|
onTap: () {
|
||||||
controller.openView();
|
controller.openView();
|
||||||
});
|
});
|
||||||
}, suggestionsBuilder:
|
}, suggestionsBuilder:
|
||||||
(BuildContext context, SearchController controller) async {
|
(BuildContext context, SearchController controller) async {
|
||||||
final List<String>? options =
|
final List<Suggestion>? options =
|
||||||
(await _debouncedSearch(controller.text))?.toList();
|
(await debouncer.process(controller.text))?.toList();
|
||||||
if (options == null) {
|
if (options == null) {
|
||||||
return _lastOptions;
|
return _lastOptions;
|
||||||
}
|
}
|
||||||
_lastOptions = List<ListTile>.generate(options.length, (int index) {
|
_lastOptions = List<ListTile>.generate(options.length, (int index) {
|
||||||
final String item = options[index];
|
final Suggestion item = options[index];
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text(json.decode(item)['name']),
|
title: Text(item.description),
|
||||||
onTap: () {
|
onTap: () async {
|
||||||
if (item.isNotEmpty) {
|
// widget.optionalPayload = service.fetchPhoto(json.decode(item.image));
|
||||||
widget.sessionController.text = json.decode(item)['name'];
|
if (item.imageReferences != null) {
|
||||||
|
// get a random photo item from the returned result
|
||||||
|
Map<String, dynamic> photo = item.imageReferences![
|
||||||
|
Random().nextInt(item.imageReferences!.length)];
|
||||||
|
|
||||||
|
await service.fetchPhoto(photo['name']).then((photoMap) {
|
||||||
|
widget.optionalPayload.photoUri = photoMap['photoUri'];
|
||||||
|
});
|
||||||
}
|
}
|
||||||
client.close();
|
|
||||||
controller.closeView(item);
|
widget.optionalPayload.address = item.address;
|
||||||
// debugPrint('You just selected $item');
|
widget.sessionController.text = item.description;
|
||||||
// Navigator.of(context).pop();
|
service.finish();
|
||||||
|
controller.closeView(item.description);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -144,55 +103,3 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Duration debounceDuration = Duration(milliseconds: 500);
|
|
||||||
typedef _Debounceable<S, T> = Future<S?> Function(T parameter);
|
|
||||||
|
|
||||||
/// Returns a new function that is a debounced version of the given function.
|
|
||||||
///
|
|
||||||
/// This means that the original function will be called only after no calls
|
|
||||||
/// have been made for the given Duration.
|
|
||||||
_Debounceable<S, T> _debounce<S, T>(_Debounceable<S?, T> function) {
|
|
||||||
_DebounceTimer? debounceTimer;
|
|
||||||
|
|
||||||
return (T parameter) async {
|
|
||||||
if (debounceTimer != null && !debounceTimer!.isCompleted) {
|
|
||||||
debounceTimer!.cancel();
|
|
||||||
}
|
|
||||||
debounceTimer = _DebounceTimer();
|
|
||||||
try {
|
|
||||||
await debounceTimer!.future;
|
|
||||||
} on _CancelException {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return function(parameter);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// A wrapper around Timer used for debouncing.
|
|
||||||
class _DebounceTimer {
|
|
||||||
_DebounceTimer() {
|
|
||||||
_timer = Timer(debounceDuration, _onComplete);
|
|
||||||
}
|
|
||||||
|
|
||||||
late final Timer _timer;
|
|
||||||
final Completer<void> _completer = Completer<void>();
|
|
||||||
|
|
||||||
void _onComplete() {
|
|
||||||
_completer.complete();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> get future => _completer.future;
|
|
||||||
|
|
||||||
bool get isCompleted => _completer.isCompleted;
|
|
||||||
|
|
||||||
void cancel() {
|
|
||||||
_timer.cancel();
|
|
||||||
_completer.completeError(const _CancelException());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// An exception indicating that the timer was canceled.
|
|
||||||
class _CancelException implements Exception {
|
|
||||||
const _CancelException();
|
|
||||||
}
|
|
||||||
|
@ -8,7 +8,8 @@ class FormTextInput extends StatelessWidget {
|
|||||||
this.icon,
|
this.icon,
|
||||||
this.maxLines,
|
this.maxLines,
|
||||||
this.minLines,
|
this.minLines,
|
||||||
this.onTap});
|
this.onTap,
|
||||||
|
this.requiresValidation=true});
|
||||||
|
|
||||||
final TextEditingController controller;
|
final TextEditingController controller;
|
||||||
final String title;
|
final String title;
|
||||||
@ -16,6 +17,7 @@ class FormTextInput extends StatelessWidget {
|
|||||||
final int? minLines;
|
final int? minLines;
|
||||||
final Icon? icon;
|
final Icon? icon;
|
||||||
final dynamic onTap;
|
final dynamic onTap;
|
||||||
|
final bool requiresValidation;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
@ -34,12 +36,14 @@ class FormTextInput extends StatelessWidget {
|
|||||||
labelText: title,
|
labelText: title,
|
||||||
),
|
),
|
||||||
validator: (String? value) {
|
validator: (String? value) {
|
||||||
if (value == null || value.isEmpty) {
|
if (requiresValidation == true) {
|
||||||
return 'Please enter some text';
|
if (value == null || value.isEmpty) {
|
||||||
}
|
return 'Please enter some text';
|
||||||
|
}
|
||||||
|
|
||||||
if (value.length < 3) {
|
if (value.length < 3) {
|
||||||
return 'Please enter a minimum of 3 characters';
|
return 'Please enter a minimum of 3 characters';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
|
@ -24,9 +24,9 @@ class _SessionCardState extends State<SessionCard> {
|
|||||||
|
|
||||||
initializeDateFormatting('en');
|
initializeDateFormatting('en');
|
||||||
|
|
||||||
return FutureBuilder<List<MediaItem>>(
|
return StreamBuilder<List<MediaItem>>(
|
||||||
future: MediaItemsDao(Provider.of<AppDatabase>(context))
|
stream: MediaItemsDao(Provider.of<AppDatabase>(context))
|
||||||
.fromSession(session),
|
.watchSessionMediaItems(session.id),
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.hasData) {
|
if (snapshot.hasData) {
|
||||||
List<MediaItem> mediaItems = snapshot.data!;
|
List<MediaItem> mediaItems = snapshot.data!;
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:drift/drift.dart' hide Column;
|
import 'package:drift/drift.dart' hide Column;
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
import 'package:sendtrain/daos/media_items_dao.dart';
|
||||||
|
import 'package:sendtrain/daos/object_media_items_dao.dart';
|
||||||
import 'package:sendtrain/daos/sessions_dao.dart';
|
import 'package:sendtrain/daos/sessions_dao.dart';
|
||||||
import 'package:sendtrain/database/database.dart';
|
import 'package:sendtrain/database/database.dart';
|
||||||
import 'package:sendtrain/widgets/builders/dialogs.dart';
|
import 'package:sendtrain/widgets/builders/dialogs.dart';
|
||||||
@ -21,6 +24,12 @@ class SessionCreator extends StatefulWidget {
|
|||||||
State<SessionCreator> createState() => _SessionCreatorState();
|
State<SessionCreator> createState() => _SessionCreatorState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// used to pass the result of the found image back to current context...
|
||||||
|
class SessionPayload {
|
||||||
|
String? photoUri;
|
||||||
|
String? address;
|
||||||
|
}
|
||||||
|
|
||||||
class _SessionCreatorState extends State<SessionCreator> {
|
class _SessionCreatorState extends State<SessionCreator> {
|
||||||
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
|
||||||
|
|
||||||
@ -33,14 +42,48 @@ class _SessionCreatorState extends State<SessionCreator> {
|
|||||||
'media': TextEditingController(),
|
'media': TextEditingController(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Future createSession(context) {
|
final SessionPayload sessionPayload = SessionPayload();
|
||||||
return SessionsDao(Provider.of<AppDatabase>(context, listen: false))
|
|
||||||
.createOrUpdate(SessionsCompanion(
|
Future createSession(context) async {
|
||||||
title: Value(sessionCreateController['name']!.text),
|
Map<Symbol, Value> payload = {
|
||||||
content: Value(sessionCreateController['content']!.text),
|
Symbol('title'): Value<String>(sessionCreateController['name']!.text),
|
||||||
status: Value(SessionStatus.pending),
|
Symbol('content'):
|
||||||
date: Value(DateTime.parse(sessionCreateController['date']!.text)),
|
Value<String>(sessionCreateController['content']!.text),
|
||||||
address: Value(sessionCreateController['address']!.text)));
|
Symbol('status'): Value<SessionStatus>(SessionStatus.pending),
|
||||||
|
Symbol('date'): Value<DateTime>(
|
||||||
|
DateTime.parse(sessionCreateController['date']!.text)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// optional params
|
||||||
|
if (sessionCreateController['address']!.text.isNotEmpty) {
|
||||||
|
payload[Symbol('address')] =
|
||||||
|
Value<String>(sessionCreateController['address']!.text);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await SessionsDao(Provider.of<AppDatabase>(context, listen: false))
|
||||||
|
.createOrUpdate(Function.apply(SessionsCompanion.new, [], payload));
|
||||||
|
}
|
||||||
|
|
||||||
|
Future createSessionMedia(context, sessionId) async {
|
||||||
|
if (sessionPayload.photoUri != null) {
|
||||||
|
MediaItemsCompanion mediaItem = MediaItemsCompanion(
|
||||||
|
title: Value('Location Image'),
|
||||||
|
description: Value(sessionPayload.address!),
|
||||||
|
reference: Value(sessionPayload.photoUri!),
|
||||||
|
type: Value(MediaType.image));
|
||||||
|
|
||||||
|
return await MediaItemsDao(
|
||||||
|
Provider.of<AppDatabase>(context, listen: false))
|
||||||
|
.createOrUpdate(mediaItem).then((id) async {
|
||||||
|
ObjectMediaItemsCompanion omi = ObjectMediaItemsCompanion(
|
||||||
|
objectId: Value(sessionId),
|
||||||
|
objectType: Value(ObjectType.sessions),
|
||||||
|
mediaId: Value(id),
|
||||||
|
);
|
||||||
|
|
||||||
|
await ObjectMediaItemsDao(Provider.of<AppDatabase>(context, listen: false)).createOrUpdate(omi);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@ -113,19 +156,23 @@ class _SessionCreatorState extends State<SessionCreator> {
|
|||||||
// }
|
// }
|
||||||
// })),
|
// })),
|
||||||
FormSearchInput(
|
FormSearchInput(
|
||||||
sessionController: sessionCreateController['address']!),
|
sessionController: sessionCreateController['address']!,
|
||||||
|
optionalPayload: sessionPayload),
|
||||||
Row(mainAxisAlignment: MainAxisAlignment.end, children: [
|
Row(mainAxisAlignment: MainAxisAlignment.end, children: [
|
||||||
Padding(
|
Padding(
|
||||||
padding: EdgeInsets.only(top: 10),
|
padding: EdgeInsets.only(top: 10),
|
||||||
child: FilledButton(
|
child: FilledButton(
|
||||||
onPressed: () => {
|
onPressed: () async => {
|
||||||
if (_formKey.currentState!.validate())
|
if (_formKey.currentState!.validate())
|
||||||
{
|
{
|
||||||
createSession(_formKey.currentContext)
|
await createSession(_formKey.currentContext)
|
||||||
.then((id) => {
|
.then((id) async => {
|
||||||
|
await createSessionMedia(
|
||||||
|
_formKey.currentContext, id),
|
||||||
SessionsDao(db).find(id).then(
|
SessionsDao(db).find(id).then(
|
||||||
(session) => showGenericDialog(
|
(session) => showGenericDialog(
|
||||||
SessionView(session: session),
|
SessionView(
|
||||||
|
session: session),
|
||||||
_formKey
|
_formKey
|
||||||
.currentContext!)),
|
.currentContext!)),
|
||||||
Navigator.pop(
|
Navigator.pop(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user