280 lines
8.3 KiB
Dart
280 lines
8.3 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:firebase_database/firebase_database.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_background_service/flutter_background_service.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'login_screen.dart';
|
|
import 'toast.dart';
|
|
|
|
class ProfilePage extends StatefulWidget {
|
|
const ProfilePage({super.key});
|
|
|
|
@override
|
|
State<ProfilePage> createState() => _ProfilePageState();
|
|
}
|
|
|
|
class _ProfilePageState extends State<ProfilePage> {
|
|
final FirebaseAuth _auth = FirebaseAuth.instance;
|
|
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
|
|
final DatabaseReference _dbRef = FirebaseDatabase.instance.ref();
|
|
|
|
String _username = '';
|
|
String _email = '';
|
|
String _obat1 = '-';
|
|
String _obat2 = '-';
|
|
String _jumlahObat1 = '-';
|
|
String _jumlahObat2 = '-';
|
|
|
|
bool _loading = true;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadProfileData();
|
|
}
|
|
|
|
Future<void> _showLogoutConfirmationDialog() async {
|
|
final shouldLogout = await showDialog<bool>(
|
|
context: context,
|
|
builder:
|
|
(context) => AlertDialog(
|
|
title: const Text('Konfirmasi Logout'),
|
|
content: const Text(
|
|
'Apakah Anda yakin ingin logout dari akun ini?',
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
child: const Text('Tidak'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
child: const Text('Ya'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (shouldLogout == true) {
|
|
_logout();
|
|
}
|
|
}
|
|
|
|
Future<void> _loadProfileData() async {
|
|
final user = _auth.currentUser;
|
|
if (user == null) {
|
|
setState(() {
|
|
_loading = false;
|
|
});
|
|
return;
|
|
}
|
|
final uid = user.uid;
|
|
|
|
final userDoc = await _firestore.collection('users').doc(uid).get();
|
|
final dataUser = userDoc.data();
|
|
final username =
|
|
dataUser != null && dataUser.containsKey('username')
|
|
? dataUser['username'] as String
|
|
: '';
|
|
final email =
|
|
dataUser != null && dataUser.containsKey('email')
|
|
? dataUser['email'] as String
|
|
: '';
|
|
|
|
Future<String> getObatIfUserMatch(
|
|
String kotak,
|
|
String obatKey,
|
|
String userIdPath,
|
|
) async {
|
|
final userIdSnap = await _dbRef.child('jadwal/$kotak/$userIdPath').get();
|
|
if (!userIdSnap.exists || userIdSnap.value != uid) return '-';
|
|
|
|
final obatSnap = await _dbRef.child('jadwal/$kotak/$obatKey').get();
|
|
if (!obatSnap.exists ||
|
|
(obatSnap.value == null ||
|
|
obatSnap.value.toString().trim().isEmpty)) {
|
|
return '-';
|
|
}
|
|
return obatSnap.value.toString();
|
|
}
|
|
|
|
Future<String> getJumlahObatIfUserMatch(
|
|
String kotak,
|
|
String jumlahKey,
|
|
String userIdPath,
|
|
) async {
|
|
final userIdSnap = await _dbRef.child('jadwal/$kotak/$userIdPath').get();
|
|
if (!userIdSnap.exists || userIdSnap.value != uid) return '-';
|
|
|
|
final jumlahSnap = await _dbRef.child('jadwal/$kotak/$jumlahKey').get();
|
|
if (!jumlahSnap.exists ||
|
|
jumlahSnap.value == null ||
|
|
jumlahSnap.value.toString().trim().isEmpty) {
|
|
return '-';
|
|
}
|
|
return jumlahSnap.value.toString();
|
|
}
|
|
|
|
final obat1 = await getObatIfUserMatch('kotak1', 'obat1', 'userId');
|
|
final obat2 = await getObatIfUserMatch('kotak2', 'obat2', 'userId');
|
|
final jumlah1 = await getJumlahObatIfUserMatch(
|
|
'kotak1',
|
|
'jumlahObat',
|
|
'userId',
|
|
);
|
|
final jumlah2 = await getJumlahObatIfUserMatch(
|
|
'kotak2',
|
|
'jumlahObat',
|
|
'userId',
|
|
);
|
|
|
|
setState(() {
|
|
_username = username;
|
|
_email = email;
|
|
_obat1 = obat1;
|
|
_obat2 = obat2;
|
|
_jumlahObat1 = jumlah1;
|
|
_jumlahObat2 = jumlah2;
|
|
_loading = false;
|
|
});
|
|
}
|
|
|
|
Future<void> _logout() async {
|
|
await FirebaseAuth.instance.signOut();
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove('userId');
|
|
FlutterBackgroundService().invoke("stopService");
|
|
|
|
print("✅ Logout berhasil dan background service dihentikan");
|
|
print('❌ userId dihapus dari SharedPreferences');
|
|
Navigator.pushReplacement(
|
|
context,
|
|
MaterialPageRoute(builder: (context) => const LoginScreen()),
|
|
);
|
|
showToast(message: "Successfully signed out");
|
|
}
|
|
|
|
Future<void> _refresh() async {
|
|
setState(() {
|
|
_loading = true;
|
|
});
|
|
await _loadProfileData();
|
|
}
|
|
|
|
Widget _buildField(String label, String value) {
|
|
return Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
label,
|
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
|
|
),
|
|
const SizedBox(height: 6),
|
|
Container(
|
|
width: double.infinity,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 14),
|
|
decoration: BoxDecoration(
|
|
color: Colors.white,
|
|
borderRadius: BorderRadius.circular(16),
|
|
boxShadow: const [
|
|
BoxShadow(
|
|
color: Colors.black12,
|
|
blurRadius: 4,
|
|
offset: Offset(0, 2),
|
|
),
|
|
],
|
|
),
|
|
child: Text(
|
|
value,
|
|
style: const TextStyle(fontSize: 18, color: Colors.black87),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_loading) {
|
|
return const Scaffold(
|
|
backgroundColor: Color(0xFFE9F5EC),
|
|
body: Center(child: CircularProgressIndicator()),
|
|
);
|
|
}
|
|
|
|
return Scaffold(
|
|
backgroundColor: const Color(0xFFE9F5EC),
|
|
body: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 16),
|
|
alignment: Alignment.center,
|
|
width: double.infinity,
|
|
decoration: const BoxDecoration(
|
|
color: Colors.green,
|
|
borderRadius: BorderRadius.only(
|
|
bottomLeft: Radius.circular(32),
|
|
bottomRight: Radius.circular(32),
|
|
),
|
|
),
|
|
child: const Text(
|
|
'Profile',
|
|
style: TextStyle(
|
|
fontSize: 24,
|
|
fontWeight: FontWeight.bold,
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: RefreshIndicator(
|
|
onRefresh: _refresh,
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.symmetric(
|
|
horizontal: 20,
|
|
vertical: 24,
|
|
),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
_buildField('Username', _username),
|
|
_buildField('Email', _email),
|
|
_buildField('Nama Obat 1', _obat1),
|
|
_buildField('Nama Obat 2', _obat2),
|
|
_buildField(
|
|
'Jumlah Obat Tersisa di Kotak 1',
|
|
_jumlahObat1,
|
|
),
|
|
_buildField(
|
|
'Jumlah Obat Tersisa di Kotak 2',
|
|
_jumlahObat2,
|
|
),
|
|
const SizedBox(height: 30),
|
|
TextButton(
|
|
onPressed: _showLogoutConfirmationDialog,
|
|
style: TextButton.styleFrom(
|
|
foregroundColor: Colors.red,
|
|
textStyle: const TextStyle(
|
|
fontWeight: FontWeight.bold,
|
|
fontSize: 16,
|
|
),
|
|
),
|
|
child: const Text('Logout Akun'),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|