Upload files to "Source Code Mobile"
This commit is contained in:
parent
f33e220271
commit
edcaf2bb91
|
|
@ -0,0 +1,70 @@
|
||||||
|
import 'package:firebase_auth/firebase_auth.dart';
|
||||||
|
import 'package:firebase_database/firebase_database.dart';
|
||||||
|
|
||||||
|
class AuthService {
|
||||||
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
||||||
|
|
||||||
|
final FirebaseDatabase _database = FirebaseDatabase.instance;
|
||||||
|
|
||||||
|
User? get currentUser => _auth.currentUser;
|
||||||
|
|
||||||
|
// ================= LOGIN =================
|
||||||
|
|
||||||
|
Future<UserCredential> login({
|
||||||
|
required String email,
|
||||||
|
required String password,
|
||||||
|
}) async {
|
||||||
|
return await _auth.signInWithEmailAndPassword(
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= REGISTER =================
|
||||||
|
|
||||||
|
Future<UserCredential> register({
|
||||||
|
required String name,
|
||||||
|
required String email,
|
||||||
|
required String password,
|
||||||
|
}) async {
|
||||||
|
UserCredential credential = await _auth.createUserWithEmailAndPassword(
|
||||||
|
email: email,
|
||||||
|
password: password,
|
||||||
|
);
|
||||||
|
|
||||||
|
final uid = credential.user!.uid;
|
||||||
|
|
||||||
|
await _database.ref('users').child(uid).set({
|
||||||
|
'uid': uid,
|
||||||
|
'name': name,
|
||||||
|
'email': email,
|
||||||
|
'created_at': DateTime.now().toIso8601String(),
|
||||||
|
});
|
||||||
|
|
||||||
|
return credential;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= LOGOUT =================
|
||||||
|
|
||||||
|
Future<void> logout() async {
|
||||||
|
await _auth.signOut();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ================= GET USER =================
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>?> getUserData() async {
|
||||||
|
if (_auth.currentUser == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final uid = _auth.currentUser!.uid;
|
||||||
|
|
||||||
|
final snapshot = await _database.ref('users').child(uid).get();
|
||||||
|
|
||||||
|
if (!snapshot.exists) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Map<String, dynamic>.from(snapshot.value as Map<dynamic, dynamic>);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
import 'package:firebase_database/firebase_database.dart';
|
||||||
|
|
||||||
|
class FirebaseService {
|
||||||
|
static final FirebaseDatabase _database = FirebaseDatabase.instance;
|
||||||
|
|
||||||
|
// ================= CURRENT =================
|
||||||
|
|
||||||
|
static DatabaseReference currentRef = _database.ref('monitoring/current');
|
||||||
|
|
||||||
|
// ================= AC =================
|
||||||
|
// Path untuk mengirim perintah dari Flutter ke ESP32 (script expects '/ac/control')
|
||||||
|
static DatabaseReference acCommandRef = _database.ref('ac/control');
|
||||||
|
static DatabaseReference acStateRef = _database.ref('ac/state');
|
||||||
|
static DatabaseReference acConfigStatusRef = _database.ref('ac/config_status');
|
||||||
|
static DatabaseReference acConfigBrandsRef = _database.ref('ac/config_brands');
|
||||||
|
static DatabaseReference acAutoTriggerHistoryRef = _database.ref('ac/auto_trigger_history');
|
||||||
|
|
||||||
|
// ================= HISTORY =================
|
||||||
|
|
||||||
|
static DatabaseReference monitoringHistoryRef = _database.ref(
|
||||||
|
'monitoring/history',
|
||||||
|
);
|
||||||
|
|
||||||
|
static DatabaseReference controlHistoryRef = _database.ref('history_control');
|
||||||
|
|
||||||
|
// ================= NOTIFICATION =================
|
||||||
|
|
||||||
|
static DatabaseReference notificationRef = _database.ref('notification');
|
||||||
|
|
||||||
|
// ================= IR RECEIVED =================
|
||||||
|
|
||||||
|
static DatabaseReference irReceivedRef = _database.ref('ir_received');
|
||||||
|
|
||||||
|
// ================= IR LEARNED & COMMANDS =================
|
||||||
|
|
||||||
|
static DatabaseReference irLearnedRef = _database.ref('ir_learned');
|
||||||
|
static DatabaseReference irCommandsRef = _database.ref('ir_commands');
|
||||||
|
|
||||||
|
// ================= USERS =================
|
||||||
|
|
||||||
|
static DatabaseReference usersRef = _database.ref('users');
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,42 @@
|
||||||
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
|
|
||||||
|
class NotificationService {
|
||||||
|
static final FlutterLocalNotificationsPlugin _plugin =
|
||||||
|
FlutterLocalNotificationsPlugin();
|
||||||
|
|
||||||
|
static const AndroidNotificationChannel _channel = AndroidNotificationChannel(
|
||||||
|
'air_quality_channel',
|
||||||
|
'Kualitas Udara',
|
||||||
|
description: 'Notifikasi sistem monitoring kualitas udara & AC otomatis',
|
||||||
|
importance: Importance.high,
|
||||||
|
playSound: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
static Future<void> initialize() async {
|
||||||
|
const androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||||
|
const initSettings = InitializationSettings(android: androidInit);
|
||||||
|
await _plugin.initialize(initSettings);
|
||||||
|
|
||||||
|
final androidPlugin = _plugin.resolvePlatformSpecificImplementation<
|
||||||
|
AndroidFlutterLocalNotificationsPlugin>();
|
||||||
|
await androidPlugin?.createNotificationChannel(_channel);
|
||||||
|
await androidPlugin?.requestNotificationsPermission();
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<void> showNotification({
|
||||||
|
required String title,
|
||||||
|
required String body,
|
||||||
|
int id = 0,
|
||||||
|
}) async {
|
||||||
|
final androidDetails = AndroidNotificationDetails(
|
||||||
|
_channel.id,
|
||||||
|
_channel.name,
|
||||||
|
channelDescription: _channel.description,
|
||||||
|
importance: Importance.high,
|
||||||
|
priority: Priority.high,
|
||||||
|
icon: '@mipmap/ic_launcher',
|
||||||
|
);
|
||||||
|
final notifDetails = NotificationDetails(android: androidDetails);
|
||||||
|
await _plugin.show(id, title, body, notifDetails);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue