From cf5e328a27bdc71513a31c8a4aa5e350fe6dc176 Mon Sep 17 00:00:00 2001 From: Naresh Pratista <2141720057@student.polinema.ac.id> Date: Thu, 7 Nov 2024 10:38:56 +0700 Subject: [PATCH] fix(history): optimize history tab initialization and data loading --- .../repositories/history_repository.dart | 3 +- .../history/provider/history_provider.dart | 79 +++++++++---------- .../history/widgets/custom_tab_bar.dart | 6 +- lib/features/home/screens/home_screen.dart | 18 +++-- 4 files changed, 54 insertions(+), 52 deletions(-) diff --git a/lib/core/services/repositories/history_repository.dart b/lib/core/services/repositories/history_repository.dart index f35814d..775a4fc 100644 --- a/lib/core/services/repositories/history_repository.dart +++ b/lib/core/services/repositories/history_repository.dart @@ -16,10 +16,9 @@ class HistoryRepository { sectionId, token, page: 1, - limit: 1000, + limit: 10, ); if (response.statusCode == 200 && response.data != null) { - // Perbaikan disini: langsung mengakses ['payload']['history'] final List historyData = response.data['payload']['history']; if (historyData.isEmpty) { return []; // Mengembalikan list kosong jika tidak ada data diff --git a/lib/features/history/provider/history_provider.dart b/lib/features/history/provider/history_provider.dart index c82d9b5..4a104ee 100644 --- a/lib/features/history/provider/history_provider.dart +++ b/lib/features/history/provider/history_provider.dart @@ -11,6 +11,7 @@ class HistoryProvider with ChangeNotifier { int _selectedPageIndex = 0; bool _isLoading = false; String? _error; + bool _isInitialized = false; HistoryProvider( this._repository, @@ -21,16 +22,49 @@ class HistoryProvider with ChangeNotifier { int get selectedPageIndex => _selectedPageIndex; bool get isLoading => _isLoading; String? get error => _error; + bool get isInitialized => _isInitialized; + + Future loadInitialData(String token) async { + if (_isInitialized) return; // Prevent double initialization + + _isLoading = true; + _error = null; + notifyListeners(); + + try { + // Only fetch sections if they haven't been fetched yet + if (_sectionProvider.sections.isEmpty) { + await _sectionProvider.fetchSections(token); + } + + if (_sectionProvider.sections.isEmpty) { + _error = 'No sections available'; + return; + } + + // Get the first section's ID + String firstSectionId = _sectionProvider.sections.first.id; + + // Fetch history for the first section + _learningHistory = + await _repository.getLearningHistory(firstSectionId, token); + _error = null; + _isInitialized = true; // Mark as initialized + } catch (e) { + _error = 'Error loading data: ${e.toString()}'; + _learningHistory = []; + } finally { + _isLoading = false; + notifyListeners(); + } + } void setSelectedPageIndex(int index) { _selectedPageIndex = index; notifyListeners(); } - Future fetchLearningHistory( - String token, { - bool refresh = false, - }) async { + Future fetchLearningHistory(String token) async { if (_sectionProvider.sections.isEmpty) { _error = 'No sections available'; notifyListeners(); @@ -43,10 +77,7 @@ class HistoryProvider with ChangeNotifier { notifyListeners(); try { - final history = await _repository.getLearningHistory( - sectionId, - token, - ); + final history = await _repository.getLearningHistory(sectionId, token); _learningHistory = history; _error = null; } catch (e) { @@ -133,36 +164,4 @@ class HistoryProvider with ChangeNotifier { notifyListeners(); } } - - // Future loadHistoryData(String token) async { - // _isLoading = true; - // _error = null; - // notifyListeners(); - - // try { - // // Fetch sections and learning history in parallel - // final sectionsResult = _sectionProvider.fetchSections(token); - - // // Use the first section ID for initial history fetch - // final firstSectionId = await sectionsResult - // .then((sections) => sections.isNotEmpty ? sections.first.id : null); - - // if (firstSectionId != null) { - // final historyResult = - // _repository.getLearningHistory(firstSectionId, token); - - // // Wait for both futures to complete - // final results = await Future.wait([sectionsResult, historyResult]); - - // _learningHistory = results[1] as List; - // } else { - // _error = 'No sections available'; - // } - // } catch (e) { - // _error = 'Error loading data: ${e.toString()}'; - // } finally { - // _isLoading = false; - // notifyListeners(); - // } - // } } diff --git a/lib/features/history/widgets/custom_tab_bar.dart b/lib/features/history/widgets/custom_tab_bar.dart index a4fc9b6..c036d84 100644 --- a/lib/features/history/widgets/custom_tab_bar.dart +++ b/lib/features/history/widgets/custom_tab_bar.dart @@ -6,7 +6,7 @@ import 'package:english_learning/core/utils/styles/theme.dart'; import 'package:provider/provider.dart'; class CustomTabBar extends StatelessWidget { - const CustomTabBar({Key? key}) : super(key: key); + const CustomTabBar({super.key}); @override Widget build(BuildContext context) { @@ -36,9 +36,9 @@ class CustomTabBar extends StatelessWidget { void _setSelectedPageIndex(BuildContext context, int index) { final historyProvider = Provider.of(context, listen: false); - historyProvider.setSelectedPageIndex(index); - final userProvider = Provider.of(context, listen: false); + + historyProvider.setSelectedPageIndex(index); historyProvider.fetchLearningHistory(userProvider.jwtToken!); } } diff --git a/lib/features/home/screens/home_screen.dart b/lib/features/home/screens/home_screen.dart index df5eedc..3b4dfd8 100644 --- a/lib/features/home/screens/home_screen.dart +++ b/lib/features/home/screens/home_screen.dart @@ -83,18 +83,22 @@ class _HomeScreenState extends State { gap: 8, selectedIndex: _selectedIndex, onTabChange: (index) async { - setState(() { - _selectedIndex = index; - _pageController.jumpToPage(index); - }); - if (index == 2) { - // Index 2 adalah tab History + if (index == 2 && _selectedIndex != 2) { + // Only if switching TO history tab final historyProvider = Provider.of(context, listen: false); final userProvider = Provider.of(context, listen: false); - await historyProvider.loadHistoryData(userProvider.jwtToken!); + + if (!historyProvider.isInitialized) { + await historyProvider.loadInitialData(userProvider.jwtToken!); + } } + + setState(() { + _selectedIndex = index; + _pageController.jumpToPage(index); + }); }, padding: const EdgeInsets.symmetric( horizontal: 16,