feat: give parameters to callPage to avoid fetching when possible

This commit is contained in:
Florian Griffon 2025-04-08 21:55:26 +03:00
parent ba2c1b049f
commit 2af6f43480

View File

@ -1,19 +1,25 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../features/call/call_page.dart';
import '../features/call/incoming_call_page.dart';
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
import '../services/contact_service.dart';
=======
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
import '../services/contact_service.dart'; // Import your ContactService
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
=======
import '../services/contact_service.dart';
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
class CallService {
static const MethodChannel _channel = MethodChannel('call_service');
static String? currentPhoneNumber;
<<<<<<< HEAD
<<<<<<< HEAD
static String? currentDisplayName;
static Uint8List? currentThumbnail;
@ -23,16 +29,27 @@ class CallService {
=======
static String? currentDisplayName; // Store display name
static Uint8List? currentThumbnail; // Store thumbnail
=======
static String? currentDisplayName;
static Uint8List? currentThumbnail;
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
static bool _isCallPageVisible = false;
<<<<<<< HEAD
final ContactService _contactService = ContactService(); // Instantiate ContactService
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
static Map<String, dynamic>? _pendingCall;
static bool wasPhoneLocked = false;
<<<<<<< HEAD
final ContactService _contactService = ContactService();
=======
static Map<String, dynamic>? _pendingCall;
static bool wasPhoneLocked = false;
>>>>>>> ec1779b (callNotifications and various fix related to calls (#49))
=======
=======
final ContactService _contactService = ContactService();
>>>>>>> acbccaa (feat: give parameters to callPage to avoid fetching when possible)
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
@ -49,9 +66,12 @@ class CallService {
final state = call.arguments["state"] as String;
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
<<<<<<< HEAD
<<<<<<< HEAD
=======
// Fetch contact info using ContactService if not already set
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
=======
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
await _fetchContactInfo(currentPhoneNumber!);
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
<<<<<<< HEAD
@ -133,13 +153,17 @@ class CallService {
if (_normalizePhoneNumber(phone.number) == normalizedPhoneNumber) {
=======
Future<void> _fetchContactInfo(String phoneNumber) async {
if (currentDisplayName != null && currentThumbnail != null) return; // Already set
try {
final contacts = await _contactService.fetchContacts(); // Use ContactService
final contacts = await _contactService.fetchContacts();
final normalizedPhoneNumber = _normalizePhoneNumber(phoneNumber);
for (var contact in contacts) {
for (var phone in contact.phones) {
<<<<<<< HEAD
if (_normalizePhoneNumber(phone.number) == _normalizePhoneNumber(phoneNumber)) {
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
=======
if (_normalizePhoneNumber(phone.number) == normalizedPhoneNumber) {
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
currentDisplayName = contact.displayName;
currentThumbnail = contact.thumbnail;
return;
@ -151,8 +175,8 @@ class CallService {
currentThumbnail = null;
=======
// If no match found, use phone number as fallback
currentDisplayName ??= phoneNumber;
currentThumbnail ??= null;
currentDisplayName = phoneNumber;
currentThumbnail = null;
} catch (e) {
print('CallService: Error fetching contact info: $e');
currentDisplayName = phoneNumber;
@ -362,15 +386,22 @@ class CallService {
try {
currentPhoneNumber = phoneNumber;
<<<<<<< HEAD
<<<<<<< HEAD
=======
// Use provided displayName and thumbnail if available, otherwise fetch
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
currentDisplayName = displayName ?? phoneNumber;
currentThumbnail = thumbnail;
if (displayName == null || thumbnail == null) {
await _fetchContactInfo(phoneNumber);
}
<<<<<<< HEAD
=======
currentDisplayName = displayName ?? phoneNumber; // Use provided or fetch later
currentThumbnail = thumbnail; // Use provided or fetch later
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
=======
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
print('CallService: Making GSM call to $phoneNumber');
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
print('CallService: makeGsmCall result: $result');
@ -382,6 +413,7 @@ class CallService {
return;
}
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
_handleCallState("dialing");
=======
@ -392,6 +424,9 @@ class CallService {
await _fetchContactInfo(phoneNumber);
_navigateToCallPage(context); // Navigate immediately after call initiation
>>>>>>> 3e2be8a (feat: fetch contact in makeGsmCall to show it in call)
=======
_navigateToCallPage(context);
>>>>>>> c2c646a (feat: give parameters to callPage to avoid fetching when possible)
} catch (e) {
print("CallService: Error making call: $e");
ScaffoldMessenger.of(context).showSnackBar(