182 lines
6.2 KiB
Dart
182 lines
6.2 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:absensi_sas_flutter/screen/login/logout_provider.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
|
import 'package:google_sign_in/google_sign_in.dart';
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../../app/constant.dart';
|
|
import '../../app/route.dart';
|
|
import '../../provider/current_user_provider.dart';
|
|
import '../../provider/google_login_provider.dart';
|
|
import '../../widget/sankbar_widget.dart';
|
|
|
|
class HomeScreenV1 extends HookConsumerWidget {
|
|
const HomeScreenV1({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final isLoading = useState(false);
|
|
// final errorMessage = useState("");
|
|
final successMessage = useState("");
|
|
final googleSignIn = ref.watch(googleSignInProvider);
|
|
|
|
// final currentUserGoogleAccount = ref.watch(currentUserGoogleProvider);
|
|
|
|
GoogleSignInAccount? currentUserGoogle =
|
|
ref.watch(currentUserGoogleProvider);
|
|
|
|
final selectedUser = ref.read(currentUserProvider);
|
|
|
|
// GoogleSignInAccount? accountX;
|
|
// final accountX = useState<GoogleSignInAccount?>(null);
|
|
|
|
// useEffect(() {
|
|
// WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
|
// final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0";
|
|
// if (staffID == "0" && currentUserGoogle == null) {
|
|
// //not login
|
|
// Navigator.of(context)
|
|
// .pushNamedAndRemoveUntil(loginRoute, (route) => true);
|
|
|
|
// // Navigator.popAndPushNamed(context, loginRoute);
|
|
// return;
|
|
// }
|
|
// });
|
|
// return () {};
|
|
// }, []);
|
|
|
|
// useEffect(() {
|
|
// WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
|
// final staffID = ref.read(currentUserProvider)?.model.staffId ?? "0";
|
|
// final accountGoogle = ref.read(currentUserGoogleProvider)?.id ?? "0";
|
|
|
|
// if (staffID == "0" && accountGoogle == "0") {
|
|
// //not login
|
|
// Navigator.of(context)
|
|
// .pushNamedAndRemoveUntil(loginRoute, (route) => true);
|
|
|
|
// // Navigator.popAndPushNamed(context, loginRoute);
|
|
// return;
|
|
// }
|
|
// });
|
|
// return () {};
|
|
// }, []);
|
|
|
|
// LISTEN PROVIDER
|
|
ref.listen(logoutProvider, (prev, next) async {
|
|
if (next is LogoutStateLoading) {
|
|
isLoading.value = true;
|
|
} else if (next is LogoutStateError) {
|
|
isLoading.value = false;
|
|
// errorMessage.value = next.message;
|
|
// Timer(const Duration(seconds: 3), () {
|
|
// errorMessage.value = "";
|
|
// });
|
|
SanckbarWidget(context, next.message, snackbarType.warning);
|
|
} else if (next is LogoutStateDone) {
|
|
isLoading.value = false;
|
|
|
|
if (next.model.status == "OK") {
|
|
final shared = await SharedPreferences.getInstance();
|
|
final bearerString = shared.get(Constant.bearerName).toString();
|
|
// print(bearerString);
|
|
if (bearerString.isNotEmpty) {
|
|
shared.remove(bearerString);
|
|
shared.clear();
|
|
// Navigator.popAndPushNamed(context, loginRoute);
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
loginRoute,
|
|
(route) => false,
|
|
);
|
|
}
|
|
Timer(const Duration(seconds: 3), () async {
|
|
successMessage.value = "";
|
|
});
|
|
} else {
|
|
SanckbarWidget(
|
|
context,
|
|
next.model.message.toString(),
|
|
snackbarType.warning,
|
|
);
|
|
}
|
|
}
|
|
});
|
|
|
|
Future<void> handleLogout() async {
|
|
final googleSignIn = GoogleSignIn();
|
|
|
|
// googleSignIn.signOut();
|
|
await googleSignIn.disconnect();
|
|
await googleSignIn.signOut();
|
|
|
|
// ref.read(currentUserGoogleProvider.notifier).update((state) => null);
|
|
// final shared = await SharedPreferences.getInstance();
|
|
// final bearerString = shared.get(Constant.bearerName).toString();
|
|
// if (bearerString.isNotEmpty) {
|
|
// shared.remove(bearerString);
|
|
// shared.clear();
|
|
// }
|
|
|
|
// if (googleSignIn.currentUser != null) {
|
|
// GoogleSignIn().signOut();
|
|
// }
|
|
|
|
// await GoogleSignIn().disconnect();
|
|
|
|
// Clear state dan kembali ke halaman login
|
|
// ref.read(currentUserGoogleProvider.notifier).update((state) => null);
|
|
// final shared = await SharedPreferences.getInstance();
|
|
// final bearerString = shared.get(Constant.bearerName).toString();
|
|
// if (bearerString.isNotEmpty) {
|
|
// shared.remove(bearerString);
|
|
// shared.clear();
|
|
// }
|
|
Navigator.of(context)
|
|
.pushNamedAndRemoveUntil(loginRoute, (route) => false);
|
|
}
|
|
|
|
return Scaffold(
|
|
body: Padding(
|
|
padding: const EdgeInsets.all(20),
|
|
child: Column(
|
|
children: [
|
|
(currentUserGoogle == null)
|
|
? Center(
|
|
child: CircularProgressIndicator(),
|
|
)
|
|
: Container(
|
|
child: ListTile(
|
|
leading: GoogleUserCircleAvatar(
|
|
// identity: accountX.value!,
|
|
// identity: selectedUser?.googleSignInAccount,
|
|
identity: currentUserGoogle,
|
|
),
|
|
title: Text(
|
|
// accountX.value!.displayName ?? ""
|
|
// selectedUser?.model.name ?? "",
|
|
currentUserGoogle.displayName ?? "",
|
|
),
|
|
// currentUserGoogle?.displayName ?? ""),
|
|
subtitle: Text(
|
|
// currentUserGoogle?.email ?? "",
|
|
selectedUser?.model.email ?? "",
|
|
// accountX.value!.email
|
|
),
|
|
trailing: IconButton(
|
|
icon: Icon(Icons.logout_outlined),
|
|
onPressed: () async {
|
|
handleLogout();
|
|
},
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|