55 lines
2.1 KiB
Dart
55 lines
2.1 KiB
Dart
import 'package:cheminova/controller/home_service.dart';
|
|
import 'package:cheminova/models/user_model.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../notification_service.dart';
|
|
|
|
class HomeController extends GetxController {
|
|
// Instance of HomeService to handle API requests
|
|
final HomeService homeService = HomeService();
|
|
// Instance of NotificationServices for handling notifications
|
|
NotificationServices notificationServices = NotificationServices();
|
|
|
|
UserModel? user; // Variable to store user details
|
|
|
|
@override
|
|
void onInit() {
|
|
// Called when the controller is initialized
|
|
getUser(); // Fetch user data
|
|
super.onInit(); // Call parent class's onInit method
|
|
notificationServices.requestNotificationPermission(); // Request permission for notifications
|
|
notificationServices.getDeviceToken().then((value) {
|
|
// Retrieve the FCM device token
|
|
print('Device Token: $value');
|
|
fcmToken(); // Call the function to send the FCM token to the server
|
|
});
|
|
}
|
|
// Function to handle FCM token retrieval and sending it to the server
|
|
Future<void> fcmToken() async {
|
|
// Get SharedPreferences instance
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? token = prefs.getString('token'); // Retrieve the saved user token
|
|
final fcmToken = await NotificationServices().getDeviceToken(); // Get the FCM device token
|
|
print('fcmToken: $fcmToken');
|
|
homeService.fcmToken({"fcmToken": fcmToken}, token); // Send the FCM token to the server
|
|
}
|
|
// Function to fetch user details from the server
|
|
Future<void> getUser() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
String? token = prefs.getString('token');
|
|
// Create an instance of HomeService
|
|
HomeService homeService = HomeService();
|
|
// Fetch user details using the token
|
|
user = await homeService.getUser(token: token);
|
|
|
|
if (user != null) {
|
|
print(user); // For debugging, prints the user details
|
|
} else {
|
|
print('Failed to fetch user data');
|
|
}
|
|
}
|
|
}
|
|
|
|
|