Update: 2026-06-12 01:23:54
This commit is contained in:
@@ -1,149 +0,0 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:google_sign_in/google_sign_in.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
import '../../constant/box_name.dart';
|
||||
import '../../constant/links.dart';
|
||||
import '../../main.dart';
|
||||
import '../../print.dart';
|
||||
import '../functions/crud.dart';
|
||||
import '../../onbording_page.dart';
|
||||
import 'login_controller.dart';
|
||||
|
||||
class GoogleSignInHelper {
|
||||
// ✅ GoogleSignIn singleton
|
||||
static final GoogleSignIn _signIn = GoogleSignIn.instance;
|
||||
|
||||
static GoogleSignInAccount? _lastUser;
|
||||
|
||||
/// 👇 استدعها في main() مرة واحدة
|
||||
static Future<void> init() async {
|
||||
await _signIn.initialize(
|
||||
serverClientId:
|
||||
'594687661098-2u640akrb3k7sak5t0nqki6f4v6hq1bq.apps.googleusercontent.com',
|
||||
);
|
||||
|
||||
// Events listener
|
||||
_signIn.authenticationEvents.listen((event) async {
|
||||
if (event is GoogleSignInAuthenticationEventSignIn) {
|
||||
_lastUser = event.user;
|
||||
await _handleSignUp(event.user);
|
||||
} else if (event is GoogleSignInAuthenticationEventSignOut) {
|
||||
_lastUser = null;
|
||||
}
|
||||
});
|
||||
|
||||
// silent login if possible
|
||||
try {
|
||||
await _signIn.attemptLightweightAuthentication();
|
||||
} catch (e) {
|
||||
Log.print("Error: $e");
|
||||
}
|
||||
}
|
||||
|
||||
/// ✅ تسجيل دخول عادي
|
||||
static Future<GoogleSignInAccount?> signIn() async {
|
||||
try {
|
||||
final user =
|
||||
await _signIn.authenticate(scopeHint: const ['email', 'profile']);
|
||||
_lastUser = user;
|
||||
await _handleSignUp(user);
|
||||
|
||||
// اطبع القيم (للتأكد)
|
||||
Log.print("Google ID: ${user.id}");
|
||||
Log.print("Email: ${user.email}");
|
||||
Log.print("Name: ${user.displayName}");
|
||||
Log.print("Photo: ${user.photoUrl}");
|
||||
return user;
|
||||
} on PlatformException catch (e) {
|
||||
if (e.code == 'sign_in_required') {
|
||||
await showGooglePlayServicesError();
|
||||
}
|
||||
return null;
|
||||
} catch (e) {
|
||||
await addError("Google Sign-In error: $e", "signIn()");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// ✅ تسجيل دخول مخصص لشاشة Login (مع استدعاء الكنترولر)
|
||||
static Future<GoogleSignInAccount?> signInFromLogin() async {
|
||||
await init();
|
||||
final user = await signIn();
|
||||
if (user != null) {
|
||||
await Get.put(LoginController()).loginUsingCredentials(
|
||||
box.read(BoxName.passengerID).toString(),
|
||||
box.read(BoxName.email).toString(),
|
||||
);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/// ✅ طلب سكوبات إضافية (بديل withScopes القديم)
|
||||
static Future<void> requestExtraScopes(List<String> scopes) async {
|
||||
final user = _lastUser;
|
||||
if (user == null) return;
|
||||
await user.authorizationClient.authorizeScopes(scopes);
|
||||
}
|
||||
|
||||
/// ✅ تسجيل خروج
|
||||
static Future<void> signOut() async {
|
||||
await _signIn.signOut();
|
||||
await _handleSignOut();
|
||||
}
|
||||
|
||||
static GoogleSignInAccount? getCurrentUser() => _lastUser;
|
||||
|
||||
// ================= Helpers ==================
|
||||
|
||||
static Future<void> _handleSignUp(GoogleSignInAccount user) async {
|
||||
box.write(BoxName.passengerID, user.id);
|
||||
box.write(BoxName.email, user.email);
|
||||
box.write(BoxName.name, user.displayName ?? '');
|
||||
box.write(BoxName.passengerPhotoUrl, user.photoUrl ?? '');
|
||||
}
|
||||
|
||||
static Future<void> _handleSignOut() async {
|
||||
box.erase();
|
||||
|
||||
Get.offAll(OnBoardingPage());
|
||||
}
|
||||
|
||||
static Future<void> addError(String error, String where) async {
|
||||
await CRUD().post(link: AppLink.addError, payload: {
|
||||
'error': error,
|
||||
'userId': box.read(BoxName.driverID) ?? box.read(BoxName.passengerID),
|
||||
'userType': box.read(BoxName.driverID) != null ? 'Driver' : 'Passenger',
|
||||
'phone': box.read(BoxName.phone) ?? box.read(BoxName.phoneDriver),
|
||||
'device': where,
|
||||
});
|
||||
}
|
||||
|
||||
static Future<void> showGooglePlayServicesError() async {
|
||||
const playStoreUrl =
|
||||
'https://play.google.com/store/apps/details?id=com.google.android.gms&hl=en_US';
|
||||
final uri = Uri.parse(playStoreUrl);
|
||||
if (await canLaunchUrl(uri)) {
|
||||
await launchUrl(uri);
|
||||
} else {
|
||||
showDialog(
|
||||
context: Get.context!,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text('Error'.tr),
|
||||
content: Text(
|
||||
'Could not open the Google Play Store. Please update Google Play Services manually.'
|
||||
.tr,
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: Text('Close'.tr),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,50 @@
|
||||
import 'dart:convert';
|
||||
import 'package:googleapis_auth/auth_io.dart';
|
||||
// import 'dart:convert';
|
||||
// import 'package:googleapis_auth/auth_io.dart';
|
||||
|
||||
class AccessTokenManager {
|
||||
static final AccessTokenManager _instance = AccessTokenManager._internal();
|
||||
late final String serviceAccountJsonKey;
|
||||
AccessToken? _accessToken;
|
||||
DateTime? _expiryDate;
|
||||
// class AccessTokenManager {
|
||||
// static final AccessTokenManager _instance = AccessTokenManager._internal();
|
||||
// late final String serviceAccountJsonKey;
|
||||
// AccessToken? _accessToken;
|
||||
// DateTime? _expiryDate;
|
||||
|
||||
AccessTokenManager._internal();
|
||||
// AccessTokenManager._internal();
|
||||
|
||||
factory AccessTokenManager(String jsonKey) {
|
||||
if (_instance._isServiceAccountKeyInitialized()) {
|
||||
// Prevent re-initialization
|
||||
return _instance;
|
||||
}
|
||||
_instance.serviceAccountJsonKey = jsonKey;
|
||||
return _instance;
|
||||
}
|
||||
// factory AccessTokenManager(String jsonKey) {
|
||||
// if (_instance._isServiceAccountKeyInitialized()) {
|
||||
// // Prevent re-initialization
|
||||
// return _instance;
|
||||
// }
|
||||
// _instance.serviceAccountJsonKey = jsonKey;
|
||||
// return _instance;
|
||||
// }
|
||||
|
||||
bool _isServiceAccountKeyInitialized() {
|
||||
try {
|
||||
serviceAccountJsonKey; // Access to check if initialized
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// bool _isServiceAccountKeyInitialized() {
|
||||
// try {
|
||||
// serviceAccountJsonKey; // Access to check if initialized
|
||||
// return true;
|
||||
// } catch (e) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<String> getAccessToken() async {
|
||||
if (_accessToken != null && DateTime.now().isBefore(_expiryDate!)) {
|
||||
return _accessToken!.data;
|
||||
}
|
||||
try {
|
||||
final serviceAccountCredentials = ServiceAccountCredentials.fromJson(
|
||||
json.decode(serviceAccountJsonKey));
|
||||
final client = await clientViaServiceAccount(
|
||||
serviceAccountCredentials,
|
||||
['https://www.googleapis.com/auth/firebase.messaging'],
|
||||
);
|
||||
// Future<String> getAccessToken() async {
|
||||
// if (_accessToken != null && DateTime.now().isBefore(_expiryDate!)) {
|
||||
// return _accessToken!.data;
|
||||
// }
|
||||
// try {
|
||||
// final serviceAccountCredentials = ServiceAccountCredentials.fromJson(
|
||||
// json.decode(serviceAccountJsonKey));
|
||||
// final client = await clientViaServiceAccount(
|
||||
// serviceAccountCredentials,
|
||||
// ['https://www.googleapis.com/auth/firebase.messaging'],
|
||||
// );
|
||||
|
||||
_accessToken = client.credentials.accessToken;
|
||||
_expiryDate = client.credentials.accessToken.expiry;
|
||||
client.close();
|
||||
return _accessToken!.data;
|
||||
} catch (e) {
|
||||
throw Exception('Failed to obtain access token');
|
||||
}
|
||||
}
|
||||
}
|
||||
// _accessToken = client.credentials.accessToken;
|
||||
// _expiryDate = client.credentials.accessToken.expiry;
|
||||
// client.close();
|
||||
// return _accessToken!.data;
|
||||
// } catch (e) {
|
||||
// throw Exception('Failed to obtain access token');
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -15,7 +15,6 @@ import '../../print.dart';
|
||||
import '../../views/Rate/rate_captain.dart';
|
||||
import '../../views/home/map_page_passenger.dart';
|
||||
import '../../views/home/profile/promos_passenger_page.dart';
|
||||
import '../auth/google_sign.dart';
|
||||
import 'package:siro_rider/controller/voice_call_controller.dart';
|
||||
import '../home/map/ride_lifecycle_controller.dart';
|
||||
import '../home/map/ride_state.dart';
|
||||
@@ -159,7 +158,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
if (Platform.isAndroid) {
|
||||
notificationController.showNotification(title, body, 'cancel');
|
||||
}
|
||||
GoogleSignInHelper.signOut();
|
||||
box.remove(BoxName.jwt);
|
||||
} else if (category == 'Driver Is Going To Passenger') {
|
||||
// <-- كان 'Driver Is Going To Passenger'
|
||||
Get.find<RideLifecycleController>().isDriverInPassengerWay = true;
|
||||
@@ -253,9 +252,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
rideIdVal: rideId.toString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
else if (category == 'Order Applied') {
|
||||
} else if (category == 'Order Applied') {
|
||||
if (Platform.isAndroid) {
|
||||
notificationController.showNotification(
|
||||
'The order Accepted by another Driver'.tr,
|
||||
@@ -317,7 +314,7 @@ class FirebaseMessagesController extends GetxController {
|
||||
// notificationController.showNotification(
|
||||
// 'token change'.tr, 'token change'.tr, 'cancel');
|
||||
// }
|
||||
// GoogleSignInHelper.signOut();
|
||||
// // GoogleSignInHelper.signOut();
|
||||
// } else if (message.notification!.title! == 'Driver Is Going To Passenger') {
|
||||
// Get.find<RideLifecycleController>().isDriverInPassengerWay = true;
|
||||
// Get.find<RideLifecycleController>().update();
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:get/get.dart';
|
||||
import '../../constant/links.dart';
|
||||
|
||||
import '../../print.dart'; // للترجمة .tr
|
||||
|
||||
class NotificationService {
|
||||
static const String _serverUrl =
|
||||
'https://api.intaleq.xyz/siro/ride/firebase/send_fcm.php';
|
||||
static String get _serverUrl => '${AppLink.server}/ride/firebase/send_fcm.php';
|
||||
|
||||
static Future<void> sendNotification({
|
||||
required String target,
|
||||
|
||||
Reference in New Issue
Block a user