feat: lots of fixes

This commit is contained in:
2024-03-14 20:50:30 +01:00
parent 5d617131ee
commit 48476f6fe4
11 changed files with 377 additions and 136 deletions

View File

@ -1,5 +1,6 @@
import 'package:bloc/bloc.dart';
import 'package:dio_cache_interceptor_hive_store/dio_cache_interceptor_hive_store.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
@ -7,8 +8,11 @@ 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';
import 'package:flutter_map_math/flutter_geo_math.dart';
import 'package:get_it/get_it.dart';
import 'package:intl/intl.dart';
import 'package:latlong2/latlong.dart';
import 'package:ot_viewer_app/global_location_store.dart';
import 'package:ot_viewer_app/settings_page.dart';
import 'package:ot_viewer_app/user_path_bloc.dart';
import 'package:ot_viewer_app/util.dart';
@ -58,15 +62,14 @@ class _MapPageState extends State<MapPage> {
return BlocProvider(
create: (context) {
final cubit = LocationSubscribeCubit();
final settings_cubit = context.read<SettingsCubit>();
cubit.subscribe(settings_cubit.state);
settings_cubit.stream.listen((settings) => cubit.subscribe(settings));
final settingsCubit = context.read<SettingsCubit>();
cubit.subscribe(settingsCubit.state);
settingsCubit.stream.listen((settings) => cubit.subscribe(settings));
return cubit;
},
child: FutureBuilder<String>(
future: getPath(),
builder: (something, temp_path) =>
BlocBuilder<SettingsCubit, SettingsState>(
builder: (something, tempPath) => BlocBuilder<SettingsCubit, SettingsState>(
builder: (context, state) {
return FlutterMap(
options: const MapOptions(
@ -78,20 +81,16 @@ class _MapPageState extends State<MapPage> {
urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
tileProvider: CachedTileProvider(
maxStale: const Duration(days: 30),
store: HiveCacheStore(temp_path.data,
hiveBoxName: 'HiveCacheStore')),
store: HiveCacheStore(tempPath.data, hiveBoxName: 'HiveCacheStore')),
),
...state.activeDevices
.map((id) => UserPath(device: id, settings: state)),
const MapCompass.cupertino(
rotationDuration: Duration(milliseconds: 600)),
...state.activeDevices.map((id) => UserPath(device: id, settings: state)),
const MapCompass.cupertino(rotationDuration: Duration(milliseconds: 600)),
// CurrentLocationLayer(), TODO: add permission
RichAttributionWidget(
attributions: [
TextSourceAttribution(
'OpenStreetMap contributors',
onTap: () =>
(Uri.parse('https://openstreetmap.org/copyright')),
onTap: () => (Uri.parse('https://openstreetmap.org/copyright')),
),
],
),
@ -124,14 +123,12 @@ class _UserPathState extends State<UserPath> {
return bloc;
},
child: BlocListener<LocationSubscribeCubit, LocationUpdateState>(
listenWhen: (prevState, state) => state is LocationUpdateReceived,
listener: (context, state) {
UserPathBloc userPathBloc = context.read<UserPathBloc>();
if (state
case LocationUpdateReceived(:final position, :final deviceId)) {
if (state case LocationUpdateReceived(:final position, :final deviceId)) {
if (userPathBloc.deviceId == state.deviceId) {
context
.read<UserPathBloc>()
.add(UserPathLiveSubscriptionUpdate(position));
context.read<UserPathBloc>().add(UserPathLiveSubscriptionUpdate(position));
}
}
},
@ -140,10 +137,10 @@ class _UserPathState extends State<UserPath> {
print("rebuild");
final _istate = state as MainUserPathState;
// make markers
final List<Marker> _markers = [];
final List<Marker> markers = [];
if (state.livePoints.isNotEmpty) {
_markers.add(Marker(
markers.add(Marker(
width: 500,
height: 100,
point: state.livePoints.last.asLatLng,
@ -151,25 +148,23 @@ class _UserPathState extends State<UserPath> {
mainAxisAlignment: MainAxisAlignment.end,
children: [
GestureDetector(
onTap: () => showUserLocationModalBottomSheet(
context, widget.device
),
onTap: () => showUserLocationModalBottomSheet(context, widget.device),
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),
boxShadow: [
boxShadow: const [
// BoxShadow(color: Colors.black, blurRadius: 4)
]),
padding: EdgeInsets.all(8),
padding: const EdgeInsets.all(8),
child: Text(
"${widget.device.$1}:${widget.device.$2}",
softWrap: false,
),
),
),
Icon(
const Icon(
Icons.location_history,
size: 32,
)
@ -184,8 +179,7 @@ class _UserPathState extends State<UserPath> {
List<List<LatLng>> segments = [];
List<Color> colors = [];
if (state.initialPoints.isNotEmpty) {
final allPoints =
state.initialPoints.map((e) => LatLng(e.lat, e.lon)).toList();
final allPoints = state.initialPoints.map((e) => LatLng(e.lat, e.lon)).toList();
final segmentCount = math.min(100, allPoints.length);
final pointsPerSegment = (allPoints.length / segmentCount).ceil();
@ -208,9 +202,8 @@ class _UserPathState extends State<UserPath> {
Polyline(
points: segments[i],
strokeWidth: 4.0,
color: colors[i].withOpacity(
(math.pow(i, 2) / math.pow(segments.length, 2)) * 0.7 +
0.3), // Fading effect
color: colors[i]
.withOpacity((math.pow(i, 2) / math.pow(segments.length, 2)) * 0.7 + 0.3), // Fading effect
),
);
}
@ -234,14 +227,12 @@ class _UserPathState extends State<UserPath> {
),
PolylineLayer(polylines: [
Polyline(
points: state.livePoints
.map((e) => LatLng(e.lat, e.lon))
.toList(),
points: state.livePoints.map((e) => LatLng(e.lat, e.lon)).toList(),
strokeWidth: 4.0,
color: Colors.blue.shade200,
),
]),
MarkerLayer(markers: _markers)
MarkerLayer(markers: markers)
],
);
},
@ -253,83 +244,170 @@ class _UserPathState extends State<UserPath> {
showUserLocationModalBottomSheet(BuildContext context, (String, String) user) {
showModalBottomSheet(
context: context,
builder: (bsContext) {
return Container(
height: MediaQuery.of(bsContext).size.height * 0.26,
width: MediaQuery.of(bsContext).size.width,
decoration: BoxDecoration(
color: Colors.black,
// border: Border.all(color: Colors.blueAccent, width: 2),
borderRadius: BorderRadius.only(topLeft: Radius.circular(30), topRight: Radius.circular(30)),
context: context,
builder: (bsContext) {
return Container(
height: MediaQuery.of(bsContext).size.height * 0.5,
width: MediaQuery.of(bsContext).size.width,
decoration: const BoxDecoration(
color: Colors.black,
borderRadius: BorderRadius.only(topLeft: Radius.circular(30), topRight: Radius.circular(30)),
),
padding: EdgeInsets.all(32),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
// Wrap non-grid content in Flexible to manage space dynamically
child: Text(
'${user.$1}:${user.$2}',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
),
),
),
padding: EdgeInsets.all(32),
child: Column(
children: [
Text(
'${user.$1}:${user.$2}',
style: const TextStyle(
fontSize: 24,
fontWeight: FontWeight.bold,
const SizedBox(height: 16),
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 Flexible(
// Use Flexible for dynamic content
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
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,
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");
},
),
],
),
),
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);
final mapRotation = mapController.camera.rotation;
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: 2,
crossAxisSpacing: 4.0,
mainAxisSpacing: 4.0,
childAspectRatio: 2.8,
),
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");
double bearing = (bearingBetween(
curLocation.lat,
curLocation.lon,
locations[index].value.lat,
locations[index].value.lon,
) +
mapRotation) %
360;
print(distance);
print(bearing);
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: EdgeInsets.all(16),
child: Column(
children: [
Row(
children: [
Text(
"${locations[index].key.$1}:${locations[index].key.$2}",
style: const TextStyle(fontWeight: FontWeight.bold),
),
Spacer(),
Text(formatDistance(distance ~/ 1)),
Transform.rotate(
angle: bearing * (math.pi / 180),
child: Icon(
Icons.arrow_upward,
color: Colors.blue,
),
)
],
),
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)
);
},
),
Spacer(),
],
),
],
),
),
);
},
);
},
),
),
],
),
),
SizedBox(height: 16,),
Builder(
builder: (sheetContext) {
final state = context.watch<UserPathBloc>().state
as MainUserPathState;
// get user's current location
// final _istate = state as MainUserPathState;
if (state.livePoints.isEmpty) {
return Text(
"Couldn't find ${user.$1}:${user.$2}'s Location");
}
final curLocation = state.livePoints.last;
// MapController.of(sheetContext).camera.pointToLatLng(
// math.Point(curLocation.lat, curLocation.lon));
return Column(
children: [
Container(
decoration: BoxDecoration(
color: Colors.black,
border: Border.all(color: Colors.orange, width: 2),
borderRadius: BorderRadius.circular(10),
),
padding: EdgeInsets.all(16),
width: double.infinity,
child: Column(
children: [
Text(
"(${curLocation.lat.toStringAsFixed(4)}, ${curLocation.lon.toStringAsFixed(4)})"),
Text(DateFormat('dd.MM.yyyy - kk:mm:ss')
.format(curLocation.timestamp)),
StreamBuilder(
// rebuild every second for that ticking effect
// not hyper efficient, but it's only a text
stream: Stream.periodic(
const Duration(seconds: 1)),
builder: (context, _) {
return Text(
"${formatDuration(DateTime.now().difference(curLocation.timestamp))} ago");
}),
],
),
)
],
);
},
),
],
));
// title with icon, user name and device id
// location as a lat, long (truncated to 4 comma spaces)
// time since last location update
// location to other users in a grid (2 rows)
});
);
},
),
],
),
);
},
);
}