From ba764db9a1b409556b6585a51fde58a6da10e9ef Mon Sep 17 00:00:00 2001 From: AlexisDanlos <91090088+AlexisDanlos@users.noreply.github.com> Date: Sun, 16 Mar 2025 15:36:51 +0100 Subject: [PATCH 1/2] fix: improve history page performance and state management --- dialer/lib/features/history/history_page.dart | 17 ++++++++++++----- dialer/lib/features/home/home_page.dart | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/dialer/lib/features/history/history_page.dart b/dialer/lib/features/history/history_page.dart index 117d1e8..bc91e3f 100644 --- a/dialer/lib/features/history/history_page.dart +++ b/dialer/lib/features/history/history_page.dart @@ -12,6 +12,7 @@ import 'package:dialer/widgets/username_color_generator.dart'; import '../../services/block_service.dart'; import '../contacts/widgets/contact_modal.dart'; import '../../services/call_service.dart'; +import 'package:flutter/foundation.dart'; // if needed class History { final Contact contact; @@ -37,7 +38,7 @@ class HistoryPage extends StatefulWidget { } class _HistoryPageState extends State - with SingleTickerProviderStateMixin { + with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin { List histories = []; bool loading = true; int? _expandedIndex; @@ -47,10 +48,13 @@ class _HistoryPageState extends State // Create a MethodChannel instance. static const MethodChannel _channel = MethodChannel('com.example.calllog'); + @override + bool get wantKeepAlive => true; // Preserve state when switching pages + @override void didChangeDependencies() { super.didChangeDependencies(); - if (loading) { + if (loading && histories.isEmpty) { _buildHistories(); } } @@ -149,9 +153,9 @@ class _HistoryPageState extends State List contacts = contactState.contacts; List callHistories = []; - // Process each log entry. - for (var entry in nativeLogs) { - // Each entry is a Map with keys: number, type, date, duration. + // Process each log entry with intermittent yields to avoid freezing. + for (int i = 0; i < nativeLogs.length; i++) { + final entry = nativeLogs[i]; final String number = entry['number'] ?? ''; if (number.isEmpty) continue; @@ -197,6 +201,8 @@ class _HistoryPageState extends State callHistories .add(History(matchedContact, callDate, callType, callStatus, 1)); + // Yield every 10 iterations to avoid blocking the UI. + if (i % 10 == 0) await Future.delayed(Duration(milliseconds: 1)); } // Sort histories by most recent. @@ -272,6 +278,7 @@ class _HistoryPageState extends State @override Widget build(BuildContext context) { + super.build(context); // required due to AutomaticKeepAliveClientMixin final contactState = ContactState.of(context); if (loading || contactState.loading) { diff --git a/dialer/lib/features/home/home_page.dart b/dialer/lib/features/home/home_page.dart index 65adaa8..dcffb07 100644 --- a/dialer/lib/features/home/home_page.dart +++ b/dialer/lib/features/home/home_page.dart @@ -25,7 +25,7 @@ class _MyHomePageState extends State void initState() { super.initState(); // Set the TabController length to 4 - _tabController = TabController(length: 4, vsync: this, initialIndex: 1); + _tabController = TabController(length: 4, vsync: this, initialIndex: 2); _tabController.addListener(_handleTabIndex); _fetchContacts(); } -- 2.45.2 From 16d6c4685d44a32ef9f264231bb59320767c0cec Mon Sep 17 00:00:00 2001 From: stcb <21@stcb.cc> Date: Sat, 5 Apr 2025 11:26:59 +0300 Subject: [PATCH 2/2] clean unused import --- dialer/lib/features/history/history_page.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/dialer/lib/features/history/history_page.dart b/dialer/lib/features/history/history_page.dart index bc91e3f..e53ab49 100644 --- a/dialer/lib/features/history/history_page.dart +++ b/dialer/lib/features/history/history_page.dart @@ -12,7 +12,6 @@ import 'package:dialer/widgets/username_color_generator.dart'; import '../../services/block_service.dart'; import '../contacts/widgets/contact_modal.dart'; import '../../services/call_service.dart'; -import 'package:flutter/foundation.dart'; // if needed class History { final Contact contact; -- 2.45.2