diff --git a/dialer/lib/features/history/history_page.dart b/dialer/lib/features/history/history_page.dart index 117d1e8..e53ab49 100644 --- a/dialer/lib/features/history/history_page.dart +++ b/dialer/lib/features/history/history_page.dart @@ -37,7 +37,7 @@ class HistoryPage extends StatefulWidget { } class _HistoryPageState extends State - with SingleTickerProviderStateMixin { + with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin { List histories = []; bool loading = true; int? _expandedIndex; @@ -47,10 +47,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 +152,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 +200,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 +277,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(); }