feat: give parameters to callPage to avoid fetching when possible
This commit is contained in:
parent
20d8c9643f
commit
acbccaac74
@ -267,8 +267,12 @@ class _ContactModalState extends State<ContactModal> {
|
|||||||
),
|
),
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
if (widget.contact.phones.isNotEmpty) {
|
if (widget.contact.phones.isNotEmpty) {
|
||||||
await _callService.makeGsmCall(context,
|
await _callService.makeGsmCall(
|
||||||
phoneNumber: phoneNumber);
|
context,
|
||||||
|
phoneNumber: phoneNumber,
|
||||||
|
displayName: widget.contact.displayName,
|
||||||
|
thumbnail: widget.contact.thumbnail,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
@ -425,7 +425,12 @@ class _HistoryPageState extends State<HistoryPage>
|
|||||||
icon: const Icon(Icons.phone, color: Colors.green),
|
icon: const Icon(Icons.phone, color: Colors.green),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
if (contact.phones.isNotEmpty) {
|
if (contact.phones.isNotEmpty) {
|
||||||
_callService.makeGsmCall(context, phoneNumber: contact.phones.first.number);
|
await _callService.makeGsmCall(
|
||||||
|
context,
|
||||||
|
phoneNumber: contact.phones.first.number,
|
||||||
|
displayName: contact.displayName,
|
||||||
|
thumbnail: contact.thumbnail,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(
|
const SnackBar(
|
||||||
|
@ -1,16 +1,17 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import '../features/call/call_page.dart';
|
import '../features/call/call_page.dart';
|
||||||
import '../features/call/incoming_call_page.dart';
|
import '../features/call/incoming_call_page.dart';
|
||||||
import '../services/contact_service.dart'; // Import your ContactService
|
import '../services/contact_service.dart';
|
||||||
|
|
||||||
class CallService {
|
class CallService {
|
||||||
static const MethodChannel _channel = MethodChannel('call_service');
|
static const MethodChannel _channel = MethodChannel('call_service');
|
||||||
static String? currentPhoneNumber;
|
static String? currentPhoneNumber;
|
||||||
static String? currentDisplayName; // Store display name
|
static String? currentDisplayName;
|
||||||
static Uint8List? currentThumbnail; // Store thumbnail
|
static Uint8List? currentThumbnail;
|
||||||
static bool _isCallPageVisible = false;
|
static bool _isCallPageVisible = false;
|
||||||
final ContactService _contactService = ContactService(); // Instantiate ContactService
|
final ContactService _contactService = ContactService();
|
||||||
|
|
||||||
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
static final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
|
||||||
@ -28,7 +29,6 @@ class CallService {
|
|||||||
final phoneNumber = call.arguments["callId"] as String;
|
final phoneNumber = call.arguments["callId"] as String;
|
||||||
final state = call.arguments["state"] as String;
|
final state = call.arguments["state"] as String;
|
||||||
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
currentPhoneNumber = phoneNumber.replaceFirst('tel:', '');
|
||||||
// Fetch contact info using ContactService if not already set
|
|
||||||
await _fetchContactInfo(currentPhoneNumber!);
|
await _fetchContactInfo(currentPhoneNumber!);
|
||||||
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
print('CallService: Call added, number: $currentPhoneNumber, state: $state');
|
||||||
if (state == "ringing") {
|
if (state == "ringing") {
|
||||||
@ -61,12 +61,12 @@ class CallService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _fetchContactInfo(String phoneNumber) async {
|
Future<void> _fetchContactInfo(String phoneNumber) async {
|
||||||
if (currentDisplayName != null && currentThumbnail != null) return; // Already set
|
|
||||||
try {
|
try {
|
||||||
final contacts = await _contactService.fetchContacts(); // Use ContactService
|
final contacts = await _contactService.fetchContacts();
|
||||||
|
final normalizedPhoneNumber = _normalizePhoneNumber(phoneNumber);
|
||||||
for (var contact in contacts) {
|
for (var contact in contacts) {
|
||||||
for (var phone in contact.phones) {
|
for (var phone in contact.phones) {
|
||||||
if (_normalizePhoneNumber(phone.number) == _normalizePhoneNumber(phoneNumber)) {
|
if (_normalizePhoneNumber(phone.number) == normalizedPhoneNumber) {
|
||||||
currentDisplayName = contact.displayName;
|
currentDisplayName = contact.displayName;
|
||||||
currentThumbnail = contact.thumbnail;
|
currentThumbnail = contact.thumbnail;
|
||||||
return;
|
return;
|
||||||
@ -74,8 +74,8 @@ class CallService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If no match found, use phone number as fallback
|
// If no match found, use phone number as fallback
|
||||||
currentDisplayName ??= phoneNumber;
|
currentDisplayName = phoneNumber;
|
||||||
currentThumbnail ??= null;
|
currentThumbnail = null;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print('CallService: Error fetching contact info: $e');
|
print('CallService: Error fetching contact info: $e');
|
||||||
currentDisplayName = phoneNumber;
|
currentDisplayName = phoneNumber;
|
||||||
@ -151,8 +151,12 @@ class CallService {
|
|||||||
}) async {
|
}) async {
|
||||||
try {
|
try {
|
||||||
currentPhoneNumber = phoneNumber;
|
currentPhoneNumber = phoneNumber;
|
||||||
currentDisplayName = displayName ?? phoneNumber; // Use provided or fetch later
|
// Use provided displayName and thumbnail if available, otherwise fetch
|
||||||
currentThumbnail = thumbnail; // Use provided or fetch later
|
currentDisplayName = displayName ?? phoneNumber;
|
||||||
|
currentThumbnail = thumbnail;
|
||||||
|
if (displayName == null || thumbnail == null) {
|
||||||
|
await _fetchContactInfo(phoneNumber);
|
||||||
|
}
|
||||||
print('CallService: Making GSM call to $phoneNumber');
|
print('CallService: Making GSM call to $phoneNumber');
|
||||||
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
|
final result = await _channel.invokeMethod('makeGsmCall', {"phoneNumber": phoneNumber});
|
||||||
print('CallService: makeGsmCall result: $result');
|
print('CallService: makeGsmCall result: $result');
|
||||||
@ -162,9 +166,7 @@ class CallService {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Fetch contact info if not provided
|
_navigateToCallPage(context);
|
||||||
await _fetchContactInfo(phoneNumber);
|
|
||||||
_navigateToCallPage(context); // Navigate immediately after call initiation
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
print("CallService: Error making call: $e");
|
print("CallService: Error making call: $e");
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
Loading…
Reference in New Issue
Block a user