* Workforce, Schedule generation and geofencing.

This commit is contained in:
Marc Rejohn Castillano 2026-02-09 22:19:31 +08:00
parent 1f16da8f88
commit f4dea74394
14 changed files with 2021 additions and 11 deletions

View File

@ -1,4 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:label="tasq"
android:name="${applicationName}"

View File

@ -24,6 +24,8 @@
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSLocationWhenInUseUsageDescription</key>
<string>TasQ uses your location to verify on-site check-ins.</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>

View File

@ -0,0 +1,33 @@
class GeofenceConfig {
GeofenceConfig({
required this.lat,
required this.lng,
required this.radiusMeters,
});
final double lat;
final double lng;
final double radiusMeters;
factory GeofenceConfig.fromJson(Map<String, dynamic> json) {
return GeofenceConfig(
lat: (json['lat'] as num).toDouble(),
lng: (json['lng'] as num).toDouble(),
radiusMeters: (json['radius_m'] as num).toDouble(),
);
}
}
class AppSetting {
AppSetting({required this.key, required this.value});
final String key;
final Map<String, dynamic> value;
factory AppSetting.fromMap(Map<String, dynamic> map) {
return AppSetting(
key: map['key'] as String,
value: Map<String, dynamic>.from(map['value'] as Map),
);
}
}

View File

@ -0,0 +1,39 @@
class DutySchedule {
DutySchedule({
required this.id,
required this.userId,
required this.shiftType,
required this.startTime,
required this.endTime,
required this.status,
required this.createdAt,
required this.checkInAt,
required this.checkInLocation,
});
final String id;
final String userId;
final String shiftType;
final DateTime startTime;
final DateTime endTime;
final String status;
final DateTime createdAt;
final DateTime? checkInAt;
final Object? checkInLocation;
factory DutySchedule.fromMap(Map<String, dynamic> map) {
return DutySchedule(
id: map['id'] as String,
userId: map['user_id'] as String,
shiftType: map['shift_type'] as String? ?? 'normal',
startTime: DateTime.parse(map['start_time'] as String),
endTime: DateTime.parse(map['end_time'] as String),
status: map['status'] as String? ?? 'scheduled',
createdAt: DateTime.parse(map['created_at'] as String),
checkInAt: map['check_in_at'] == null
? null
: DateTime.parse(map['check_in_at'] as String),
checkInLocation: map['check_in_location'],
);
}
}

View File

@ -0,0 +1,36 @@
class SwapRequest {
SwapRequest({
required this.id,
required this.requesterId,
required this.recipientId,
required this.shiftId,
required this.status,
required this.createdAt,
required this.updatedAt,
required this.approvedBy,
});
final String id;
final String requesterId;
final String recipientId;
final String shiftId;
final String status;
final DateTime createdAt;
final DateTime? updatedAt;
final String? approvedBy;
factory SwapRequest.fromMap(Map<String, dynamic> map) {
return SwapRequest(
id: map['id'] as String,
requesterId: map['requester_id'] as String,
recipientId: map['recipient_id'] as String,
shiftId: map['shift_id'] as String,
status: map['status'] as String? ?? 'pending',
createdAt: DateTime.parse(map['created_at'] as String),
updatedAt: map['updated_at'] == null
? null
: DateTime.parse(map['updated_at'] as String),
approvedBy: map['approved_by'] as String?,
);
}
}

View File

