Skip to content

Commit

Permalink
[Presentation] [Application] Moved some bool logic out of the splash …
Browse files Browse the repository at this point in the history
…page
  • Loading branch information
Wolfteam committed Nov 6, 2022
1 parent 3e23e61 commit ed4f985
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 36 deletions.
72 changes: 69 additions & 3 deletions lib/application/splash/splash_bloc.dart
Expand Up @@ -38,23 +38,36 @@ class SplashBloc extends Bloc<SplashEvent, SplashState> {
final noResourcesHasBeenDownloaded = _settingsService.noResourcesHasBeenDownloaded;
//This is just to trigger a change in the ui
if (event.retry) {
const type = AppResourceUpdateResultType.retrying;
yield SplashState.loaded(
updateResultType: AppResourceUpdateResultType.retrying,
updateResultType: type,
language: _language,
noResourcesHasBeenDownloaded: noResourcesHasBeenDownloaded,
isUpdating: _isUpdating(type),
isLoading: _isLoading(type),
updateFailed: _updateFailed(type),
canSkipUpdate: _canSkipUpdate(type),
noInternetConnectionOnFirstInstall: _noInternetConnectionOnFirstInstall(type),
needsLatestAppVersionOnFirstInstall: _needsLatestAppVersionOnFirstInstall(type),
);
await Future.delayed(const Duration(seconds: 1));
}

final result = await _resourceService.checkForUpdates(_deviceInfoService.version, _settingsService.resourceVersion);
final unknownErrorOnFirstInstall = result.type == AppResourceUpdateResultType.unknownError && _settingsService.noResourcesHasBeenDownloaded;
final unknownErrorOnFirstInstall = _unknownErrorOnFirstInstall(result.type);
final resultType = unknownErrorOnFirstInstall ? AppResourceUpdateResultType.unknownErrorOnFirstInstall : result.type;
await _telemetryService.trackCheckForResourceUpdates(resultType);
yield SplashState.loaded(
updateResultType: resultType,
language: _language,
result: result,
noResourcesHasBeenDownloaded: noResourcesHasBeenDownloaded,
isUpdating: _isUpdating(resultType),
isLoading: _isLoading(resultType),
updateFailed: _updateFailed(resultType),
canSkipUpdate: _canSkipUpdate(resultType),
noInternetConnectionOnFirstInstall: _noInternetConnectionOnFirstInstall(resultType),
needsLatestAppVersionOnFirstInstall: _needsLatestAppVersionOnFirstInstall(resultType),
);
return;
}
Expand All @@ -63,7 +76,17 @@ class SplashBloc extends Bloc<SplashEvent, SplashState> {
assert(state is _LoadedState, 'The current state should be loaded');
final currentState = state as _LoadedState;
assert(currentState.result != null, 'The update result must not be null');
yield currentState.copyWith(updateResultType: AppResourceUpdateResultType.updating);

const type = AppResourceUpdateResultType.updating;
yield currentState.copyWith(
updateResultType: type,
isUpdating: _isUpdating(type),
isLoading: _isLoading(type),
updateFailed: _updateFailed(type),
canSkipUpdate: _canSkipUpdate(type),
noInternetConnectionOnFirstInstall: _noInternetConnectionOnFirstInstall(type),
needsLatestAppVersionOnFirstInstall: _needsLatestAppVersionOnFirstInstall(type),
);

//the stream is required to avoid blocking the bloc
final result = currentState.result!;
Expand Down Expand Up @@ -113,6 +136,12 @@ class SplashBloc extends Bloc<SplashEvent, SplashState> {
language: _language,
progress: 100,
noResourcesHasBeenDownloaded: _settingsService.noResourcesHasBeenDownloaded,
isUpdating: _isUpdating(appliedResult),
isLoading: _isLoading(appliedResult),
updateFailed: _updateFailed(appliedResult),
canSkipUpdate: _canSkipUpdate(appliedResult),
noInternetConnectionOnFirstInstall: _noInternetConnectionOnFirstInstall(appliedResult),
needsLatestAppVersionOnFirstInstall: _needsLatestAppVersionOnFirstInstall(appliedResult),
);
}
}
Expand All @@ -122,4 +151,41 @@ class SplashBloc extends Bloc<SplashEvent, SplashState> {
await _downloadStream?.cancel();
return super.close();
}

bool _isLoading(AppResourceUpdateResultType type) {
return type == AppResourceUpdateResultType.noUpdatesAvailable ||
type == AppResourceUpdateResultType.retrying ||
type == AppResourceUpdateResultType.updated;
}

bool _isUpdating(AppResourceUpdateResultType type) => type == AppResourceUpdateResultType.updating;

bool _updateFailed(AppResourceUpdateResultType type) {
return type == AppResourceUpdateResultType.unknownError ||
type == AppResourceUpdateResultType.unknownErrorOnFirstInstall ||
type == AppResourceUpdateResultType.noInternetConnection ||
type == AppResourceUpdateResultType.apiIsUnavailable ||
_noInternetConnectionOnFirstInstall(type) ||
_needsLatestAppVersionOnFirstInstall(type);
}

