feat(app): stabilize recommendation flow
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:lunchpick/core/utils/category_mapper.dart';
|
||||
import 'package:lunchpick/core/utils/app_logger.dart';
|
||||
import 'package:lunchpick/domain/entities/restaurant.dart';
|
||||
import 'package:lunchpick/domain/repositories/restaurant_repository.dart';
|
||||
import 'package:lunchpick/presentation/providers/di_providers.dart';
|
||||
@@ -14,16 +15,28 @@ final restaurantListProvider = StreamProvider<List<Restaurant>>((ref) {
|
||||
});
|
||||
|
||||
/// 거리 정보를 포함한 맛집 목록 Provider (현재 위치 기반)
|
||||
/// StreamProvider 의존으로 초기 이벤트를 놓치는 문제를 피하기 위해
|
||||
/// 기존 리스트 스트림의 AsyncValue를 그대로 전달하며 정렬만 적용한다.
|
||||
final sortedRestaurantsByDistanceProvider =
|
||||
StreamProvider<List<({Restaurant restaurant, double? distanceKm})>>((ref) {
|
||||
final restaurantsStream = ref.watch(restaurantListProvider.stream);
|
||||
final positionAsync = ref.watch(currentLocationProvider);
|
||||
Provider<AsyncValue<List<({Restaurant restaurant, double? distanceKm})>>>((
|
||||
ref,
|
||||
) {
|
||||
final restaurantsAsync = ref.watch(restaurantListProvider);
|
||||
final positionAsync = ref.watch(currentLocationWithFallbackProvider);
|
||||
final position = positionAsync.maybeWhen(
|
||||
data: (pos) => pos ?? defaultPosition(),
|
||||
data: (pos) => pos,
|
||||
orElse: () => defaultPosition(),
|
||||
);
|
||||
|
||||
return restaurantsStream.map((restaurants) {
|
||||
AppLogger.debug(
|
||||
'[restaurant_list] position ready for sorting: '
|
||||
'${position.latitude}, ${position.longitude}',
|
||||
);
|
||||
|
||||
return restaurantsAsync.whenData((restaurants) {
|
||||
AppLogger.debug(
|
||||
'[restaurant_list] incoming restaurants: ${restaurants.length}',
|
||||
);
|
||||
final sorted =
|
||||
restaurants.map<({Restaurant restaurant, double? distanceKm})>((r) {
|
||||
final distanceKm = DistanceCalculator.calculateDistance(
|
||||
@@ -38,6 +51,10 @@ final sortedRestaurantsByDistanceProvider =
|
||||
b.distanceKm ?? double.infinity,
|
||||
),
|
||||
);
|
||||
AppLogger.debug(
|
||||
'[restaurant_list] sorted list emitted, first distanceKm: '
|
||||
'${sorted.isNotEmpty ? sorted.first.distanceKm?.toStringAsFixed(3) : 'none'}',
|
||||
);
|
||||
return sorted;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user