refactor: remove old main file and enhance blocked numbers settings with local storage preferences
All checks were successful
/ mirror (push) Successful in 4s
/ build (push) Successful in 9m13s
/ build-stealth (push) Successful in 9m17s

This commit is contained in:
AlexisDanlos 2025-04-02 15:54:41 +02:00
parent 62d48dc084
commit 91a739a0cd
2 changed files with 132 additions and 165 deletions

View File

@ -1,84 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'services/call_service.dart';
import 'services/cryptography/asymmetric_crypto_service.dart';
import 'globals.dart' as globals;
import 'presentation/features/home/home_page.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Check for stealth mode
const stealthFlag = String.fromEnvironment('STEALTH', defaultValue: 'false');
globals.isStealthMode = stealthFlag.toLowerCase() == 'true';
// Initialize cryptography service with error handling
final AsymmetricCryptoService cryptoService = AsymmetricCryptoService();
try {
await cryptoService.initializeDefaultKeyPair();
} catch (e) {
debugPrint('Error initializing cryptography: $e');
// Continue app initialization even if crypto fails
}
// Request permissions before running the app
await _requestPermissions();
// Initialize call service
CallService();
runApp(
MultiProvider(
providers: [
Provider<AsymmetricCryptoService>(
create: (_) => cryptoService,
),
],
child: const DialerApp(),
),
);
}
Future<void> _requestPermissions() async {
try {
Map<Permission, PermissionStatus> statuses = await [
Permission.phone,
Permission.contacts,
Permission.microphone,
].request();
if (statuses.values.every((status) => status.isGranted)) {
debugPrint("All required permissions granted");
const channel = MethodChannel('call_service');
await channel.invokeMethod('permissionsGranted');
} else {
debugPrint("Permissions denied: ${statuses.entries.where((e) => !e.value.isGranted).map((e) => e.key).join(', ')}");
}
} catch (e) {
debugPrint("Error requesting permissions: $e");
}
}
class DialerApp extends StatelessWidget {
const DialerApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Dialer App',
navigatorKey: CallService.navigatorKey,
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: Colors.black,
primaryColor: Colors.blue,
appBarTheme: const AppBarTheme(
backgroundColor: Colors.black,
elevation: 0,
),
),
home: const MyHomePage(),
);
}
}

View File

