107 lines
3.5 KiB
Dart
107 lines
3.5 KiB
Dart
import 'package:get/get.dart';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
import 'package:cloud_firestore/cloud_firestore.dart'; // Import Firestore
|
|
import '../../../routes/app_pages.dart'; // Pastikan route sudah diatur dengan benar
|
|
|
|
class HomeController extends GetxController {
|
|
final latitude = 0.0.obs;
|
|
final longitude = 0.0.obs;
|
|
final lastUpdate = DateTime.now().obs;
|
|
|
|
final DatabaseReference _database = FirebaseDatabase.instance.ref();
|
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
|
|
double previousLatitude = 0.0;
|
|
double previousLongitude = 0.0;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
fetchLocation();
|
|
}
|
|
|
|
void fetchLocation() {
|
|
_database.child('lokasi').onValue.listen((event) async {
|
|
final data = event.snapshot.value as Map?;
|
|
if (data != null) {
|
|
double newLatitude = _convertToDouble(data['latitude']);
|
|
double newLongitude = _convertToDouble(data['longitude']);
|
|
lastUpdate.value = DateTime.now();
|
|
|
|
// Cek jika data berbeda dengan sebelumnya
|
|
if (newLatitude != previousLatitude || newLongitude != previousLongitude) {
|
|
// Pengecekan ke Firestore untuk data lokasi terakhir
|
|
bool shouldSave = await _shouldSaveNewLocation(newLatitude, newLongitude);
|
|
if (shouldSave) {
|
|
// Menyimpan data ke Firestore jika ada perubahan
|
|
_saveLocationToHistory(newLatitude, newLongitude);
|
|
|
|
// Update nilai sebelumnya
|
|
previousLatitude = newLatitude;
|
|
previousLongitude = newLongitude;
|
|
}
|
|
}
|
|
|
|
// Update nilai yang terhubung ke UI
|
|
latitude.value = newLatitude;
|
|
longitude.value = newLongitude;
|
|
}
|
|
});
|
|
}
|
|
|
|
// Fungsi untuk mengonversi data ke double, mengatasi string dan number
|
|
double _convertToDouble(dynamic value) {
|
|
if (value == null) return 0.0;
|
|
if (value is double) return value;
|
|
if (value is int) return value.toDouble();
|
|
if (value is String) {
|
|
final parsedValue = double.tryParse(value);
|
|
return parsedValue ?? 0.0; // Jika tidak bisa di-parse, kembalikan 0.0
|
|
}
|
|
return 0.0;
|
|
}
|
|
|
|
// Fungsi untuk menyimpan data lokasi yang berubah ke Firestore
|
|
void _saveLocationToHistory(double latitude, double longitude) {
|
|
_firestore.collection('history_maps').add({
|
|
'latitude': latitude,
|
|
'longitude': longitude,
|
|
'timestamp': FieldValue.serverTimestamp(),
|
|
}).then((value) {
|
|
print("Data lokasi berhasil disimpan di Firestore");
|
|
}).catchError((error) {
|
|
print("Gagal menyimpan data lokasi: $error");
|
|
});
|
|
}
|
|
|
|
// Fungsi untuk memeriksa apakah lokasi baru berbeda dari data terakhir yang ada di Firestore
|
|
Future<bool> _shouldSaveNewLocation(double latitude, double longitude) async {
|
|
// Ambil data terakhir dari history_maps
|
|
final snapshot = await _firestore.collection('history_maps').orderBy('timestamp', descending: true).limit(1).get();
|
|
|
|
if (snapshot.docs.isNotEmpty) {
|
|
final lastDoc = snapshot.docs.first;
|
|
final lastLatitude = lastDoc['latitude'] as double;
|
|
final lastLongitude = lastDoc['longitude'] as double;
|
|
|
|
// Bandingkan data baru dengan data terakhir
|
|
if (lastLatitude != latitude || lastLongitude != longitude) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void goToAboutUs() {
|
|
Get.toNamed(Routes.ABOUT); // Arahkan ke halaman About Us
|
|
}
|
|
|
|
void goToMaps() {
|
|
Get.toNamed(Routes.MAPS); // Arahkan ke halaman Maps
|
|
}
|
|
|
|
void goToSettings() {
|
|
Get.toNamed(Routes.SETTING); // Arahkan ke halaman Settings
|
|
}
|
|
}
|