import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:cashumit/services/firefly_api_service.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { final _formKey = GlobalKey(); final _urlController = TextEditingController(); final _tokenController = TextEditingController(); bool _isTestingConnection = false; bool _isTestingAuth = false; @override void initState() { super.initState(); _loadSettings(); } @override void dispose() { _urlController.dispose(); _tokenController.dispose(); super.dispose(); } Future _loadSettings() async { final prefs = await SharedPreferences.getInstance(); // Tambahkan pengecekan mounted sebelum setState if (mounted) { setState(() { _urlController.text = prefs.getString('firefly_url') ?? ''; _tokenController.text = prefs.getString('firefly_token') ?? ''; }); } } Future _saveSettings() async { if (_formKey.currentState!.validate()) { final prefs = await SharedPreferences.getInstance(); await prefs.setString('firefly_url', _urlController.text.trim()); await prefs.setString('firefly_token', _tokenController.text.trim()); if (mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Pengaturan berhasil disimpan')), ); } } } Future _testConnection() async { // Tambahkan pengecekan mounted if (!mounted) return; setState(() { _isTestingConnection = true; }); // Simpan pengaturan terlebih dahulu await _saveSettings(); // Uji koneksi final success = await FireflyApiService.testConnection(baseUrl: _urlController.text.trim()); // Tambahkan pengecekan mounted sebelum setState if (mounted) { setState(() { _isTestingConnection = false; }); if (success) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Koneksi berhasil!')), ); } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Koneksi gagal. Periksa URL dan pastikan Firefly III berjalan.')), ); } } } Future _testAuthentication() async { // Tambahkan pengecekan mounted if (!mounted) return; setState(() { _isTestingAuth = true; }); // Simpan pengaturan terlebih dahulu await _saveSettings(); // Uji autentikasi final success = await FireflyApiService.testAuthentication(baseUrl: _urlController.text.trim(), accessToken: _tokenController.text.trim()); // Tambahkan pengecekan mounted sebelum setState if (mounted) { setState(() { _isTestingAuth = false; }); if (success) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Autentikasi berhasil!')), ); } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Autentikasi gagal. Periksa token yang digunakan.')), ); } } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Pengaturan Firefly III'), centerTitle: true, ), body: Padding( padding: const EdgeInsets.all(16.0), child: Form( key: _formKey, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Koneksi ke Firefly III', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 16), const Text( 'Masukkan URL lengkap ke instance Firefly III Anda (contoh: https://firefly.example.com)', ), const SizedBox(height: 8), TextFormField( controller: _urlController, decoration: const InputDecoration( labelText: 'Firefly III URL', border: OutlineInputBorder(), hintText: 'https://firefly.example.com', ), validator: (value) { if (value == null || value.isEmpty) { return 'Mohon masukkan URL Firefly III'; } return null; }, ), const SizedBox(height: 16), const Text( 'Masukkan Personal Access Token dari Firefly III', ), const SizedBox(height: 8), TextFormField( controller: _tokenController, decoration: const InputDecoration( labelText: 'Access Token', border: OutlineInputBorder(), ), obscureText: true, validator: (value) { if (value == null || value.isEmpty) { return 'Mohon masukkan access token'; } return null; }, ), const SizedBox(height: 16), Row( children: [ Expanded( child: ElevatedButton( onPressed: _isTestingConnection ? null : _testConnection, child: _isTestingConnection ? const Text('Menguji Koneksi...') : const Text('Uji Koneksi'), ), ), const SizedBox(width: 16), Expanded( child: ElevatedButton( onPressed: _isTestingAuth ? null : _testAuthentication, child: _isTestingAuth ? const Text('Menguji Auth...') : const Text('Uji Auth'), ), ), ], ), const SizedBox(height: 16), SizedBox( width: double.infinity, child: ElevatedButton( onPressed: _saveSettings, child: const Text('Simpan Pengaturan'), ), ), ], ), ), ), ); } }