feat: add alot

This commit is contained in:
2026-10-08 04:12:36 +02:00
parent ccf5165417
commit fc607cb7bd
99 changed files with 4981 additions and 3655 deletions
+39 -67
View File
@@ -1,18 +1,23 @@
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import '../models/venue.dart';
import '../services/kooltap_api_client.dart';
import '../services/remote_sync_service.dart';
import '../utils/app_config.dart';
enum RegistrationStep { initial, loadingVenues, venuesLoaded, error, registering, registered }
enum RegistrationStep {
initial,
loadingVenues,
venuesLoaded,
error,
registering,
registered,
}
class RegistrationViewModel extends ChangeNotifier {
final KoolTapApiClient apiClient;
final RemoteSyncService syncService;
RegistrationViewModel({required this.syncService});
RegistrationViewModel({required this.apiClient, required this.syncService});
RegistrationStep _step = RegistrationStep.initial;
RegistrationStep get step => _step;
@@ -23,6 +28,7 @@ class RegistrationViewModel extends ChangeNotifier {
Venue? _selectedVenue;
Venue? get selectedVenue => _selectedVenue;
/// Server-provided, so shown as-is.
String? _errorMessage;
String? get errorMessage => _errorMessage;
@@ -37,21 +43,9 @@ class RegistrationViewModel extends ChangeNotifier {
notifyListeners();
try {
final response = await http
.get(Uri.parse('$kRemoteServerUrl/venues/'))
.timeout(const Duration(seconds: 15));
if (response.statusCode == 200) {
final List<dynamic> data = jsonDecode(response.body) as List<dynamic>;
_venues = data
.map((e) => Venue.fromJson(e as Map<String, dynamic>))
.toList();
_step = RegistrationStep.venuesLoaded;
} else {
_errorMessage = 'Failed to load venues (HTTP ${response.statusCode})';
_step = RegistrationStep.error;
}
} catch (e) {
_venues = await apiClient.getVenues();
_step = RegistrationStep.venuesLoaded;
} on Exception catch (e) {
debugPrint('RegistrationViewModel: fetchVenues error: $e');
_errorMessage = e.toString();
_step = RegistrationStep.error;
@@ -72,58 +66,36 @@ class RegistrationViewModel extends ChangeNotifier {
notifyListeners();
try {
final response = await http
.post(
Uri.parse('$kRemoteServerUrl/devices/register'),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({
'venueId': venue.id,
'name': deviceName,
'registrationCode': registrationCode,
}),
)
.timeout(const Duration(seconds: 15));
final credentials = await apiClient.registerDevice(
venueId: venue.id,
deviceName: deviceName,
registrationCode: registrationCode,
);
if (response.statusCode == 201) {
final data = jsonDecode(response.body) as Map<String, dynamic>;
final deviceId = data['deviceId'] as String;
final token = data['token'] as String;
await syncService.setCredentials(
deviceId: credentials.deviceId,
token: credentials.token,
venueId: venue.id,
);
await syncService.setCredentials(
deviceId: deviceId,
token: token,
venueId: venue.id,
serverUrl: kRemoteServerUrl,
);
_step = RegistrationStep.registered;
notifyListeners();
return true;
} else {
String message;
try {
final errorData = jsonDecode(response.body) as Map<String, dynamic>;
message = errorData['message'] as String? ?? 'Registration failed';
} catch (_) {
message = 'Registration failed (HTTP ${response.statusCode})';
}
_errorMessage = message;
_step = RegistrationStep.venuesLoaded;
notifyListeners();
return false;
}
} catch (e) {
_step = RegistrationStep.registered;
notifyListeners();
return true;
} on ApiException catch (e) {
_errorMessage = e.message.isEmpty
? 'Registration failed (HTTP ${e.statusCode})'
: e.message;
} on Exception catch (e) {
debugPrint('RegistrationViewModel: registerDevice error: $e');
_errorMessage = e.toString();
_step = RegistrationStep.venuesLoaded;
notifyListeners();
return false;
}
_step = RegistrationStep.venuesLoaded;
notifyListeners();
return false;
}
Future<void> retry() async {
await fetchVenues();
}
Future<void> retry() => fetchVenues();
void reset() {
_step = RegistrationStep.initial;
@@ -132,4 +104,4 @@ class RegistrationViewModel extends ChangeNotifier {
_errorMessage = null;
notifyListeners();
}
}
}