DRY up some search and places code
This commit is contained in:
@ -2,17 +2,28 @@ import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.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';
|
||||
|
||||
class Suggestion<T> {
|
||||
T type;
|
||||
|
||||
Suggestion(this.type);
|
||||
}
|
||||
|
||||
class FormSearchInput extends StatefulWidget {
|
||||
const FormSearchInput(
|
||||
{super.key, required this.sessionController, required this.service, this.requestCallback, this.optionalPayload});
|
||||
{super.key,
|
||||
required this.sessionController,
|
||||
required this.service,
|
||||
this.title,
|
||||
this.callback,
|
||||
this.optionalPayload});
|
||||
|
||||
final String? title;
|
||||
final TextEditingController sessionController;
|
||||
final dynamic service;
|
||||
final Function? requestCallback;
|
||||
final Function? callback;
|
||||
final dynamic optionalPayload;
|
||||
|
||||
@override
|
||||
@ -23,7 +34,7 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
String? _currentQuery;
|
||||
|
||||
late final service = widget.service;
|
||||
late final requestCallback = widget.requestCallback;
|
||||
late final callback = widget.callback;
|
||||
// The most recent suggestions received from the API.
|
||||
late Iterable<Widget> _lastOptions = <Widget>[];
|
||||
late final Debouncer debouncer;
|
||||
@ -68,7 +79,7 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
builder: (BuildContext context, SearchController controller) {
|
||||
return FormTextInput(
|
||||
controller: widget.sessionController,
|
||||
title: 'Location (optional)',
|
||||
title: widget.title ?? "",
|
||||
icon: Icon(Icons.search_rounded),
|
||||
maxLines: 2,
|
||||
requiresValidation: false,
|
||||
@ -84,24 +95,15 @@ class _FormSearchInputState extends State<FormSearchInput> {
|
||||
}
|
||||
_lastOptions = List<ListTile>.generate(options.length, (int index) {
|
||||
final Suggestion item = options[index];
|
||||
final dynamic content = item.type;
|
||||
return ListTile(
|
||||
title: Text(item.description),
|
||||
title: Text(content.description),
|
||||
onTap: () async {
|
||||
// widget.optionalPayload = service.fetchPhoto(json.decode(item.image));
|
||||
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'];
|
||||
});
|
||||
if (callback != null) {
|
||||
callback!(content, service);
|
||||
}
|
||||
|
||||
widget.optionalPayload.address = item.address;
|
||||
widget.sessionController.text = item.description;
|
||||
service.finish();
|
||||
controller.closeView(item.description);
|
||||
controller.closeView(null);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
@ -1,5 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:drift/drift.dart' hide Column;
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
@ -181,9 +182,27 @@ class _SessionEditorState extends State<SessionEditor> {
|
||||
});
|
||||
}),
|
||||
FormSearchInput(
|
||||
title: 'Location (optional)',
|
||||
sessionController: sessionCreateController['address']!,
|
||||
service: GooglePlacesService(),
|
||||
optionalPayload: sessionPayload),
|
||||
callback: (content, service) async {
|
||||
if (content.imageReferences != null) {
|
||||
// get a random photo item from the returned result
|
||||
Map<String, dynamic> photo = content.imageReferences![
|
||||
Random().nextInt(content.imageReferences!.length)];
|
||||
|
||||
await service
|
||||
.fetchPhoto(photo['name'])
|
||||
.then((photoMap) {
|
||||
sessionPayload.photoUri = photoMap['photoUri'];
|
||||
});
|
||||
}
|
||||
|
||||
sessionPayload.address = content.address;
|
||||
sessionCreateController['address']!.text =
|
||||
content.description;
|
||||
service.finish();
|
||||
}),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(top: 10, bottom: 10),
|
||||
child: TextFormField(
|
||||
@ -248,11 +267,15 @@ class _SessionEditorState extends State<SessionEditor> {
|
||||
i++) {
|
||||
PlatformFile file =
|
||||
sessionPayload.files![i];
|
||||
String? type = lookupMimeType(file.path!)!.split('/').first;
|
||||
String? type =
|
||||
lookupMimeType(file.path!)!
|
||||
.split('/')
|
||||
.first;
|
||||
Uint8List fileBytes =
|
||||
await file.xFile.readAsBytes();
|
||||
|
||||
MediaType mediaType = MediaType.localImage;
|
||||
MediaType mediaType =
|
||||
MediaType.localImage;
|
||||
if (type == "video") {
|
||||
mediaType = MediaType.localVideo;
|
||||
}
|
||||
@ -282,8 +305,7 @@ class _SessionEditorState extends State<SessionEditor> {
|
||||
_formKey.currentContext!, 'Submit');
|
||||
|
||||
if (widget.callback != null) {
|
||||
await widget
|
||||
.callback!();
|
||||
await widget.callback!();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user