64 lines
2.0 KiB
Dart
64 lines
2.0 KiB
Dart
import 'package:flutter/services.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../models/voicemail.dart';
|
|
import '../services/contact_service.dart';
|
|
import 'package:flutter_contacts/flutter_contacts.dart';
|
|
|
|
class VoicemailService {
|
|
static const MethodChannel _channel = MethodChannel('voicemail_service');
|
|
final ContactService _contactService = ContactService();
|
|
|
|
Future<List<Voicemail>> getVoicemails() async {
|
|
try {
|
|
final List<dynamic> voicemailMaps = await _channel.invokeMethod('getVoicemails');
|
|
return voicemailMaps.map((map) => Voicemail.fromMap(Map<String, dynamic>.from(map))).toList();
|
|
} on PlatformException catch (e) {
|
|
debugPrint('Error fetching voicemails: ${e.message}');
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<bool> markAsRead(String voicemailId, {bool isRead = true}) async {
|
|
try {
|
|
final result = await _channel.invokeMethod('markVoicemailAsRead', {
|
|
'id': voicemailId,
|
|
'isRead': isRead,
|
|
});
|
|
return result ?? false;
|
|
} on PlatformException catch (e) {
|
|
debugPrint('Error marking voicemail as read: ${e.message}');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<bool> deleteVoicemail(String voicemailId) async {
|
|
try {
|
|
final result = await _channel.invokeMethod('deleteVoicemail', {
|
|
'id': voicemailId,
|
|
});
|
|
return result ?? false;
|
|
} on PlatformException catch (e) {
|
|
debugPrint('Error deleting voicemail: ${e.message}');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
Future<String?> getSenderName(String phoneNumber) async {
|
|
// Get contacts to find name for the phone number
|
|
List<Contact> contacts = await _contactService.fetchContacts();
|
|
|
|
// Find matching contact
|
|
for (var contact in contacts) {
|
|
for (var phone in contact.phones) {
|
|
if (_sanitizeNumber(phone.number) == _sanitizeNumber(phoneNumber)) {
|
|
return contact.displayName;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
String _sanitizeNumber(String number) {
|
|
return number.replaceAll(RegExp(r'\D'), '');
|
|
}
|
|
} |