bool _noInternetConnectionOnFirstInstall(AppResourceUpdateResultType type) {
return type == AppResourceUpdateResultType.noInternetConnectionForFirstInstall ||
(_settingsService.noResourcesHasBeenDownloaded && type == AppResourceUpdateResultType.noInternetConnection);
}

bool _needsLatestAppVersionOnFirstInstall(AppResourceUpdateResultType type) =>
_settingsService.noResourcesHasBeenDownloaded && type == AppResourceUpdateResultType.needsLatestAppVersion;

bool _canSkipUpdate(AppResourceUpdateResultType type) {
return type != AppResourceUpdateResultType.unknownErrorOnFirstInstall &&
!_noInternetConnectionOnFirstInstall(type) &&
!_needsLatestAppVersionOnFirstInstall(type) &&
!_settingsService.noResourcesHasBeenDownloaded;
}

bool _unknownErrorOnFirstInstall(AppResourceUpdateResultType type) {
return (type == AppResourceUpdateResultType.unknownError || type == AppResourceUpdateResultType.apiIsUnavailable) &&
_settingsService.noResourcesHasBeenDownloaded;
}
}
6 changes: 6 additions & 0 deletions lib/application/splash/splash_state.dart
Expand Up @@ -8,6 +8,12 @@ class SplashState with _$SplashState {
required AppResourceUpdateResultType updateResultType,
required LanguageModel language,
required bool noResourcesHasBeenDownloaded,
required bool isLoading,
required bool isUpdating,
required bool updateFailed,
required bool noInternetConnectionOnFirstInstall,
required bool needsLatestAppVersionOnFirstInstall,
required bool canSkipUpdate,
CheckForUpdatesResult? result,
@Default(0) double progress,
}) = _LoadedState;
Expand Down
2 changes: 1 addition & 1 deletion lib/l10n/intl_en.arb
Expand Up @@ -459,7 +459,7 @@
"resourceUpdateCompleted": "Resource update was successfully applied",
"internetRequiredToUpdate": "Make sure you are connected to a stable internet connection",
"doNotCloseAppWhileUpdating": "Do not close the app while the update takes place",
"unknownErrorWhileUpdating": "Unknown error occurred while checking updates",
"unknownErrorWhileUpdating": "Unknown error occurred while checking for updates",
"newAppVersionInStore": "A new version of the app should be available in the store",
"beforeSendingEmailMsg": "Before sending an email, make sure you write in either english or spanish.\nIf you write in any other language I may not answer you.",
"checkForResourceUpdates": "Check for resource updates",
Expand Down
Expand Up @@ -74,6 +74,7 @@ class CheckForResourceUpdatesDialog extends StatelessWidget {
case AppResourceUpdateResultType.unknownError:
case AppResourceUpdateResultType.unknownErrorOnFirstInstall:
return s.unknownError;
case AppResourceUpdateResultType.apiIsUnavailable:
case AppResourceUpdateResultType.noUpdatesAvailable:
return '${s.noUpdatesAvailable}\n${s.tryAgainLater}';
case AppResourceUpdateResultType.needsLatestAppVersion:
Expand Down
77 changes: 45 additions & 32 deletions lib/presentation/splash/splash_page.dart
Expand Up @@ -41,7 +41,12 @@ class SplashPage extends StatelessWidget {
},
builder: (context, state) => _SplashPage(
updateResultType: state.maybeMap(loaded: (state) => state.updateResultType, orElse: () => null),
noResourcesHasBeenDownloaded: state.maybeMap(loaded: (state) => state.noResourcesHasBeenDownloaded, orElse: () => true),
isUpdating: state.maybeMap(loaded: (state) => state.isUpdating, orElse: () => false),
isLoading: state.maybeMap(loaded: (state) => state.isLoading, orElse: () => true),
updateFailed: state.maybeMap(loaded: (state) => state.updateFailed, orElse: () => false),
canSkipUpdate: state.maybeMap(loaded: (state) => state.canSkipUpdate, orElse: () => false),
needsLatestAppVersionOnFirstInstall: state.maybeMap(loaded: (state) => state.needsLatestAppVersionOnFirstInstall, orElse: () => false),
noInternetConnectionOnFirstInstall: state.maybeMap(loaded: (state) => state.noInternetConnectionOnFirstInstall, orElse: () => false),
),
),
),
Expand All @@ -57,6 +62,11 @@ class SplashPage extends StatelessWidget {
bool initMain = false;
switch (updateResultType) {
case AppResourceUpdateResultType.needsLatestAppVersion:
if (noResourcesHasBeenDownloaded) {
break;
}
initMain = true;
break;
case AppResourceUpdateResultType.noUpdatesAvailable:
case AppResourceUpdateResultType.updated:
initMain = true;
Expand Down Expand Up @@ -89,6 +99,7 @@ class SplashPage extends StatelessWidget {
case AppResourceUpdateResultType.noInternetConnectionForFirstInstall:
case AppResourceUpdateResultType.unknownErrorOnFirstInstall:
case AppResourceUpdateResultType.unknownError:
case AppResourceUpdateResultType.apiIsUnavailable:
break;
}

Expand All @@ -110,34 +121,22 @@ class SplashPage extends StatelessWidget {

class _SplashPage extends StatelessWidget {
final AppResourceUpdateResultType? updateResultType;
final bool noResourcesHasBeenDownloaded;

bool get isLoading =>
updateResultType == null ||
updateResultType == AppResourceUpdateResultType.noUpdatesAvailable ||
updateResultType == AppResourceUpdateResultType.needsLatestAppVersion ||
updateResultType == AppResourceUpdateResultType.retrying ||
updateResultType == AppResourceUpdateResultType.updated;

bool get isUpdating => updateResultType == AppResourceUpdateResultType.updating;

bool get updateFailed =>
updateResultType == AppResourceUpdateResultType.unknownError ||
updateResultType == AppResourceUpdateResultType.unknownErrorOnFirstInstall ||
updateResultType == AppResourceUpdateResultType.noInternetConnection ||
noInternetConnectionOnFirstInstall;

bool get noInternetConnectionOnFirstInstall => updateResultType == AppResourceUpdateResultType.noInternetConnectionForFirstInstall;

bool get canContinue =>
updateResultType != AppResourceUpdateResultType.unknownErrorOnFirstInstall &&
!noInternetConnectionOnFirstInstall &&
!noResourcesHasBeenDownloaded;
final bool isLoading;
final bool isUpdating;
final bool updateFailed;
final bool noInternetConnectionOnFirstInstall;
final bool needsLatestAppVersionOnFirstInstall;
final bool canSkipUpdate;

const _SplashPage({
Key? key,
this.updateResultType,
required this.noResourcesHasBeenDownloaded,
required this.isLoading,
required this.isUpdating,
required this.updateFailed,
required this.noInternetConnectionOnFirstInstall,
required this.needsLatestAppVersionOnFirstInstall,
required this.canSkipUpdate,
}) : super(key: key);

@override
Expand All @@ -161,7 +160,7 @@ class _SplashPage extends StatelessWidget {
child: const CircularProgressIndicator(valueColor: AlwaysStoppedAnimation<Color>(Colors.white)),
),
if (isUpdating) const _Updating(),
if (updateFailed) _Buttons(updateResultType: updateResultType, canContinue: canContinue),
if (updateFailed) _Buttons(updateResultType: updateResultType, canSkipUpdate: canSkipUpdate),
],
),
);
Expand All @@ -170,12 +169,12 @@ class _SplashPage extends StatelessWidget {

class _Buttons extends StatelessWidget {
final AppResourceUpdateResultType? updateResultType;
final bool canContinue;
final bool canSkipUpdate;

const _Buttons({
Key? key,
this.updateResultType,
required this.canContinue,
required this.canSkipUpdate,
}) : super(key: key);

@override
Expand All @@ -195,9 +194,7 @@ class _Buttons extends StatelessWidget {
style: theme.textTheme.subtitle1!.copyWith(color: Colors.white, fontWeight: FontWeight.bold),
),
Text(
updateResultType == AppResourceUpdateResultType.unknownError || updateResultType == AppResourceUpdateResultType.unknownErrorOnFirstInstall
? s.unknownErrorWhileUpdating
: s.internetRequiredToUpdate,
_getErrorMsg(s),
textAlign: TextAlign.center,
style: theme.textTheme.labelMedium!.copyWith(color: Colors.white),
),
Expand All @@ -210,7 +207,7 @@ class _Buttons extends StatelessWidget {
label: Text(s.retry, style: const TextStyle(color: Colors.white)),
style: buttonStyle,
),
if (canContinue)
if (canSkipUpdate)
OutlinedButton.icon(
onPressed: () => context.read<MainBloc>().add(MainEvent.init(updateResultType: updateResultType)),
icon: const Icon(Icons.arrow_right_alt, color: Colors.white),
Expand All @@ -223,6 +220,22 @@ class _Buttons extends StatelessWidget {
),
);
}

String _getErrorMsg(S s) {
if (updateResultType == AppResourceUpdateResultType.unknownError || updateResultType == AppResourceUpdateResultType.unknownErrorOnFirstInstall) {
return '${s.unknownErrorWhileUpdating}\n${s.tryAgainLater}';
}

if (updateResultType == AppResourceUpdateResultType.apiIsUnavailable) {
return s.tryAgainLater;
}

if (updateResultType == AppResourceUpdateResultType.needsLatestAppVersion) {
return '${s.newAppVersionInStore}\n${s.tryAgainLater}';
}

return s.internetRequiredToUpdate;
}
}

class _Updating extends StatelessWidget {
Expand Down

0 comments on commit ed4f985

Please sign in to comment.