Skip to content

Commit

Permalink
Merge pull request #4 from schaerfo/feature/linequery_date
Browse files Browse the repository at this point in the history
Feature/linequery date
  • Loading branch information
schaerfo committed Oct 29, 2023
2 parents ab70372 + 8360e96 commit c8188e0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 25 deletions.
7 changes: 3 additions & 4 deletions lib/backend/db_transport_rest.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ class DbTransportRestBackend {
final _client = Client();

Future<List<Leg>> findLines(String query) async {
final now = DateTime.now();
Future<List<Leg>> findLines(String query, DateTime date) async {
var uri = Uri(
scheme: 'https',
host: 'v6.db.transport.rest',
path: 'trips',
queryParameters: {
'query': query,
'onlyCurrentlyRunning': false.toString(),
'fromWhen': DateTime(now.year, now.month, now.day).toIso8601String(),
'untilWhen': DateTime(now.year, now.month, now.day, 23, 59, 59)
'fromWhen': DateTime(date.year, date.month, date.day).toIso8601String(),
'untilWhen': DateTime(date.year, date.month, date.day, 23, 59, 59)
.toIso8601String(),
},
);
Expand Down
64 changes: 43 additions & 21 deletions lib/screens/linequery.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:io';

import 'package:async/async.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:journeyplanner_fl/backend/db_transport_rest.dart';

import '../data/leg.dart';
Expand Down Expand Up @@ -37,41 +38,41 @@ class _LineQueryState extends State<_LineQuery> {

final _backend = DbTransportRestBackend();
var _query = "";
DateTime _date = DateTime.now().copyWith(hour: 0, minute: 0);
late RestartableTimer _timer;

_LineQueryState() {
_timer = RestartableTimer(const Duration(milliseconds: 500), () {
if (_query.isNotEmpty) {
_queryLines(_query);
}
_queryLines();
});
}

void _queryLines(String query) async {
void _queryLines() async {
if (_query.isEmpty) {
return;
}
setState(() {
_lines.clear();
_runningQuery = CancelableOperation.fromFuture(_backend.findLines(query));
_runningQuery =
CancelableOperation.fromFuture(_backend.findLines(_query, _date));
});
try {
_runningQuery!.then((value) {
setState(() {
_lines = value;
_emptyResult = _lines.isEmpty;
_runningQuery = null;
});
}, onError: (error, _) {
setState(() {
_runningQuery = null;
});
throw error;
_runningQuery!.then((value) {
setState(() {
_lines = value;
_emptyResult = _lines.isEmpty;
_runningQuery = null;
});
} on HttpException catch (e) {
print(e.message);
}, onError: (error, _) {
setState(() {
_runningQuery = null;
_lines.clear();
});
return;
}
if (error is HttpException) {
print(error.message);
} else {
throw error;
}
});
}

void _abortQuery() {
Expand Down Expand Up @@ -102,6 +103,27 @@ class _LineQueryState extends State<_LineQuery> {
),
),
),
ListTile(
title: Text(
DateFormat.yMEd().format(_date),
),
onTap: () async {
final result = await showDatePicker(
context: context,
initialDate: _date,
firstDate: DateTime.parse('2020-01-01'),
lastDate: DateTime.parse('2029-12-31'),
);
if (result == null) {
return;
}
setState(() {
_date = result;
});
_abortQuery();
_queryLines();
},
),
const SizedBox(
height: 15,
),
Expand Down

0 comments on commit c8188e0

Please sign in to comment.