Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/screens/map_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1901,6 +1901,23 @@ class _MaizeBusCoreState extends State<MaizeBusCore> {
);
}
},
onSelectBuilding: (Location location) {
_showBuildingSheet(location);
},
onBuildingGetDirections: (Location location) {
Map<String, double>? start;
Map<String, double>? end = {
'lat': location.latlng!.latitude,
'lon': location.latlng!.longitude,
};
_showDirectionsSheet(
start,
end,
"Current Location",
location.name,
false,
);
},
onUnfavorite: (stpid) {
// update in memory and marker icons immediately
setState(() {
Expand Down
105 changes: 105 additions & 0 deletions lib/util/favorite_building_storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import 'dart:convert';

import 'package:google_maps_flutter/google_maps_flutter.dart';

import '../constants.dart';

const String kFavoriteBuildingsPrefsKey = 'favorite_buildings';
// Function to get the storage id for a building
String favoriteBuildingStorageId(Location b) {
final ll = b.latlng;
if (ll != null) {
return 'geo:${ll.latitude.toStringAsFixed(6)},${ll.longitude.toStringAsFixed(6)}';
}
return 'name:${b.name}';
}

// Function to encode a building into a string
String encodeFavoriteBuilding(Location b) {
final ll = b.latlng;
return jsonEncode({
'id': favoriteBuildingStorageId(b),
'name': b.name,
'abbrev': b.abbrev,
if (ll != null) 'lat': ll.latitude,
if (ll != null) 'lon': ll.longitude,
});
}

//Checks if the stored string is a valid favorite building entry, if not return null.
String? favoriteBuildingEntryId(String stored) {
try {
final m = jsonDecode(stored) as Map<String, dynamic>;
return m['id'] as String?;
} catch (_) {
if (stored.startsWith('geo:') || stored.startsWith('name:')) {
return stored;
}
return null;
}
}

class FavoriteBuildingEntry {
final String raw;
final String id;
final String name;
final String abbrev;
final double? lat;
final double? lon;

const FavoriteBuildingEntry({
required this.raw,
required this.id,
required this.name,
required this.abbrev,
required this.lat,
required this.lon,
});
// Function to convert a building to a location
Location? toLocation() {
if (lat == null || lon == null) return null;
return Location(
name,
abbrev,
const [],
false,
latlng: LatLng(lat!, lon!),
);
}
}
// Function to decode a building from a string
FavoriteBuildingEntry? decodeFavoriteBuildingEntry(String stored) {
try {
final m = jsonDecode(stored) as Map<String, dynamic>;
final id = m['id'] as String?;
if (id == null) return null;
return FavoriteBuildingEntry(
raw: stored,
id: id,
name: m['name'] as String? ?? 'Building',
abbrev: m['abbrev'] as String? ?? '',
lat: (m['lat'] as num?)?.toDouble(),
lon: (m['lon'] as num?)?.toDouble(),
);
} catch (_) {
return _decodeLegacyFavoriteBuildingEntry(stored);
}
}
//if the json fails since its not a json then this function cehcks is this old geo: format pulls the lat and lon so that the UI can display.
FavoriteBuildingEntry? _decodeLegacyFavoriteBuildingEntry(String stored) {
if (!stored.startsWith('geo:')) return null;
final rest = stored.substring(4);
final comma = rest.indexOf(',');
if (comma <= 0 || comma >= rest.length - 1) return null;
final lat = double.tryParse(rest.substring(0, comma));
final lon = double.tryParse(rest.substring(comma + 1));
if (lat == null || lon == null) return null;
return FavoriteBuildingEntry(
raw: stored,
id: stored,
name: 'Saved building',
abbrev: '',
lat: lat,
lon: lon,
);
}
Loading