owntracks-viewer/lib/map_page.dart

389 lines
16 KiB
Dart
Raw Normal View History

2024-03-12 20:41:04 +00:00
import 'package:bloc/bloc.dart';
import 'package:dio_cache_interceptor_hive_store/dio_cache_interceptor_hive_store.dart';
2024-03-14 19:50:30 +00:00
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
2024-03-12 20:41:04 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_cache/flutter_map_cache.dart';
import 'package:flutter_map_compass/flutter_map_compass.dart';
2024-03-14 19:50:30 +00:00
import 'package:flutter_map_math/flutter_geo_math.dart';
import 'package:get_it/get_it.dart';
2024-03-12 20:41:04 +00:00
import 'package:intl/intl.dart';
import 'package:latlong2/latlong.dart';
2024-03-14 19:50:30 +00:00
import 'package:ot_viewer_app/global_location_store.dart';
2024-03-12 20:41:04 +00:00
import 'package:ot_viewer_app/settings_page.dart';
import 'package:ot_viewer_app/user_path_bloc.dart';
import 'package:ot_viewer_app/util.dart';
import 'package:ot_viewer_app/web_socket_cubit.dart';
import 'package:path_provider/path_provider.dart';
import 'owntracks_api.dart';
import 'dart:math' as math;
Future<String> getPath() async {
final cacheDirectory = await getTemporaryDirectory();
return cacheDirectory.path;
}
class MapPage extends StatefulWidget {
const MapPage({super.key});
@override
createState() => _MapPageState();
}
class _MapPageState extends State<MapPage> {
@override
void initState() {
super.initState();
// _fetchPoints();
}
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (context) {
final cubit = LocationSubscribeCubit();
2024-03-14 19:50:30 +00:00
final settingsCubit = context.read<SettingsCubit>();
cubit.subscribe(settingsCubit.state);
settingsCubit.stream.listen((settings) => cubit.subscribe(settings));
2024-03-12 20:41:04 +00:00
return cubit;
},
child: FutureBuilder<String>(
future: getPath(),
2024-03-14 19:50:30 +00:00
builder: (something, tempPath) => BlocBuilder<SettingsCubit, SettingsState>(
2024-03-12 20:41:04 +00:00
builder: (context, state) {
if (tempPath.data == null) {
return const Center(child: Text('Loading Map...'));
}
2024-03-12 20:41:04 +00:00
return FlutterMap(
options: const MapOptions(
initialCenter: LatLng(48.3285, 9.8942),
initialZoom: 13.0,
),
children: [
TileLayer(
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
tileProvider: CachedTileProvider(
maxStale: const Duration(days: 30),
store: HiveCacheStore(tempPath.data!, hiveBoxName: 'HiveCacheStore')),
2024-03-12 20:41:04 +00:00
),
...state.activeDevices.map((id) => UserPath(key: ValueKey(id), device: id, settings: state)),
2024-03-14 19:50:30 +00:00
const MapCompass.cupertino(rotationDuration: Duration(milliseconds: 600)),
2024-03-12 20:41:04 +00:00
// CurrentLocationLayer(), TODO: add permission
RichAttributionWidget(
attributions: [
TextSourceAttribution(
'OpenStreetMap contributors',
2024-03-14 19:50:30 +00:00
onTap: () => (Uri.parse('https://openstreetmap.org/copyright')),
2024-03-12 20:41:04 +00:00
),
],
),
],
);
},
),
),
);
}
}
class UserPath extends StatefulWidget {
UserPath({super.key, required this.device, required this.settings});
2024-03-12 20:41:04 +00:00
(String, String) device;
SettingsState settings;
@override
createState() => _UserPathState();
}
class _UserPathState extends State<UserPath> {
@override
Widget build(BuildContext ctx) {
print('rebuilding widget for ${widget.device}');
2024-03-12 20:41:04 +00:00
return BlocProvider(
create: (context) {
final bloc = UserPathBloc(widget.device, widget.settings);
bloc.add(UserPathFullUpdate());
// ONLY WORKS because gets rebuilt every time with settings update
2024-03-12 20:41:04 +00:00
return bloc;
},
child: BlocListener<LocationSubscribeCubit, LocationUpdateState>(
2024-03-14 19:50:30 +00:00
listenWhen: (prevState, state) => state is LocationUpdateReceived,
2024-03-12 20:41:04 +00:00
listener: (context, state) {
UserPathBloc userPathBloc = context.read<UserPathBloc>();
2024-03-14 19:50:30 +00:00
if (state case LocationUpdateReceived(:final position, :final deviceId)) {
2024-03-12 20:41:04 +00:00
if (userPathBloc.deviceId == state.deviceId) {
userPathBloc.add(UserPathLiveSubscriptionUpdate(position));
2024-03-12 20:41:04 +00:00
}
}
},
child: BlocBuilder<UserPathBloc, UserPathState>(
builder: (context, state) {
// TODO: change once smarter rebuilds are ready
context.read<UserPathBloc>().add(UserPathLoginDataChanged(widget.settings));
2024-03-12 20:41:04 +00:00
print("rebuild");
final _istate = state as MainUserPathState;
// make markers
2024-03-14 19:50:30 +00:00
final List<Marker> markers = [];
2024-03-12 20:41:04 +00:00
if (state.livePoints.isNotEmpty) {
2024-03-14 19:50:30 +00:00
markers.add(Marker(
2024-03-12 20:41:04 +00:00
width: 500,
height: 100,
point: state.livePoints.last.asLatLng,
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
2024-03-14 19:50:30 +00:00
onTap: () => showUserLocationModalBottomSheet(context, widget.device),
2024-03-12 20:41:04 +00:00
child: Container(
decoration: BoxDecoration(
color: Colors.black.withOpacity(0.85),
// border: Border.all(color: Colors.lightBlue, width: 2), TODO: add border
borderRadius: BorderRadius.circular(10),
2024-03-14 19:50:30 +00:00
boxShadow: const [
2024-03-12 20:41:04 +00:00
// BoxShadow(color: Colors.black, blurRadius: 4)
]),
2024-03-14 19:50:30 +00:00
padding: const EdgeInsets.all(8),
2024-03-12 20:41:04 +00:00
child: Text(
"${widget.device.$1}:${widget.device.$2}",
softWrap: false,
),
),
),
2024-03-14 19:50:30 +00:00
const Icon(
2024-03-12 20:41:04 +00:00
Icons.location_history,
size: 32,
)
],
),
alignment: Alignment.topCenter,
rotate: true,
));
}
// Create fancy fade-out and blended line (TODO: make distance based. use flutter_map_math)
List<List<LatLng>> segments = [];
List<Color> colors = [];
if (state.initialPoints.isNotEmpty) {
2024-03-14 19:50:30 +00:00
final allPoints = state.initialPoints.map((e) => LatLng(e.lat, e.lon)).toList();
2024-03-12 20:41:04 +00:00
final segmentCount = math.min(100, allPoints.length);
final pointsPerSegment = (allPoints.length / segmentCount).ceil();
// Split the points into segments and generate colors
for (int i = 0; i < allPoints.length; i += pointsPerSegment) {
int end = math.min(i + pointsPerSegment + 1, allPoints.length);
segments.add(allPoints.sublist(i, end));
// Calculate the color for the segment
double ratio = i / allPoints.length;
Color color = Color.lerp(Colors.purple, Colors.blue, ratio)!;
colors.add(color);
}
}
// Create polylines for each segment with the corresponding color
List<Polyline> polylines = [];
for (int i = 0; i < segments.length; i++) {
polylines.add(
Polyline(
points: segments[i],
strokeWidth: 4.0,
2024-03-14 19:50:30 +00:00
color: colors[i]
.withOpacity((math.pow(i, 2) / math.pow(segments.length, 2)) * 0.7 + 0.3), // Fading effect
2024-03-12 20:41:04 +00:00
),
);
}
return Stack(
children: [
PolylineLayer(
key: ValueKey('${widget.device}_historyLines'),
2024-03-12 20:41:04 +00:00
polylines: [
/*
Polyline(
points: state.initialPoints
.map((e) => LatLng(e.lat, e.lon))
.toList(),
strokeWidth: 4.0,
color: Colors.blue,
),
*/
...polylines
],
),
PolylineLayer(key: ValueKey('${widget.device}_liveLines'), polylines: [
2024-03-12 20:41:04 +00:00
Polyline(
2024-03-14 19:50:30 +00:00
points: state.livePoints.map((e) => LatLng(e.lat, e.lon)).toList(),
2024-03-12 20:41:04 +00:00
strokeWidth: 4.0,
color: Colors.blue.shade200,
),
]),
2024-03-14 19:50:30 +00:00
MarkerLayer(markers: markers)
2024-03-12 20:41:04 +00:00
],
);
},
),
),
);
}
}
showUserLocationModalBottomSheet(BuildContext context, (String, String) user) {
showModalBottomSheet(
2024-03-14 19:50:30 +00:00
context: context,
builder: (bsContext) {
return Container(
// height: MediaQuery.of(bsContext).size.height * 0.5,
2024-03-14 19:50:30 +00:00
width: MediaQuery.of(bsContext).size.width,
height: 500,
2024-03-14 19:50:30 +00:00
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.only(topLeft: Radius.circular(30), topRight: Radius.circular(30)),
),
padding: const EdgeInsets.all(32),
child: StreamBuilder<UserPathState>(
stream: context.read<UserPathBloc>().stream,
builder: (sheetContext, state) {
final istate = state.data as MainUserPathState? ?? context.read<UserPathBloc>().state as MainUserPathState;
if (istate.livePoints.isEmpty) {
return Text("Couldn't find ${user.$1}:${user.$2}'s Location");
}
final curLocation = istate.livePoints.last;
return Column(
// mainAxisSize: MainAxisSize.min,
children: [
Text(
'${user.$1}:${user.$2}',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
2024-03-12 20:41:04 +00:00
),
Container(
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(color: Colors.orange, width: 2),
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.all(16),
width: double.infinity,
2024-03-14 19:50:30 +00:00
child: Column(
children: [
Text("(${curLocation.lat.toStringAsFixed(4)}, ${curLocation.lon.toStringAsFixed(4)})"),
Text(DateFormat('dd.MM.yyyy - kk:mm:ss').format(curLocation.timestamp)),
StreamBuilder(
stream: Stream.periodic(const Duration(seconds: 1)),
builder: (context, _) {
return Text("${formatDuration(DateTime.now().difference(curLocation.timestamp))} ago");
},
2024-03-14 19:50:30 +00:00
),
],
),
),
const SizedBox(
height: 16,
),
Expanded(
// Ensure GridView.builder is within an Expanded widget
child: BlocBuilder<GlobalLocationStoreCubit, GlobalLocationStoreState>(
bloc: GetIt.I.get<GlobalLocationStoreCubit>(),
builder: (sheetContext, state) {
// get map camera
final mapController = MapController.of(context);
2024-03-12 20:41:04 +00:00
final mapRotation = mapController.camera.rotation;
2024-03-12 20:41:04 +00:00
List<MapEntry<(String, String), Point>> locations = state.locations
.remove(user) // remove this
.entries // get entries into a list, and sort alphabetically
.toList()
..sort((a, b) => "${a.key.$1}:${a.key.$2}".compareTo("${b.key.$1}:${b.key.$2}"));
if (locations.isEmpty) {
return const SizedBox();
}
return GridView.builder(
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 1, crossAxisSpacing: 4.0, mainAxisSpacing: 4.0, mainAxisExtent: 100),
itemCount: locations.length,
itemBuilder: (BuildContext context, int index) {
// calculate distance and bearing
double distance = distanceBetween(curLocation.lat, curLocation.lon,
locations[index].value.lat, locations[index].value.lon, "meters");
2024-03-12 20:41:04 +00:00
double bearing = (bearingBetween(
curLocation.lat,
curLocation.lon,
locations[index].value.lat,
locations[index].value.lon,
) +
mapRotation) %
360;
2024-03-12 20:41:04 +00:00
print(distance);
print(bearing);
2024-03-12 20:41:04 +00:00
return Center(
child: Container(
margin: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(color: Colors.pink, width: 2),
borderRadius: BorderRadius.circular(10),
),
padding: const EdgeInsets.all(16),
child: Column(
children: [
Row(
children: [
Text(
"${locations[index].key.$1}:${locations[index].key.$2}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
const Spacer(),
Text(formatDistance(distance ~/ 1)),
Transform.rotate(
angle: bearing * (math.pi / 180),
child: const Icon(
Icons.arrow_upward,
color: Colors.blue,
2024-03-14 19:50:30 +00:00
),
)
],
2024-03-14 19:50:30 +00:00
),
Row(
children: [
StreamBuilder(
stream: Stream.periodic(const Duration(seconds: 1)),
builder: (context, _) {
return Text(
"${formatDuration(DateTime.now().difference(locations[index].value.timestamp))} ago",
style: const TextStyle(fontSize: 12));
},
),
const Spacer(),
],
),
],
),
),
);
},
);
},
2024-03-14 19:50:30 +00:00
),
),
],
);
},
2024-03-14 19:50:30 +00:00
),
);
},
);
2024-03-12 20:41:04 +00:00
}