From 56f8b386930b948076b2b17567bcc406381b485c Mon Sep 17 00:00:00 2001 From: MohammadTaha Basiri Date: Thu, 31 Mar 2022 13:53:36 +0430 Subject: [PATCH] studio error handlign + bug fixes --- lib/views/home/studio/studio.dart | 148 +++++------ .../studio_details/studio_details.mobile.dart | 8 +- lib/views/home/widgets/search_field.dart | 1 + lib/views/widgets/didvan/bnb.dart | 242 +++++++++--------- 4 files changed, 207 insertions(+), 192 deletions(-) diff --git a/lib/views/home/studio/studio.dart b/lib/views/home/studio/studio.dart index e23a38a..79619e8 100644 --- a/lib/views/home/studio/studio.dart +++ b/lib/views/home/studio/studio.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:didvan/config/design_config.dart'; import 'package:didvan/constants/app_icons.dart'; +import 'package:didvan/models/enums.dart'; import 'package:didvan/models/requests/studio.dart'; import 'package:didvan/models/view/action_sheet_data.dart'; import 'package:didvan/routes/routes.dart'; @@ -18,6 +19,7 @@ import 'package:didvan/views/widgets/didvan/divider.dart'; import 'package:didvan/views/widgets/didvan/icon_button.dart'; import 'package:didvan/views/widgets/didvan/radial_button.dart'; import 'package:didvan/views/widgets/item_title.dart'; +import 'package:didvan/views/widgets/state_handlers/empty_result.dart'; import 'package:didvan/views/widgets/state_handlers/sliver_state_handler.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -41,89 +43,91 @@ class _StudioState extends State { @override Widget build(BuildContext context) { - return CustomScrollView( - slivers: [ - SliverToBoxAdapter( - child: Row( - children: [ - const Expanded(child: LogoAppBar(type: 'studio')), - Padding( - padding: - EdgeInsets.only(top: MediaQuery.of(context).padding.top), - child: DidvanIconButton( - icon: DidvanIcons.bookmark_regular, - onPressed: () => Navigator.of(context).pushNamed( - Routes.filteredBookmarks, - arguments: { - 'type': context.read().type, - 'onDeleted': (_) {} - }, - ), - ), - ), - ], - ), - ), - const SliverToBoxAdapter( - child: StudioTabBar(), - ), - SliverToBoxAdapter( - child: Padding( - padding: const EdgeInsets.all(16.0), - child: SearchField( - title: 'استودیو', - onChanged: _onChanged, - focusNode: _focusNode, - ), - ), - ), - SliverToBoxAdapter( - child: Consumer( - builder: (context, state, child) => AnimatedVisibility( - isVisible: !state.searching, - duration: DesignConfig.lowAnimationDuration, - child: const StudioSlider(), - ), - ), - ), - const SliverPadding( - padding: EdgeInsets.symmetric(horizontal: 16), - sliver: SliverToBoxAdapter( - child: DidvanDivider( - verticalPadding: 0, - ), - ), - ), - SliverPadding( - padding: const EdgeInsets.symmetric(horizontal: 16), - sliver: SliverToBoxAdapter( + return Consumer( + builder: (context, state, child) => CustomScrollView( + slivers: [ + SliverToBoxAdapter( child: Row( - mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ - Consumer( - builder: (context, state, child) => AnimatedVisibility( - isVisible: !state.searching, - duration: DesignConfig.lowAnimationDuration, - child: ItemTitle(title: state.orderString), + const Expanded(child: LogoAppBar(type: 'studio')), + Padding( + padding: + EdgeInsets.only(top: MediaQuery.of(context).padding.top), + child: DidvanIconButton( + icon: DidvanIcons.bookmark_regular, + onPressed: () => Navigator.of(context).pushNamed( + Routes.filteredBookmarks, + arguments: {'type': state.type, 'onDeleted': (_) {}}, + ), ), ), - DidvanIconButton( - gestureSize: 36, - icon: DidvanIcons.sort_regular, - onPressed: _showSortDialog, - ), ], ), ), - ), - Consumer( - builder: (context, state, child) => SliverStateHandler( + const SliverToBoxAdapter( + child: StudioTabBar(), + ), + if (state.appState != AppState.failed) + SliverToBoxAdapter( + child: Padding( + padding: const EdgeInsets.all(16.0), + child: SearchField( + title: 'استودیو', + onChanged: _onChanged, + focusNode: _focusNode, + ), + ), + ), + if (state.appState != AppState.failed) + SliverToBoxAdapter( + child: AnimatedVisibility( + isVisible: !state.searching, + duration: DesignConfig.lowAnimationDuration, + child: const StudioSlider(), + ), + ), + if (state.appState != AppState.failed && state.studios.isNotEmpty) + const SliverPadding( + padding: EdgeInsets.symmetric(horizontal: 16), + sliver: SliverToBoxAdapter( + child: DidvanDivider( + verticalPadding: 0, + ), + ), + ), + if (state.appState != AppState.failed && state.studios.isNotEmpty) + SliverPadding( + padding: const EdgeInsets.symmetric(horizontal: 16), + sliver: SliverToBoxAdapter( + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceBetween, + children: [ + AnimatedVisibility( + isVisible: !state.searching, + duration: DesignConfig.lowAnimationDuration, + child: ItemTitle(title: state.orderString), + ), + DidvanIconButton( + gestureSize: 36, + icon: DidvanIcons.sort_regular, + onPressed: _showSortDialog, + ), + ], + ), + ), + ), + SliverStateHandler( state: state, itemPadding: const EdgeInsets.only( bottom: 8, left: 16, right: 16, ), + emptyState: EmptyResult( + onNewSearch: () => _focusNode.requestFocus(), + ), + centerEmptyState: false, + enableEmptyState: state.studios.isEmpty, placeholder: state.videosSelected ? VideoOverview.placeHolder : PodcastOverview.placeholder, @@ -154,8 +158,8 @@ class _StudioState extends State { childCount: state.studios.length, onRetry: () => state.getStudios(page: 1), ), - ), - ], + ], + ), ); } diff --git a/lib/views/home/studio/studio_details/studio_details.mobile.dart b/lib/views/home/studio/studio_details/studio_details.mobile.dart index 4f9e77a..2331fb1 100644 --- a/lib/views/home/studio/studio_details/studio_details.mobile.dart +++ b/lib/views/home/studio/studio_details/studio_details.mobile.dart @@ -86,7 +86,13 @@ class _StudioDetailsState extends State { return Consumer( builder: (context, state, child) => StateHandler( state: state, - onRetry: () => state.getStudioDetails(state.studio.id), + onRetry: () { + try { + state.getStudioDetails(state.studio.id); + } catch (e) { + state.getStudioDetails(widget.pageData['id']); + } + }, builder: (context, state) { return WillPopScope( onWillPop: () async { diff --git a/lib/views/home/widgets/search_field.dart b/lib/views/home/widgets/search_field.dart index dea0aa1..9a979b7 100644 --- a/lib/views/home/widgets/search_field.dart +++ b/lib/views/home/widgets/search_field.dart @@ -125,6 +125,7 @@ class _SearchFieldState extends State { @override void dispose() { + widget.focusNode.removeListener(() {}); super.dispose(); } } diff --git a/lib/views/widgets/didvan/bnb.dart b/lib/views/widgets/didvan/bnb.dart index 5583482..8641ef5 100644 --- a/lib/views/widgets/didvan/bnb.dart +++ b/lib/views/widgets/didvan/bnb.dart @@ -118,134 +118,138 @@ class _PlayerNavBar extends StatelessWidget { ), ), alignment: Alignment.topCenter, - child: !_enablePlayerController(state) - ? const SizedBox() - : MediaService.currentPodcast == null - ? SizedBox( - height: 32, - child: Center( - child: SpinKitThreeBounce( - size: 18, + child: Builder(builder: (context) { + if (!_enablePlayerController(state)) return const SizedBox(); + if (state.appState == AppState.failed) { + Future.delayed(const Duration(seconds: 2), () { + MediaService.resetAudioPlayer(); + }); + return DidvanText( + 'اتصال اینترنت برقرار نمی‌باشد', + color: DesignConfig.isDark + ? Theme.of(context).colorScheme.title + : Theme.of(context).colorScheme.secondCTA, + ); + } + if (MediaService.currentPodcast == null) { + return SizedBox( + height: 32, + child: Center( + child: SpinKitThreeBounce( + size: 18, + color: DesignConfig.isDark + ? Theme.of(context).colorScheme.title + : Theme.of(context).colorScheme.secondCTA, + ), + ), + ); + } + return SizedBox( + height: 56, + child: Row( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Padding( + padding: const EdgeInsets.only( + right: 12, + left: 16, + ), + child: DidvanIconButton( + icon: DidvanIcons.close_regular, + color: DesignConfig.isDark + ? null + : Theme.of(context).colorScheme.secondCTA, + gestureSize: 28, + onPressed: MediaService.resetAudioPlayer, + ), + ), + SkeletonImage( + imageUrl: MediaService.currentPodcast!.image, + width: 32, + height: 32, + ), + const SizedBox(width: 16), + Expanded( + child: Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + DidvanText( + MediaService.currentPodcast!.title, color: DesignConfig.isDark - ? Theme.of(context).colorScheme.title + ? null : Theme.of(context).colorScheme.secondCTA, ), - ), - ) - : SizedBox( - height: 56, - child: Row( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Padding( - padding: const EdgeInsets.only( - right: 12, - left: 16, - ), - child: DidvanIconButton( - icon: DidvanIcons.close_regular, + AudioSlider( + disableThumb: true, + tag: MediaService.audioPlayerTag!, + ), + ], + ), + ), + StreamBuilder( + stream: MediaService.audioPlayer.playerStateStream, + builder: (context, snapshot) { + final playerState = MediaService + .audioPlayer.playerState.processingState; + if (playerState == ProcessingState.loading || + state.appState == AppState.busy) { + return Padding( + padding: const EdgeInsets.only( + top: 4, + left: 16, + right: 16, + ), + child: SizedBox( + height: 18, + width: 18, + child: CircularProgressIndicator( + strokeWidth: 2, color: DesignConfig.isDark - ? null + ? Theme.of(context).colorScheme.title : Theme.of(context).colorScheme.secondCTA, - gestureSize: 28, - onPressed: MediaService.resetAudioPlayer, ), ), - SkeletonImage( - imageUrl: MediaService.currentPodcast!.image, - width: 32, - height: 32, - ), - const SizedBox(width: 16), - Expanded( - child: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - DidvanText( - MediaService.currentPodcast!.title, - color: DesignConfig.isDark - ? null - : Theme.of(context) - .colorScheme - .secondCTA, - ), - AudioSlider( - disableThumb: true, - tag: MediaService.audioPlayerTag!, - ), - ], - ), - ), - StreamBuilder( - stream: - MediaService.audioPlayer.playerStateStream, - builder: (context, snapshot) { - final playerState = MediaService - .audioPlayer.playerState.processingState; - if (playerState == ProcessingState.loading || - state.appState == AppState.busy) { - return Padding( - padding: const EdgeInsets.only( - top: 4, - left: 16, - right: 16, - ), - child: SizedBox( - height: 18, - width: 18, - child: CircularProgressIndicator( - strokeWidth: 2, - color: DesignConfig.isDark - ? Theme.of(context) - .colorScheme - .title - : Theme.of(context) - .colorScheme - .secondCTA, - ), - ), - ); - } - return const SizedBox(); - }, - ), - if (state.appState != AppState.busy && - MediaService.audioPlayer.playerState - .processingState != - ProcessingState.loading) - Padding( - padding: const EdgeInsets.only( - left: 12, - right: 16, - ), - child: DidvanIconButton( - gestureSize: 28, - color: DesignConfig.isDark - ? null - : Theme.of(context).colorScheme.secondCTA, - icon: snapshot.data! - ? DidvanIcons.pause_solid - : DidvanIcons.play_solid, - onPressed: () { - if (state.args.type == 'video') { - state.getStudioDetails( - MediaService.currentPodcast!.id, - args: state.podcastArgs, - forceFetch: true, - ); - } - MediaService.handleAudioPlayback( - audioSource: - MediaService.currentPodcast!.media, - id: MediaService.currentPodcast!.id, - isVoiceMessage: false, - ); - }, - ), - ), - ], + ); + } + return const SizedBox(); + }, + ), + if (state.appState != AppState.busy && + MediaService.audioPlayer.playerState.processingState != + ProcessingState.loading) + Padding( + padding: const EdgeInsets.only( + left: 12, + right: 16, + ), + child: DidvanIconButton( + gestureSize: 28, + color: DesignConfig.isDark + ? null + : Theme.of(context).colorScheme.secondCTA, + icon: snapshot.data! + ? DidvanIcons.pause_solid + : DidvanIcons.play_solid, + onPressed: () { + if (state.args.type == 'video') { + state.getStudioDetails( + MediaService.currentPodcast!.id, + args: state.podcastArgs, + forceFetch: true, + ); + } + MediaService.handleAudioPlayback( + audioSource: MediaService.currentPodcast!.media, + id: MediaService.currentPodcast!.id, + isVoiceMessage: false, + ); + }, ), ), + ], + ), + ); + }), ), ), ),