Skip to content

Commit

Permalink
Refactor map screen and query improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-radhika-s committed Dec 24, 2024
1 parent c9ee06e commit 90a01ef
Show file tree
Hide file tree
Showing 8 changed files with 289 additions and 294 deletions.
18 changes: 9 additions & 9 deletions app/lib/ui/flow/home/components/home_top_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class HomeTopBar extends StatefulWidget {

class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {
bool expand = false;
late int selectedIndex = widget.spaces.indexWhere((space) => space.space.id == widget.selectedSpace?.space.id);

late AnimationController _animationController;
late Animation<double> _animation;
Expand Down Expand Up @@ -104,7 +103,11 @@ class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {
builder: (context, orientation) {
return IntrinsicHeight(
child: Container(
padding: EdgeInsets.only(left: 16, right: 16, bottom: 16, top: orientation == Orientation.portrait ? 0 : 16),
padding: EdgeInsets.only(
left: 16,
right: 16,
bottom: 16,
top: orientation == Orientation.portrait ? 0 : 16),
color: context.colorScheme.surface,
child: SingleChildScrollView(
child: Column(
Expand Down Expand Up @@ -133,7 +136,8 @@ class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {
visibility: !expand,
onTap: () {
if (widget.selectedSpace != null) {
AppRoute.message(widget.selectedSpace!).push(context);
AppRoute.message(widget.selectedSpace!)
.push(context);
}
},
),
Expand Down Expand Up @@ -270,9 +274,6 @@ class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {
padding: const EdgeInsets.symmetric(vertical: 8),
child: GestureDetector(
onTap: () {
setState(() {
selectedIndex = index;
});
widget.onSpaceItemTap(space);
},
child: _spaceListItem(
Expand Down Expand Up @@ -304,7 +305,6 @@ class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {
return GestureDetector(
onTap: () {
setState(() {
selectedIndex = index;
widget.onSpaceItemTap(space);
expand = false;
performAnimation();
Expand All @@ -326,10 +326,10 @@ class _HomeTopBarState extends State<HomeTopBar> with TickerProviderStateMixin {
const SizedBox(width: 4),
Radio<int>(
value: index,
groupValue: selectedIndex,
groupValue: widget.spaces.indexWhere(
(space) => space.space.id == widget.selectedSpace?.space.id),
onChanged: (val) {
setState(() {
selectedIndex = index;
widget.onSpaceItemTap(space);
});
},
Expand Down
1 change: 0 additions & 1 deletion app/lib/ui/flow/home/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:yourspace_flutter/ui/components/app_page.dart';
import 'package:yourspace_flutter/ui/components/error_snakebar.dart';
import 'package:yourspace_flutter/ui/components/resume_detector.dart';
import 'package:yourspace_flutter/ui/flow/home/home_screen_viewmodel.dart';
import 'package:yourspace_flutter/ui/flow/home/map/map_view_model.dart';

import '../../../domain/fcm/notification_handler.dart';
import '../../components/alert.dart';
Expand Down
7 changes: 5 additions & 2 deletions app/lib/ui/flow/home/home_screen_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
if (_currentUser == null) return;
try {
_spacePlacesSubscription?.cancel();
if (_currentUser?.space_ids?.isEmpty ?? true) return;
_spacePlacesSubscription = spaceService
.getStreamPlacesByUserId(_currentUser!.id)
.getStreamPlacesByUserId(_currentUser?.space_ids ?? List.empty())
.listen((places) {
if (places.isEmpty) {
logger.e('No places found for spaces.');
return;
}


GeofenceService.startMonitoring(places);
});
} catch (error) {
Expand Down Expand Up @@ -119,6 +121,8 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {
} else if (prevUser?.id != currentUser.id) {
fetchData();
_listenPlaces();
} else if (prevUser?.space_ids != currentUser.space_ids) {
_listenPlaces();
}
}

Expand All @@ -138,7 +142,6 @@ class HomeViewNotifier extends StateNotifier<HomeViewState> {

_spacesSubscription =
spaceService.streamAllSpace(userId).listen((spaces) {

if ((currentSpaceId?.isEmpty ?? true) && spaces.isNotEmpty) {
spaceService.currentSpaceId = spaces.firstOrNull?.space.id;
}
Expand Down
156 changes: 156 additions & 0 deletions app/lib/ui/flow/home/map/components/marker_generator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import 'dart:io';
import 'dart:ui' as ui;

import 'package:flutter/cupertino.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:style/extenstions/context_extenstions.dart';
import 'package:style/text/app_text_dart.dart';
import 'package:yourspace_flutter/ui/flow/home/map/map_view_model.dart';

double markerSize = Platform.isAndroid ? 124.0 : 70.0;
double markerRadius = Platform.isAndroid ? 60.0 : 30.0;

Future<List<Marker>> createMarkerFromAsset(
BuildContext context,
List<MapUserInfo> users, {
required Function(String)? onTap,
}) async {
if (!context.mounted) return [];
final markers = await Future.wait(users.map((item) async {
final marker = await _mapMarker(
item.user.fullName,
item.imageUrl,
item.isSelected
? context.colorScheme.secondary
: context.colorScheme.surface,
context.colorScheme.primary,
AppTextStyle.subtitle2.copyWith(
fontSize: Platform.isAndroid ? 70 : 40,
color: context.colorScheme.textInversePrimary),
);

return Marker(
markerId: MarkerId(item.userId),
position: LatLng(item.latitude, item.longitude),
anchor: const Offset(0.0, 1.0),
icon: marker,
onTap: () {
onTap?.call(item.userId);
},
);
}).toList());
return markers;
}

Future<BitmapDescriptor> _mapMarker(
String userName,
ui.Image? imageUrl,
Color markerBgColor,
Color iconBgColor,
TextStyle textStyle,
) async {
if (imageUrl != null) {
return await _userImageMarker(imageUrl, markerBgColor, iconBgColor);
} else {
return await _userCharMarker(
userName, markerBgColor, iconBgColor, textStyle);
}
}

Future<BitmapDescriptor> _userCharMarker(
String userName,
Color markerBgColor,
Color iconBgColor,
TextStyle textStyle,
) async {
final pictureRecorder = ui.PictureRecorder();
final canvas = Canvas(pictureRecorder);

// Draw background rectangle
canvas.drawRRect(
RRect.fromRectAndCorners(
Rect.fromLTWH(0.0, 0.0, markerSize, markerSize),
topLeft: Radius.circular(markerRadius),
topRight: Radius.circular(markerRadius),
bottomLeft: const Radius.circular(0),
bottomRight: Radius.circular(markerRadius),
),
Paint()..color = markerBgColor,
);

_drawUserName(canvas, userName, iconBgColor, textStyle);

final picture = pictureRecorder.endRecording();
final img = await picture.toImage(markerSize.toInt(), markerSize.toInt());
final byteData = await img.toByteData(format: ui.ImageByteFormat.png);
final bitmapDescriptor = BitmapDescriptor.bytes(
byteData!.buffer.asUint8List(),
bitmapScaling: MapBitmapScaling.none);

return bitmapDescriptor;
}

void _drawUserName(
Canvas canvas,
String userName,
Color bgColor,
TextStyle textStyle,
) {
final textPainter = TextPainter(textDirection: TextDirection.ltr);

canvas.drawCircle(Offset(markerSize / 2, markerSize / 2),
Platform.isAndroid ? 50 : 30, Paint()..color = bgColor);

textPainter.text = TextSpan(
text: userName.isNotEmpty ? userName[0] : '',
style: textStyle,
);
textPainter.layout();
textPainter.paint(
canvas,
Offset(
(markerSize - textPainter.width) / 2,
(markerSize - textPainter.height) / 2,
),
);
}

Future<BitmapDescriptor> _userImageMarker(
ui.Image uiImage,
Color markerBgColor,
Color iconBgColor,
) async {
// Prepare the canvas to draw the rounded rectangle and the image
final recorder = ui.PictureRecorder();
final canvas = ui.Canvas(recorder,
Rect.fromPoints(const Offset(0, 0), Offset(markerSize, markerSize)));

// Draw the rounded rectangle
canvas.drawRRect(
RRect.fromRectAndCorners(
Rect.fromLTWH(0.0, 0.0, markerSize, markerSize),
topLeft: Radius.circular(markerRadius),
topRight: Radius.circular(markerRadius),
bottomLeft: const Radius.circular(0),
bottomRight: Radius.circular(markerRadius),
),
Paint()..color = markerBgColor,
);

// Calculate the position to center the image
final double imageOffset = (markerSize - uiImage.width.toDouble()) / 2;
final Offset offset = Offset(imageOffset, imageOffset);

// Draw the image on the canvas centered
canvas.drawImage(uiImage, offset, Paint()..color = iconBgColor);

// End recording and create an image from the canvas
final picture = recorder.endRecording();
final imgData = await picture.toImage(markerSize.toInt(), markerSize.toInt());
final data = await imgData.toByteData(format: ui.ImageByteFormat.png);

final bitmapDescriptor = BitmapDescriptor.bytes(data!.buffer.asUint8List(),
bitmapScaling: MapBitmapScaling.none);

return bitmapDescriptor;
}
Loading

0 comments on commit 90a01ef

Please sign in to comment.