@ -0,0 +1,139 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import '../models/app_settings.dart';
import '../models/duty_schedule.dart';
import '../models/swap_request.dart';
import 'profile_provider.dart';
import 'supabase_provider.dart';
final geofenceProvider = FutureProvider<GeofenceConfig?>((ref) async {
final client = ref.watch(supabaseClientProvider);
final data = await client
.from('app_settings')
.select()
.eq('key', 'geofence')
.maybeSingle();
if (data == null) return null;
final setting = AppSetting.fromMap(data);
return GeofenceConfig.fromJson(setting.value);
});
final dutySchedulesProvider = StreamProvider<List<DutySchedule>>((ref) {
final client = ref.watch(supabaseClientProvider);
final profileAsync = ref.watch(currentProfileProvider);
final profile = profileAsync.valueOrNull;
if (profile == null) {
return Stream.value(const <DutySchedule>[]);
}
final isAdmin = profile.role == 'admin' || profile.role == 'dispatcher';
final base = client.from('duty_schedules').stream(primaryKey: ['id']);
if (isAdmin) {
return base
.order('start_time')
.map((rows) => rows.map(DutySchedule.fromMap).toList());
}
return base
.eq('user_id', profile.id)
.order('start_time')
.map((rows) => rows.map(DutySchedule.fromMap).toList());
});
final swapRequestsProvider = StreamProvider<List<SwapRequest>>((ref) {
final client = ref.watch(supabaseClientProvider);
final profileAsync = ref.watch(currentProfileProvider);
final profile = profileAsync.valueOrNull;
if (profile == null) {
return Stream.value(const <SwapRequest>[]);
}
final isAdmin = profile.role == 'admin' || profile.role == 'dispatcher';
final base = client.from('swap_requests').stream(primaryKey: ['id']);
if (isAdmin) {
return base
.order('created_at', ascending: false)
.map((rows) => rows.map(SwapRequest.fromMap).toList());
}
return base
.order('created_at', ascending: false)
.map(
(rows) => rows
.where(
(row) =>
row['requester_id'] == profile.id ||
row['recipient_id'] == profile.id,
)
.map(SwapRequest.fromMap)
.toList(),
);
});
final workforceControllerProvider = Provider<WorkforceController>((ref) {
final client = ref.watch(supabaseClientProvider);
return WorkforceController(client);
});
class WorkforceController {
WorkforceController(this._client);
final SupabaseClient _client;
Future<void> generateSchedule({
required DateTime startDate,
required DateTime endDate,
}) async {
await _client.rpc(
'generate_duty_schedule',
params: {
'start_date': _formatDate(startDate),
'end_date': _formatDate(endDate),
},
);
}
Future<void> insertSchedules(List<Map<String, dynamic>> schedules) async {
if (schedules.isEmpty) return;
await _client.from('duty_schedules').insert(schedules);
}
Future<String?> checkIn({
required String dutyScheduleId,
required double lat,
required double lng,
}) async {
final data = await _client.rpc(
'duty_check_in',
params: {'p_duty_id': dutyScheduleId, 'p_lat': lat, 'p_lng': lng},
);
return data as String?;
}
Future<String?> requestSwap({
required String shiftId,
required String recipientId,
}) async {
final data = await _client.rpc(
'request_shift_swap',
params: {'p_shift_id': shiftId, 'p_recipient_id': recipientId},
);
return data as String?;
}
Future<void> respondSwap({
required String swapId,
required String action,
}) async {
await _client.rpc(
'respond_shift_swap',
params: {'p_swap_id': swapId, 'p_action': action},
);
}
String _formatDate(DateTime value) {
final date = DateTime(value.year, value.month, value.day);
final month = date.month.toString().padLeft(2, '0');
final day = date.day.toString().padLeft(2, '0');
return '${date.year}-$month-$day';
}
}

View File

