42 lines
1.2 KiB
Dart
42 lines
1.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
|
|
import '../services/firebase_service.dart';
|
|
|
|
class ACConfigProvider extends ChangeNotifier {
|
|
bool _hasConfig = false;
|
|
String _protocol = 'UNKNOWN';
|
|
int _model = -1;
|
|
|
|
bool get hasConfig => _hasConfig;
|
|
String get protocol => _protocol;
|
|
int get model => _model;
|
|
|
|
StreamSubscription<DatabaseEvent>? _subscription;
|
|
|
|
void startListening() {
|
|
_subscription?.cancel();
|
|
_subscription = FirebaseService.acStateRef.onValue.listen((event) {
|
|
if (event.snapshot.value == null) {
|
|
_hasConfig = false;
|
|
} else {
|
|
final data = Map<dynamic, dynamic>.from(event.snapshot.value as Map);
|
|
_hasConfig =
|
|
data['has_config'] == true ||
|
|
(data['protocol'] != null && data['protocol'] != 'UNKNOWN');
|
|
_protocol = data['protocol']?.toString() ?? 'UNKNOWN';
|
|
_model = int.tryParse(data['model']?.toString() ?? '-1') ?? -1;
|
|
}
|
|
notifyListeners();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_subscription?.cancel();
|
|
super.dispose();
|
|
}
|
|
}
|