chore: Flutter mobile app, CI, and dev tooling
- mobile/: Flutter/Dart merchant mobile app skeleton - .github/: GitHub Actions CI workflows - .dockerignore: exclude host node_modules from build context - .cursorrules: Cursor IDE project rules - .claude/: Claude Code project settings and launch config Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../core/api/api_client.dart';
|
||||
import '../../core/auth/auth_provider.dart';
|
||||
import '../../core/hub/hub_provider.dart';
|
||||
import 'waiter_notification.dart';
|
||||
|
||||
// ── Local notification setup ────────────────────────────────────────────────
|
||||
final _localNotifications = FlutterLocalNotificationsPlugin();
|
||||
|
||||
Future<void> initLocalNotifications() async {
|
||||
const android = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const ios = DarwinInitializationSettings(
|
||||
requestAlertPermission: true,
|
||||
requestBadgePermission: true,
|
||||
requestSoundPermission: true,
|
||||
);
|
||||
await _localNotifications.initialize(
|
||||
const InitializationSettings(android: android, iOS: ios),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showLocalNotification(WaiterNotification n) async {
|
||||
const android = AndroidNotificationDetails(
|
||||
'meezi_waiter_channel',
|
||||
'میزی — گارسون',
|
||||
channelDescription: 'اعلانهای میزی برای گارسون',
|
||||
importance: Importance.max,
|
||||
priority: Priority.high,
|
||||
playSound: true,
|
||||
);
|
||||
const ios = DarwinNotificationDetails(presentSound: true, presentAlert: true);
|
||||
await _localNotifications.show(
|
||||
n.createdAt.millisecondsSinceEpoch ~/ 1000,
|
||||
n.title,
|
||||
n.body,
|
||||
const NotificationDetails(android: android, iOS: ios),
|
||||
);
|
||||
}
|
||||
|
||||
// ── Notification state ───────────────────────────────────────────────────────
|
||||
class NotificationState {
|
||||
const NotificationState({
|
||||
this.items = const [],
|
||||
this.isLoading = false,
|
||||
});
|
||||
|
||||
final List<WaiterNotification> items;
|
||||
final bool isLoading;
|
||||
|
||||
int get unreadCount => items.where((n) => !n.isRead).length;
|
||||
|
||||
NotificationState copyWith({
|
||||
List<WaiterNotification>? items,
|
||||
bool? isLoading,
|
||||
}) =>
|
||||
NotificationState(
|
||||
items: items ?? this.items,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
);
|
||||
}
|
||||
|
||||
final notificationProvider =
|
||||
StateNotifierProvider<NotificationNotifier, NotificationState>((ref) {
|
||||
final notifier = NotificationNotifier(ref);
|
||||
notifier._init();
|
||||
return notifier;
|
||||
});
|
||||
|
||||
class NotificationNotifier extends StateNotifier<NotificationState> {
|
||||
NotificationNotifier(this._ref) : super(const NotificationState());
|
||||
|
||||
final Ref _ref;
|
||||
StreamSubscription<dynamic>? _hubSub;
|
||||
|
||||
void _init() {
|
||||
_fetchFromApi();
|
||||
_subscribeHub();
|
||||
}
|
||||
|
||||
void _subscribeHub() {
|
||||
_hubSub?.cancel();
|
||||
final hub = _ref.read(hubClientProvider);
|
||||
if (hub == null) return;
|
||||
|
||||
_hubSub = hub.notifications.listen((event) {
|
||||
final n = WaiterNotification.fromHub(event);
|
||||
state = state.copyWith(items: [n, ...state.items]);
|
||||
_showLocalNotification(n);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _fetchFromApi() async {
|
||||
final session = _ref.read(authProvider);
|
||||
if (session == null) return;
|
||||
state = state.copyWith(isLoading: true);
|
||||
try {
|
||||
final client = _ref.read(apiClientProvider);
|
||||
final res = await client.dio.get<Map<String, dynamic>>(
|
||||
'/api/cafes/${session.cafeId}/notifications',
|
||||
queryParameters: {'limit': 50},
|
||||
);
|
||||
final raw = res.data?['data'] as List?;
|
||||
final items = raw
|
||||
?.map((e) =>
|
||||
WaiterNotification.fromJson(Map<String, dynamic>.from(e as Map)))
|
||||
.toList() ??
|
||||
[];
|
||||
state = state.copyWith(items: items, isLoading: false);
|
||||
} catch (_) {
|
||||
state = state.copyWith(isLoading: false);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refresh() => _fetchFromApi();
|
||||
|
||||
Future<void> markRead(String id) async {
|
||||
final session = _ref.read(authProvider);
|
||||
if (session == null) return;
|
||||
try {
|
||||
final client = _ref.read(apiClientProvider);
|
||||
await client.dio.post(
|
||||
'/api/cafes/${session.cafeId}/notifications/read',
|
||||
data: {'ids': [id]},
|
||||
);
|
||||
} catch (_) {}
|
||||
final updated = state.items.map((n) {
|
||||
if (n.id == id) n.isRead = true;
|
||||
return n;
|
||||
}).toList();
|
||||
state = state.copyWith(items: updated);
|
||||
}
|
||||
|
||||
Future<void> markAllRead() async {
|
||||
final session = _ref.read(authProvider);
|
||||
if (session == null) return;
|
||||
try {
|
||||
final client = _ref.read(apiClientProvider);
|
||||
await client.dio.post(
|
||||
'/api/cafes/${session.cafeId}/notifications/read',
|
||||
data: {'all': true},
|
||||
);
|
||||
} catch (_) {}
|
||||
final updated = state.items.map((n) {
|
||||
n.isRead = true;
|
||||
return n;
|
||||
}).toList();
|
||||
state = state.copyWith(items: updated);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_hubSub?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:shamsi_date/shamsi_date.dart';
|
||||
|
||||
import 'notification_provider.dart';
|
||||
import 'waiter_notification.dart';
|
||||
|
||||
class NotificationsScreen extends ConsumerWidget {
|
||||
const NotificationsScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final state = ref.watch(notificationProvider);
|
||||
final notifier = ref.read(notificationProvider.notifier);
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('اعلانها'),
|
||||
actions: [
|
||||
if (state.unreadCount > 0)
|
||||
TextButton(
|
||||
onPressed: notifier.markAllRead,
|
||||
child: const Text('همه خوانده شد'),
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.refresh),
|
||||
tooltip: 'بارگذاری مجدد',
|
||||
onPressed: notifier.refresh,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: state.isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: state.items.isEmpty
|
||||
? Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.notifications_none,
|
||||
size: 56,
|
||||
color: theme.colorScheme.onSurface.withOpacity(0.3)),
|
||||
const SizedBox(height: 12),
|
||||
const Text('اعلانی وجود ندارد',
|
||||
style: TextStyle(fontSize: 15)),
|
||||
],
|
||||
),
|
||||
)
|
||||
: RefreshIndicator(
|
||||
onRefresh: notifier.refresh,
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
itemCount: state.items.length,
|
||||
itemBuilder: (_, i) {
|
||||
final n = state.items[i];
|
||||
return _NotificationTile(
|
||||
notification: n,
|
||||
onTap: () => notifier.markRead(n.id),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _NotificationTile extends StatelessWidget {
|
||||
const _NotificationTile({
|
||||
required this.notification,
|
||||
required this.onTap,
|
||||
});
|
||||
|
||||
final WaiterNotification notification;
|
||||
final VoidCallback onTap;
|
||||
|
||||
IconData get _icon {
|
||||
if (notification.isCallWaiter) return Icons.notifications_active;
|
||||
if (notification.isNewOrder) return Icons.restaurant;
|
||||
if (notification.isOrderReady) return Icons.check_circle_outline;
|
||||
return Icons.notifications;
|
||||
}
|
||||
|
||||
Color _iconColor(BuildContext context) {
|
||||
final cs = Theme.of(context).colorScheme;
|
||||
if (notification.isCallWaiter) return cs.error;
|
||||
if (notification.isNewOrder) return cs.primary;
|
||||
if (notification.isOrderReady) return Colors.green;
|
||||
return cs.onSurfaceVariant;
|
||||
}
|
||||
|
||||
String get _timeLabel {
|
||||
try {
|
||||
final j = Jalali.fromDateTime(notification.createdAt.toLocal());
|
||||
final f = j.formatter;
|
||||
return '${f.d} ${f.mN} — ${notification.createdAt.toLocal().hour.toString().padLeft(2, '0')}:${notification.createdAt.toLocal().minute.toString().padLeft(2, '0')}';
|
||||
} catch (_) {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final isUnread = !notification.isRead;
|
||||
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
child: Material(
|
||||
color: isUnread
|
||||
? theme.colorScheme.primaryContainer.withOpacity(0.25)
|
||||
: theme.colorScheme.surface,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
onTap: onTap,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(14),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Container(
|
||||
width: 42,
|
||||
height: 42,
|
||||
decoration: BoxDecoration(
|
||||
color: _iconColor(context).withOpacity(0.12),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Icon(_icon,
|
||||
color: _iconColor(context), size: 22),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
notification.title,
|
||||
style: theme.textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: isUnread
|
||||
? FontWeight.bold
|
||||
: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (isUnread)
|
||||
Container(
|
||||
width: 8,
|
||||
height: 8,
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.primary,
|
||||
shape: BoxShape.circle,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (notification.body != null) ...[
|
||||
const SizedBox(height: 3),
|
||||
Text(
|
||||
notification.body!,
|
||||
style: theme.textTheme.bodySmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 4),
|
||||
Row(
|
||||
children: [
|
||||
if (notification.tableNumber != null) ...[
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 8, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.surfaceContainerHighest,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: Text(
|
||||
'میز ${notification.tableNumber}',
|
||||
style: theme.textTheme.labelSmall,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 6),
|
||||
],
|
||||
Text(
|
||||
_timeLabel,
|
||||
style: theme.textTheme.labelSmall?.copyWith(
|
||||
color: theme.colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import '../../core/hub/hub_client.dart';
|
||||
|
||||
class WaiterNotification {
|
||||
WaiterNotification({
|
||||
required this.id,
|
||||
required this.type,
|
||||
required this.title,
|
||||
this.body,
|
||||
this.tableNumber,
|
||||
this.referenceId,
|
||||
required this.createdAt,
|
||||
this.isRead = false,
|
||||
});
|
||||
|
||||
final String id;
|
||||
final String type;
|
||||
final String title;
|
||||
final String? body;
|
||||
final String? tableNumber;
|
||||
final String? referenceId;
|
||||
final DateTime createdAt;
|
||||
bool isRead;
|
||||
|
||||
factory WaiterNotification.fromHub(HubNotification n) => WaiterNotification(
|
||||
id: n.id,
|
||||
type: n.type,
|
||||
title: n.title,
|
||||
body: n.body,
|
||||
tableNumber: n.tableNumber,
|
||||
referenceId: n.referenceId,
|
||||
createdAt: n.createdAt,
|
||||
);
|
||||
|
||||
factory WaiterNotification.fromJson(Map<String, dynamic> json) =>
|
||||
WaiterNotification(
|
||||
id: json['id'] as String,
|
||||
type: json['type'] as String,
|
||||
title: json['title'] as String,
|
||||
body: json['body'] as String?,
|
||||
tableNumber: json['tableNumber'] as String?,
|
||||
referenceId: json['referenceId'] as String?,
|
||||
createdAt:
|
||||
DateTime.tryParse(json['createdAt'] as String? ?? '') ??
|
||||
DateTime.now(),
|
||||
isRead: json['isRead'] as bool? ?? false,
|
||||
);
|
||||
|
||||
bool get isCallWaiter => type == 'table_call_waiter';
|
||||
bool get isNewOrder => type == 'guest_order_new';
|
||||
bool get isOrderReady => type == 'guest_order_ready';
|
||||
}
|
||||
Reference in New Issue
Block a user