Skip to content

Commit 3965b14

Browse files
committed
Initial refactor
1 parent e6da0c3 commit 3965b14

20 files changed

Lines changed: 3504 additions & 402 deletions

File tree

app/lib/config/backend_config.dart

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:flutter_dotenv/flutter_dotenv.dart';
3+
import '../services/logger.dart';
4+
5+
/// Configuration for the BijbelQuiz Backend API Proxy
6+
///
7+
/// This class manages the configuration for routing API requests through
8+
/// the secure backend proxy at backend.bijbelquiz.app
9+
class BackendConfig {
10+
static String? _baseUrl;
11+
static String? _authToken;
12+
static bool _initialized = false;
13+
static bool _isExplicitlyConfigured = false;
14+
15+
/// Default production backend URL
16+
static const String defaultBackendUrl = 'https://backend.bijbelquiz.app';
17+
18+
/// Backend base URL
19+
static String get baseUrl {
20+
if (_baseUrl != null) return _baseUrl!;
21+
22+
// Try to load from environment
23+
final envUrl = dotenv.env['BACKEND_URL'];
24+
if (envUrl != null && envUrl.isNotEmpty) {
25+
return envUrl;
26+
}
27+
28+
// Default production URL
29+
return defaultBackendUrl;
30+
}
31+
32+
/// Check if backend is explicitly configured (not just using defaults)
33+
static bool get isConfigured => _isExplicitlyConfigured;
34+
35+
/// Gemini API endpoint
36+
static String get geminiEndpoint => '$baseUrl/api/gemini';
37+
38+
/// Supabase API endpoint
39+
static String get supabaseEndpoint => '$baseUrl/api/supabase';
40+
41+
/// PostHog API endpoint
42+
static String get posthogEndpoint => '$baseUrl/api/posthog';
43+
44+
/// Health check endpoint
45+
static String get healthEndpoint => '$baseUrl/api/health';
46+
47+
/// Initialize the backend configuration
48+
static Future<void> initialize() async {
49+
if (_initialized) return;
50+
51+
AppLogger.info('Initializing Backend configuration...');
52+
53+
// Load from environment variables
54+
_baseUrl = dotenv.env['BACKEND_URL'];
55+
56+
if (_baseUrl == null || _baseUrl!.isEmpty) {
57+
AppLogger.warning('BACKEND_URL not set in environment, using default');
58+
_baseUrl = defaultBackendUrl;
59+
} else {
60+
// Mark as explicitly configured when URL comes from environment
61+
_isExplicitlyConfigured = true;
62+
}
63+
64+
AppLogger.info('Backend URL configured: $_baseUrl');
65+
_initialized = true;
66+
}
67+
68+
/// Reset configuration for testing purposes
69+
@visibleForTesting
70+
static void reset() {
71+
_baseUrl = null;
72+
_authToken = null;
73+
_initialized = false;
74+
_isExplicitlyConfigured = false;
75+
}
76+
77+
/// Set the authentication token for API requests
78+
static void setAuthToken(String? token) {
79+
_authToken = token;
80+
AppLogger.info(token != null
81+
? 'Auth token set'
82+
: 'Auth token cleared');
83+
}
84+
85+
/// Get the current authentication token
86+
static Future<String?> getAuthToken() async {
87+
// Return cached token if available
88+
if (_authToken != null) return _authToken;
89+
90+
// Otherwise, try to get from secure storage or Supabase auth
91+
// This would typically integrate with your auth service
92+
return null;
93+
}
94+
95+
/// Clear the authentication token
96+
static void clearAuthToken() {
97+
_authToken = null;
98+
AppLogger.info('Auth token cleared');
99+
}
100+
101+
/// Check backend health
102+
///
103+
/// Note: This method is not yet fully implemented. It currently throws
104+
/// [UnimplementedError] to indicate that a real health check should be
105+
/// implemented using an HTTP GET to the backend health endpoint.
106+
static Future<bool> checkHealth() async {
107+
throw UnimplementedError(
108+
'Health check is not implemented. '
109+
'Implement by making an HTTP GET request to $healthEndpoint '
110+
'and returning true only on a successful 200 response.'
111+
);
112+
}
113+
}

app/lib/services/analytics_service.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'logger.dart';
88
import 'tracking_service.dart';
99
import '../utils/automatic_error_reporter.dart';
1010
import 'package:uuid/uuid.dart';
11-
import 'package:posthog_flutter/posthog_flutter.dart';
1211

1312
/// A service that provides an interface to the in-house tracking service.
1413
///
@@ -42,8 +41,8 @@ class AnalyticsService {
4241
}
4342
}
4443

45-
/// Returns a [PosthogObserver] that can be used to automatically track screen views.
46-
PosthogObserver getObserver() => _trackingService.getObserver();
44+
/// Returns a [RouteObserver] that can be used to automatically track screen views.
45+
RouteObserver<ModalRoute<void>> getObserver() => _trackingService.getObserver();
4746

4847
/// Tracks a screen view.
4948
///

0 commit comments

Comments
 (0)