@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import '../../../../domain/services/block_service.dart';
import 'package:shared_preferences/shared_preferences.dart';
class BlockedNumbersPage extends StatefulWidget {
const BlockedNumbersPage({super.key});
@ -9,108 +9,159 @@ class BlockedNumbersPage extends StatefulWidget {
}
class _BlockedNumbersPageState extends State<BlockedNumbersPage> {
List<String> _blockedNumbers = [];
bool _loading = true;
bool _blockUnknownNumbers = false; // Toggle for blocking unknown numbers
List<String> _blockedNumbers = []; // List of blocked numbers
final TextEditingController _numberController = TextEditingController();
@override
void initState() {
super.initState();
_loadBlockedNumbers();
_loadPreferences(); // Load data on initialization
}
Future<void> _loadBlockedNumbers() async {
final numbers = await BlockService().getBlockedNumbers();
// Load preferences from local storage
Future<void> _loadPreferences() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_blockedNumbers = numbers;
_loading = false;
_blockUnknownNumbers = prefs.getBool('blockUnknownNumbers') ?? false;
_blockedNumbers = prefs.getStringList('blockedNumbers') ?? [];
});
}
Future<void> _removeBlockedNumber(String number) async {
await BlockService().unblockNumber(number);
await _loadBlockedNumbers();
}
Future<void> _addBlockedNumber(String number) async {
await BlockService().blockNumber(number);
await _loadBlockedNumbers();
// Save preferences to local storage
Future<void> _savePreferences() async {
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('blockUnknownNumbers', _blockUnknownNumbers);
await prefs.setStringList('blockedNumbers', _blockedNumbers);
}
@override
Widget build(BuildContext context) {
if (_loading) {
return const Scaffold(
backgroundColor: Colors.black,
body: Center(child: CircularProgressIndicator()),
);
}
return Scaffold(
backgroundColor: Colors.black,
appBar: AppBar(
title: const Text('Blocked Numbers'),
),
body: _blockedNumbers.isEmpty
? const Center(
child: Text(
'No blocked numbers',
style: TextStyle(color: Colors.white70),
),
)
: ListView.builder(
itemCount: _blockedNumbers.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(
_blockedNumbers[index],
style: const TextStyle(color: Colors.white),
),
trailing: IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () => _removeBlockedNumber(_blockedNumbers[index]),
),
);
},
body: ListView(
padding: const EdgeInsets.all(16),
children: [
SwitchListTile(
title: const Text(
'Block Unknown Numbers',
style: TextStyle(color: Colors.white),
),
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.add),
onPressed: () {
showDialog(
context: context,
builder: (BuildContext context) {
final TextEditingController controller = TextEditingController();
return AlertDialog(
backgroundColor: Colors.grey[900],
title: const Text('Block a Number', style: TextStyle(color: Colors.white)),
content: TextField(
controller: controller,
style: const TextStyle(color: Colors.white),
decoration: const InputDecoration(
hintText: 'Enter phone number',
hintStyle: TextStyle(color: Colors.grey),
),
keyboardType: TextInputType.phone,
),
actions: [
TextButton(
child: const Text('Cancel'),
onPressed: () => Navigator.of(context).pop(),
),
TextButton(
child: const Text('Block'),
onPressed: () {
if (controller.text.trim().isNotEmpty) {
_addBlockedNumber(controller.text.trim());
Navigator.of(context).pop();
}
},
),
],
);
value: _blockUnknownNumbers,
onChanged: (bool value) {
setState(() {
_blockUnknownNumbers = value;
_savePreferences(); // Save the state to local storage
});
},
);
},
),
const SizedBox(height: 16),
ListTile(
title: const Text(
'Blocked Numbers',
style: TextStyle(color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold),
),
subtitle: _blockedNumbers.isEmpty
? const Text(
'No blocked numbers',
style: TextStyle(color: Colors.grey),
)
: null,
),
..._blockedNumbers.map(
(number) => ListTile(
title: Text(
number,
style: const TextStyle(color: Colors.white),
),
trailing: IconButton(
icon: const Icon(Icons.delete, color: Colors.red),
onPressed: () => _unblockNumber(number),
),
),
),
const Divider(color: Colors.grey),
ListTile(
title: const Text(
'Block a Number',
style: TextStyle(color: Colors.white),
),
trailing: const Icon(Icons.add, color: Colors.white),
onTap: () => _showBlockNumberDialog(),
),
],
),
);
}
// Function to block a number
void _blockNumber(String number) {
if (number.isNotEmpty && !_blockedNumbers.contains(number)) {
setState(() {
_blockedNumbers.add(number);
_savePreferences(); // Save the updated list
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$number has been blocked')),
);
}
}
// Function to unblock a number
void _unblockNumber(String number) {
setState(() {
_blockedNumbers.remove(number);
_savePreferences(); // Save the updated list
});
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('$number has been unblocked')),
);
}
// Dialog for blocking a new number
void _showBlockNumberDialog() {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Colors.grey[900],
title: const Text('Block a Number', style: TextStyle(color: Colors.white)),
content: TextField(
controller: _numberController,
keyboardType: TextInputType.phone,
decoration: const InputDecoration(
hintText: 'Enter number',
hintStyle: TextStyle(color: Colors.grey),
),
style: const TextStyle(color: Colors.white),
),
actions: [
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Cancel', style: TextStyle(color: Colors.white)),
),
TextButton(
onPressed: () {
_blockNumber(_numberController.text);
_numberController.clear();
Navigator.pop(context);
},
child: const Text('Block', style: TextStyle(color: Colors.red)),
),
],
);
},
);
}
@override
void dispose() {
_numberController.dispose();
super.dispose();
}
}