-
Notifications
You must be signed in to change notification settings - Fork 2
perf(search): Cache building list at startup to speed up search #82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: maizebus-2.1
Are you sure you want to change the base?
Changes from all commits
198f4c2
ae070a3
1dab18f
1c0bc2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -691,7 +691,7 @@ | |
| CLANG_ENABLE_MODULES = YES; | ||
| CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements; | ||
| CURRENT_PROJECT_VERSION = 5; | ||
| DEVELOPMENT_TEAM = 4LLPM7NY5C; | ||
| DEVELOPMENT_TEAM = TY8BMZ5C7D; | ||
| ENABLE_BITCODE = NO; | ||
| INFOPLIST_FILE = Runner/Info.plist; | ||
| INFOPLIST_KEY_CFBundleDisplayName = MaizeBus; | ||
|
|
@@ -724,7 +724,7 @@ | |
| CLANG_ENABLE_MODULES = YES; | ||
| CODE_SIGN_ENTITLEMENTS = Runner/Runner.entitlements; | ||
| CURRENT_PROJECT_VERSION = 5; | ||
| DEVELOPMENT_TEAM = 4LLPM7NY5C; | ||
| DEVELOPMENT_TEAM = TY8BMZ5C7D; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is also unnecessary. |
||
| ENABLE_BITCODE = NO; | ||
| INFOPLIST_FILE = Runner/Info.plist; | ||
| INFOPLIST_KEY_CFBundleDisplayName = MaizeBus; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>aps-environment</key> | ||
| <string>development</string> | ||
| </dict> | ||
| <dict/> | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This again is unnecessary |
||
| </plist> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,97 +35,11 @@ class LocationSearchBar extends HookWidget { | |
| }, [focusNode]); | ||
| final searchQuery = useState(''); | ||
|
|
||
| final refreshKey = useState(0); | ||
|
|
||
| // Parse Buildings | ||
| final locations = useMemoized(() async { | ||
| try { | ||
| final buildingResponse = await http.get( | ||
| Uri.parse(BACKEND_URL + '/getBuildingLocations'), | ||
| ); | ||
| List<Location> buildingLocs = []; | ||
| if (buildingResponse.statusCode == 200 && | ||
| buildingResponse.body.trim().isNotEmpty && | ||
| buildingResponse.body.trim() != '{}') { | ||
| final buildingLocations = | ||
| jsonDecode(buildingResponse.body) as List<dynamic>; | ||
| buildingLocs = buildingLocations.map((building) { | ||
| final name = building['buildingName'] as String; | ||
| final abbrev = building['abbrev'] as String?; | ||
| final altName = building['altName'] as String?; | ||
| final lat = building['lat'] as double; | ||
| final long = building['long'] as double; | ||
| return Location( | ||
| name, | ||
| (abbrev != null) ? abbrev : "", | ||
| [if (abbrev != null) abbrev, if (altName != null) altName], | ||
| false, | ||
| latlng: LatLng(lat, long), | ||
| ); | ||
| }).toList(); | ||
| } | ||
|
|
||
| // TODO: this code is DUPLICATED. We need to refactor to avoid duplication. | ||
| // LOADS BOTH STOP TYPES | ||
| final uriStops = Uri.parse(BACKEND_URL + '/getAllStops'); | ||
| final uriRideStops = Uri.parse(BACKEND_URL + '/getAllRideStops'); | ||
|
|
||
| // Calling in parallel | ||
| final responses = await Future.wait([ | ||
| http.get(uriStops), | ||
| http.get(uriRideStops), | ||
| ]); | ||
|
|
||
| // Helper function to parse a response into a List<Location> | ||
| // This prevents copying/pasting the parsing logic. | ||
| List<Location> parseLocations(http.Response response) { | ||
| if (response.statusCode == 200 && | ||
| response.body.trim().isNotEmpty && | ||
| response.body.trim() != '{}') { | ||
|
|
||
| final stopList = jsonDecode(response.body) as List<dynamic>; | ||
|
|
||
| return stopList.map((stop) { | ||
| final name = stop['name'] as String; | ||
| final aliases = [ | ||
| name.split(' ').map((w) => w.isNotEmpty ? w[0] : '').join(), | ||
| ]; | ||
| final stopId = stop['stpid'] as String?; | ||
| final lat = stop['lat'] as double?; | ||
| final lon = stop['lon'] as double?; | ||
|
|
||
| return Location( | ||
| name, | ||
| (stopId != null) ? stopId : "", | ||
| aliases, | ||
| true, | ||
| stopId: stopId, | ||
| latlng: (lat != null && lon != null) ? LatLng(lat, lon) : null, | ||
| ); | ||
| }).toList(); | ||
| } | ||
| return []; // Return empty list if call failed or body is empty | ||
| } | ||
|
|
||
| // parse both and merge | ||
| List<Location> stopLocs = [ | ||
| ...parseLocations(responses[0]), | ||
| ...parseLocations(responses[1]), | ||
| ]; | ||
|
|
||
| globalStopLocs = stopLocs; | ||
|
|
||
| final allLocs = [...buildingLocs, ...stopLocs]; | ||
| if (allLocs.isEmpty) { | ||
| refreshKey.value++; | ||
| } | ||
|
|
||
| return allLocs; | ||
| } catch (e) { | ||
| print('Failed to fetch locations: $e'); | ||
| refreshKey.value++; | ||
| return <Location>[]; | ||
| } | ||
| }, [refreshKey.value]); | ||
| final allLocs = [...globalBuildingLocs, ...globalStopLocs]; | ||
| return allLocs; | ||
| }, []); | ||
|
|
||
| Map<String, Set<Location>> buildNgramIndex( | ||
| List<Location> locations, { | ||
|
|
@@ -207,15 +121,13 @@ class LocationSearchBar extends HookWidget { | |
| SizedBox( | ||
| height: 50, | ||
| child: DecoratedBox( | ||
| decoration: BoxDecoration( | ||
| borderRadius: BorderRadius.circular(56), | ||
| ), | ||
| decoration: BoxDecoration(borderRadius: BorderRadius.circular(56)), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future, stylistic changes such as this and the others in this file can be in a separate pull request if you feel that they are necessary, otherwise, try not to include them in an unrelated pull request. This is fine for now tho |
||
| child: TextField( | ||
| textAlignVertical: TextAlignVertical.center, | ||
| textInputAction: TextInputAction.go, | ||
| style: TextStyle( | ||
| style: TextStyle( | ||
| color: getColor(context, ColorType.opposite).withAlpha(204), | ||
| fontSize: 22 | ||
| fontSize: 22, | ||
| ), | ||
| autofocus: true, | ||
| controller: controller, | ||
|
|
@@ -312,12 +224,16 @@ class LocationSearchBar extends HookWidget { | |
| ? Icon( | ||
| Icons.hail, | ||
| size: 40, | ||
| color: isDarkMode(context) ? Color.fromARGB(150, 255, 255, 255) : Color.fromARGB(150, 0, 0, 0), | ||
| color: isDarkMode(context) | ||
| ? Color.fromARGB(150, 255, 255, 255) | ||
| : Color.fromARGB(150, 0, 0, 0), | ||
| ) | ||
| : Icon( | ||
| Icons.business_rounded, | ||
| size: 40, | ||
| color: isDarkMode(context) ? Color.fromARGB(150, 255, 255, 255) : Color.fromARGB(150, 0, 0, 0), | ||
| color: isDarkMode(context) | ||
| ? Color.fromARGB(150, 255, 255, 255) | ||
| : Color.fromARGB(150, 0, 0, 0), | ||
| ), | ||
| onTap: () { | ||
| controller.text = loc.name; | ||
|
|
@@ -372,7 +288,7 @@ class _SearchSheetState extends State<SearchSheet> { | |
| topLeft: Radius.circular(30), | ||
| topRight: Radius.circular(30), | ||
| ), | ||
| boxShadow: [SheetBoxShadow] | ||
| boxShadow: [SheetBoxShadow], | ||
| ), | ||
| child: Column( | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
|
|
@@ -421,7 +337,7 @@ class _SearchSheetState extends State<SearchSheet> { | |
| color: getColor(context, ColorType.inputText), | ||
| ), | ||
| ), | ||
|
|
||
| enabledBorder: OutlineInputBorder( | ||
| borderRadius: BorderRadius.all(Radius.circular(56.0)), | ||
| borderSide: BorderSide(color: Colors.transparent, width: 0), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is unnecessary.