Skip to content
Merged
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
5 changes: 5 additions & 0 deletions lib/core/config/app_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class FleetEventTypes {
static const rideCompleted = 'ride.completed';
static const rideCancelled = 'ride.cancelled';

/// 司機放棄已接的訂單,行程**回到派單中**(注意:不是取消)。
/// 沒有這則事件時,乘客畫面會停在司機卡片上,直到最多 15 秒後的輪詢
/// 才無聲退回「配對中」,乘客不知道發生什麼事。
static const rideRedispatched = 'ride.redispatched';

/// 司機標記到達/跳過某一站(N8)。payload 帶**整趟** stops,收到直接覆蓋即可。
static const rideStopUpdated = 'ride.stop_updated';
static const chatMessage = 'chat.message';
Expand Down
33 changes: 33 additions & 0 deletions lib/customer/customer_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class CustomerController extends ChangeNotifier {
// 是否有待呈現的取消通知(P4)。reason 為 null 也要通知(乘客主動取消/司機放棄
// 走泛用文案),故需獨立旗標,不能只看 _cancelReason。
bool _rideCancelled = false;
// 司機放棄後「正在重新派車」的說明;與取消通知分開——**行程還在**,
// 混用取消通知會讓乘客以為要重新叫車。
String? _redispatchNotice;
// 乘客指定的車種(P2);null =不指定,維持現行行為。
VehicleType? _requiredVehicleType;
// 寵物車清潔費率(P5);null =尚未查到(費率不常變,快取一次即可)。
Expand Down Expand Up @@ -124,6 +127,17 @@ class CustomerController extends ChangeNotifier {
bool get suggestAnyVehicle =>
_rideCancelled && shouldSuggestAnyVehicle(_cancelReason);

/// 司機放棄後的「正在重新派車」說明;null = 沒有要顯示的。
/// 與 [cancelNotice] 互斥使用:這個代表**行程還在**,只是換司機。
String? get redispatchNotice => _redispatchNotice;

/// 有新司機接單/新叫車/行程結束時清掉。
void dismissRedispatchNotice() {
if (_redispatchNotice == null) return;
_redispatchNotice = null;
notifyListeners();
}

/// 關閉取消通知(乘客按「知道了」或採用快捷操作後)。
void dismissCancelNotice() {
_rideCancelled = false;
Expand Down Expand Up @@ -471,6 +485,7 @@ class CustomerController extends ChangeNotifier {
_cancelReason = null;
_cancelledVehicleType = null;
_rideCancelled = false;
_redispatchNotice = null;
_requiredVehicleType = null;
_passengers.clear();
_estimate = null;
Expand Down Expand Up @@ -601,6 +616,22 @@ class CustomerController extends ChangeNotifier {
// O4/O7:車種車牌供路邊對車,電話供直接聯絡(明碼,僅該趟乘客收得到此事件)。
_driverInfo = RideDriverInfo.fromPayload(event.payload ?? const {});
_driverArrived = false;
// 新司機接單=「重新派車中」已經結束,通知留著會與畫面上的司機卡片矛盾。
_redispatchNotice = null;
refreshActive(silent: true);
// 司機放棄,行程回到派單中——**不是取消**,訂單還在,只是重新找司機。
// 這裡必須把上一位司機的所有痕跡清乾淨:車牌/撥號按鈕若留著,
// 乘客會打給一個已經不來的司機;ETA 與地圖上的車也是舊的。
case FleetEventTypes.rideRedispatched:
_driverName = null;
_driverInfo = null;
_driverArrived = false;
_liveEtaSec = null;
_liveDistM = null;
_liveDriverLat = null;
_liveDriverLng = null;
_redispatchNotice = '司機取消了行程,正在為您重新派車';
notifyListeners();
refreshActive(silent: true);
case FleetEventTypes.driverLocation:
_liveEtaSec = (event.payload?['eta_sec'] as num?)?.toInt();
Expand All @@ -624,6 +655,8 @@ class CustomerController extends ChangeNotifier {
_cancelReason = CancelReason.fromCode(event.payload?['cancel_reason'] as String?);
_cancelledVehicleType = event.payload?['required_vehicle_type'] as String?;
_rideCancelled = true;
// 行程真的結束了,「正在重新派車」不能再留著(重派沒成功才會走到這裡)。
_redispatchNotice = null;
refreshActive(silent: true);
default:
break;
Expand Down
29 changes: 29 additions & 0 deletions lib/customer/widgets/ride_phase_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ class SearchingContent extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 8),
// 司機放棄後會退回這個階段。沒有這塊說明,乘客只會看到司機卡片
// 突然變回轉圈圈,不知道發生什麼事(2026-07-28 跨端對帳實測抓到)。
if (ctrl.redispatchNotice != null) ...[
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.secondaryContainer,
borderRadius: BorderRadius.circular(12),
),
child: Row(
children: [
Icon(
Icons.info_outline,
color: Theme.of(context).colorScheme.onSecondaryContainer,
),
const SizedBox(width: 8),
Expanded(
child: Text(
ctrl.redispatchNotice!,
style: TextStyle(
color: Theme.of(context).colorScheme.onSecondaryContainer,
),
),
),
],
),
),
const SizedBox(height: 12),
],
const Center(child: CircularProgressIndicator()),
const SizedBox(height: 16),
Center(
Expand Down
117 changes: 117 additions & 0 deletions test/customer_redispatch_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:line_fleet_app/core/api/customer_api_client.dart';
import 'package:line_fleet_app/core/config/app_config.dart';
import 'package:line_fleet_app/core/models/models.dart';
import 'package:line_fleet_app/core/theme/app_theme.dart';
import 'package:line_fleet_app/core/ws/fleet_ws_client.dart';
import 'package:line_fleet_app/customer/customer_controller.dart';
import 'package:line_fleet_app/customer/widgets/ride_phase_content.dart';
import 'package:provider/provider.dart';

/// 2026-07-28 跨端契約對帳抓到:司機放棄已接的訂單時,後端**只推 LINE**
/// (「司機取消了行程,正在為您重新派車」),對 App 乘客一則 WS 事件都沒有。
/// 結果是司機卡片停在畫面上,直到最多 15 秒後的輪詢才無聲退回「配對中」——
/// 期間乘客看到的車牌與撥號按鈕都是那位已經不來的司機。
///
/// 後端補送 `ride.redispatched`(本來就定義了,先前只用於 audit),App 這邊接住它。
void main() {
late CustomerController ctrl;
// 輪詢是 Timer.periodic:測試裡若讓它活著,testWidgets 會以 "Pending timers" 失敗。
// dispose 會停掉它;已在測試內 dispose 過的就別再 dispose 一次。
var disposed = false;

setUp(() {
disposed = false;
ctrl = CustomerController(api: _StubApi());
ctrl.setSessionForTest(
const CustomerSession(customerId: 1, token: 'tok', name: '測試乘客'),
);
ctrl.setActiveRideForTest(
const CustomerRide(rideId: 9, status: RideStatus.accepted),
driverName: '阿明',
liveEtaSec: 300,
);
});

tearDown(() {
if (!disposed) ctrl.dispose();
});

void redispatch() => ctrl.handleWsEventForTest(
FleetWsEvent(type: FleetEventTypes.rideRedispatched, rideId: 9),
);

test('司機放棄 → 清掉上一位司機的所有痕跡並給說明', () {
expect(ctrl.driverName, '阿明');

redispatch();

expect(ctrl.redispatchNotice, '司機取消了行程,正在為您重新派車');
// 車牌/撥號按鈕若留著,乘客會打給一個已經不來的司機
expect(ctrl.driverName, isNull);
expect(ctrl.driverInfo, isNull);
expect(ctrl.liveEtaSec, isNull, reason: '舊司機的 ETA 不能留在畫面上');
});

test('不是取消:訂單還在,不該顯示取消通知', () {
redispatch();

expect(ctrl.cancelNotice, isNull,
reason: '行程只是回到派單中,顯示「已取消」會讓乘客以為要重新叫車');
});

test('新司機接單後通知消失', () {
redispatch();
expect(ctrl.redispatchNotice, isNotNull);

ctrl.handleWsEventForTest(FleetWsEvent(
type: FleetEventTypes.rideAccepted,
rideId: 9,
payload: {'driver_name': '阿華', 'driver_plate_number': 'NEW-0001'},
));

expect(ctrl.redispatchNotice, isNull);
expect(ctrl.driverName, '阿華');
});

test('重派也失敗真的取消時,通知換成取消訊息', () {
redispatch();
ctrl.handleWsEventForTest(FleetWsEvent(
type: FleetEventTypes.rideCancelled,
rideId: 9,
payload: {'cancel_reason': 'no_driver_available'},
));

expect(ctrl.redispatchNotice, isNull);
expect(ctrl.cancelNotice, isNotNull);
});

testWidgets('配對中畫面要把這則說明顯示出來(不能設了沒人讀)', (tester) async {
redispatch();

await tester.pumpWidget(ChangeNotifierProvider.value(
value: ctrl,
child: MaterialApp(
theme: appLightTheme,
home: Scaffold(body: SearchingContent(ctrl: ctrl)),
),
));
await tester.pump();

expect(find.text('司機取消了行程,正在為您重新派車'), findsOneWidget);
expect(find.text('正在為您配對司機'), findsOneWidget);

ctrl.dispose();
disposed = true;
});
}

class _StubApi extends CustomerApiClient {
_StubApi() : super(dio: Dio(BaseOptions(baseUrl: 'http://test.invalid/api')));

@override
Future<CustomerRide?> activeRide() async =>
const CustomerRide(rideId: 9, status: RideStatus.requested);
}
Loading