mac OS compatibility

This commit is contained in:
2024-11-08 23:20:09 +01:00
parent c03e9556b7
commit c7233daec7
4 changed files with 64 additions and 7 deletions

View File

@@ -1,3 +1,5 @@
// ignore_for_file: prefer_const_constructors
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'comparison_page.dart';
@@ -5,6 +7,7 @@ import 'final_clearance_page.dart'; // Import final clearance page
import '../utils/frequency_input_formatter.dart'; // Ensure this path is correct
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
class ExpectationInputPage extends StatefulWidget {
final bool isDarkMode;
@@ -41,6 +44,38 @@ class ExpectationInputPageState extends State<ExpectationInputPage> {
final FocusNode _frequencyFocusNode = FocusNode();
final FocusNode _squawkFocusNode = FocusNode();
// Neue Variablen
bool _saveSimbriefId = false;
static const String _simbriefIdKey = 'simbrief_id';
@override
void initState() {
super.initState();
_loadSavedSimbriefId();
}
// Methode zum Laden der gespeicherten ID
Future<void> _loadSavedSimbriefId() async {
final prefs = await SharedPreferences.getInstance();
final savedId = prefs.getString(_simbriefIdKey);
if (savedId != null) {
setState(() {
_simbriefIdController.text = savedId;
_saveSimbriefId = true;
});
}
}
// Methode zum Speichern der ID
Future<void> _saveSimbriefIdToPrefs() async {
final prefs = await SharedPreferences.getInstance();
if (_saveSimbriefId) {
await prefs.setString(_simbriefIdKey, _simbriefIdController.text);
} else {
await prefs.remove(_simbriefIdKey);
}
}
@override
void dispose() {
_expectedClearanceLimitController.dispose();
@@ -58,34 +93,36 @@ class ExpectationInputPageState extends State<ExpectationInputPage> {
Future<void> _fetchSimbriefData() async {
try {
if (_saveSimbriefId) {
await _saveSimbriefIdToPrefs();
}
final response = await http.get(
Uri.parse('https://www.simbrief.com/api/xml.fetcher.php?userid=${_simbriefIdController.text}&json=1'),
);
// Prüfe ob Widget noch mounted ist
if (!mounted) return;
if (response.statusCode == 200) {
final data = json.decode(response.body);
// Debug-Ausgabe
print('SimBrief API Response: ${response.body}');
// Extrahiere die komplette Route
String fullRoute = data['general']['route'] ?? '';
// Hole nur den ersten Teil der Route (SID)
String sid = fullRoute.split(' ').first;
setState(() {
_expectedClearanceLimitController.text = data['destination']['icao_code'] ?? '';
_expectedRouteController.text = sid;
// Altitude und Frequency bleiben leer
_expectedAltitudeController.text = '';
_expectedFrequencyController.text = '';
});
if (!mounted) return; // Zweiter Check vor ScaffoldMessenger
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('SimBrief Daten erfolgreich geladen')),
);
}
} catch (e) {
if (!mounted) return; // Check vor Error-SnackBar
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Fehler beim Laden der SimBrief Daten: $e')),
);
@@ -273,6 +310,20 @@ class ExpectationInputPageState extends State<ExpectationInputPage> {
),
),
const SizedBox(height: 10),
Row(
children: [
Checkbox(
value: _saveSimbriefId,
onChanged: (bool? value) {
setState(() {
_saveSimbriefId = value ?? false;
});
_saveSimbriefIdToPrefs();
},
),
const Text('Save SimBrief ID'),
],
),
SizedBox(
width: double.infinity,
child: ElevatedButton(
@@ -303,7 +354,7 @@ class ExpectationInputPageState extends State<ExpectationInputPage> {
),
const SizedBox(height: 10),
const Text(
'Enter your expected clearance information below. '
'Enter your expected clearance information below (Or use the SimBrief import). '
'Use the listening page or skip to the readback page.',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.left,