68 lines
2.0 KiB
Dart
68 lines
2.0 KiB
Dart
import 'package:digiplug/data/models/device.dart';
|
|
import 'package:digiplug/data/models/room.dart';
|
|
import 'package:digiplug/utils/enums.dart';
|
|
import 'package:flutter/foundation.dart'; // PERBAIKAN: Import foundation untuk ChangeNotifier
|
|
|
|
// PERBAIKAN: Tambahkan 'extends ChangeNotifier'
|
|
class HomeDataProvider extends ChangeNotifier {
|
|
// --- DATA STATIS (HARDCODED) ---
|
|
|
|
// Daftar ruangan yang tersedia di rumah
|
|
final List<Room> _rooms = [
|
|
Room(id: 'r1', name: 'Kamar Tidur', deviceCount: 1),
|
|
Room(id: 'r2', name: 'Ruang Keluarga', deviceCount: 1),
|
|
Room(id: 'r3', name: 'Dapur', deviceCount: 1),
|
|
Room(id: 'r4', name: 'Teras', deviceCount: 1),
|
|
];
|
|
|
|
// Daftar perangkat yang ada di rumah
|
|
final List<Device> _devices = [
|
|
Device(
|
|
id: '1',
|
|
name: 'Lampu Teras',
|
|
type: DeviceType.light,
|
|
room: 'Teras',
|
|
active: true,
|
|
isFavorite: true),
|
|
Device(
|
|
id: '2',
|
|
name: 'AC Kamar Tidur',
|
|
type: DeviceType.ac,
|
|
room: 'Kamar Tidur',
|
|
active: false),
|
|
Device(
|
|
id: '3',
|
|
name: 'TV Ruang Keluarga',
|
|
type: DeviceType.smartTv,
|
|
room: 'Ruang Keluarga',
|
|
active: true),
|
|
Device(
|
|
id: '4',
|
|
name: 'DigiPlug Kulkas',
|
|
type: DeviceType.plug,
|
|
room: 'Dapur',
|
|
active: true),
|
|
];
|
|
|
|
// --- GETTERS ---
|
|
// UI akan memanggil getter ini untuk mendapatkan data.
|
|
List<Room> get rooms => _rooms;
|
|
List<Device> get devices => _devices;
|
|
|
|
// --- FUNGSI UNTUK MENGUBAH DATA ---
|
|
// Fungsi ini menunjukkan bagaimana Anda bisa memodifikasi state dan
|
|
// memberi tahu UI untuk memperbarui dirinya.
|
|
void toggleDeviceStatus(String deviceId) {
|
|
// Cari perangkat berdasarkan ID
|
|
final index = _devices.indexWhere((d) => d.id == deviceId);
|
|
|
|
if (index != -1) {
|
|
// Ubah status 'active' dari perangkat
|
|
_devices[index].active = !_devices[index].active;
|
|
|
|
// Beri tahu semua widget yang mendengarkan provider ini untuk membangun ulang
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|