@ -15,6 +15,7 @@ import '../screens/tasks/task_detail_screen.dart';
import '../screens/tasks/tasks_list_screen.dart';
import '../screens/tickets/ticket_detail_screen.dart';
import '../screens/tickets/tickets_list_screen.dart';
import '../screens/workforce/workforce_screen.dart';
import '../widgets/app_shell.dart';
final appRouterProvider = Provider<GoRouter>((ref) {
@ -105,11 +106,7 @@ final appRouterProvider = Provider<GoRouter>((ref) {
),
GoRoute(
path: '/workforce',
builder: (context, state) => const UnderDevelopmentScreen(
title: 'Workforce',
subtitle: 'Workforce management is in progress.',
icon: Icons.groups,
),
builder: (context, state) => const WorkforceScreen(),
),
GoRoute(
path: '/reports',

View File

@ -177,8 +177,9 @@ class _UserManagementScreenState extends ConsumerState<UserManagementScreen> {
columnSpacing: 24,
horizontalMargin: 16,
dividerThickness: 1,
headingRowColor: MaterialStateProperty.resolveWith(
(states) => Theme.of(context).colorScheme.surfaceVariant,
headingRowColor: WidgetStateProperty.resolveWith(
(states) =>
Theme.of(context).colorScheme.surfaceContainerHighest,
),
columns: const [
DataColumn(label: Text('User')),
@ -228,16 +229,16 @@ class _UserManagementScreenState extends ConsumerState<UserManagementScreen> {
if (selected != true) return;
_showUserDialog(context, profile, offices, assignments);
},
color: MaterialStateProperty.resolveWith((states) {
if (states.contains(MaterialState.selected)) {
color: WidgetStateProperty.resolveWith((states) {
if (states.contains(WidgetState.selected)) {
return Theme.of(
context,
).colorScheme.surfaceTint.withOpacity(0.12);
).colorScheme.surfaceTint.withValues(alpha: 0.12);
}
if (index.isEven) {
return Theme.of(
context,
).colorScheme.surface.withOpacity(0.6);
).colorScheme.surface.withValues(alpha: 0.6);
}
return Theme.of(context).colorScheme.surface;
}),

File diff suppressed because it is too large Load Diff

View File

@ -7,12 +7,14 @@ import Foundation
import app_links
import audioplayers_darwin
import geolocator_apple
import shared_preferences_foundation
import url_launcher_macos
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AppLinksMacosPlugin.register(with: registry.registrar(forPlugin: "AppLinksMacosPlugin"))
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}

View File

@ -304,6 +304,54 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.5.0"
geolocator:
dependency: "direct main"
description:
name: geolocator
sha256: f62bcd90459e63210bbf9c35deb6a51c521f992a78de19a1fe5c11704f9530e2
url: "https://pub.dev"
source: hosted
version: "13.0.4"
geolocator_android:
dependency: transitive
description:
name: geolocator_android
sha256: fcb1760a50d7500deca37c9a666785c047139b5f9ee15aa5469fae7dbbe3170d
url: "https://pub.dev"
source: hosted
version: "4.6.2"
geolocator_apple:
dependency: transitive
description:
name: geolocator_apple
sha256: dbdd8789d5aaf14cf69f74d4925ad1336b4433a6efdf2fce91e8955dc921bf22
url: "https://pub.dev"
source: hosted
version: "2.3.13"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
sha256: "30cb64f0b9adcc0fb36f628b4ebf4f731a2961a0ebd849f4b56200205056fe67"
url: "https://pub.dev"
source: hosted
version: "4.2.6"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
sha256: b1ae9bdfd90f861fde8fd4f209c37b953d65e92823cb73c7dee1fa021b06f172
url: "https://pub.dev"
source: hosted
version: "4.1.3"
geolocator_windows:
dependency: transitive
description:
name: geolocator_windows
sha256: "175435404d20278ffd220de83c2ca293b73db95eafbdc8131fe8609be1421eb6"
url: "https://pub.dev"
source: hosted
version: "0.2.5"
glob:
dependency: transitive
description:

View File

@ -16,6 +16,7 @@ dependencies:
font_awesome_flutter: ^10.7.0
google_fonts: ^6.2.1
audioplayers: ^6.1.0
geolocator: ^13.0.1
dev_dependencies:
flutter_test:

View File

@ -8,6 +8,7 @@
#include <app_links/app_links_plugin_c_api.h>
#include <audioplayers_windows/audioplayers_windows_plugin.h>
#include <geolocator_windows/geolocator_windows.h>
#include <url_launcher_windows/url_launcher_windows.h>
void RegisterPlugins(flutter::PluginRegistry* registry) {
@ -15,6 +16,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) {
registry->GetRegistrarForPlugin("AppLinksPluginCApi"));
AudioplayersWindowsPluginRegisterWithRegistrar(
registry->GetRegistrarForPlugin("AudioplayersWindowsPlugin"));
GeolocatorWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("GeolocatorWindows"));
UrlLauncherWindowsRegisterWithRegistrar(
registry->GetRegistrarForPlugin("UrlLauncherWindows"));
}

View File

@ -5,6 +5,7 @@
list(APPEND FLUTTER_PLUGIN_LIST
app_links
audioplayers_windows
geolocator_windows
url_launcher_windows
)