Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
upgrade flutter bloc
  • Loading branch information
Nikolas Rimikis committed Mar 30, 2023
commit 09859c8f45fc5a34acd01fdc5da80aed32023edb
49 changes: 24 additions & 25 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_settings_screens/flutter_settings_screens.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/authentication/authentication.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/categories/categories.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/recipes_short/recipes_short.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/authentication/authentication_bloc.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/categories/categories_bloc.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/recipes_short/recipes_short_bloc.dart';
import 'package:nextcloud_cookbook_flutter/src/blocs/simple_bloc_delegatae.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/category/category_screen.dart';
import 'package:nextcloud_cookbook_flutter/src/screens/loading_screen.dart';
Expand Down Expand Up @@ -39,7 +39,7 @@ void main() async {
providers: [
BlocProvider<AuthenticationBloc>(
create: (context) {
return AuthenticationBloc()..add(AppStarted());
return AuthenticationBloc()..add(const AppStarted());
},
),
BlocProvider<RecipesShortBloc>(
Expand Down Expand Up @@ -106,27 +106,26 @@ class _AppState extends State<App> {
),
home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
builder: (context, state) {
if (state is AuthenticationUninitialized) {
return const SplashPage();
} else if (state is AuthenticationAuthenticated) {
IntentRepository().handleIntent();
if (BlocProvider.of<CategoriesBloc>(context).state
is CategoriesInitial) {
BlocProvider.of<CategoriesBloc>(context)
.add(CategoriesLoaded());
}
return const CategoryScreen();
} else if (state is AuthenticationUnauthenticated) {
return const LoginScreen();
} else if (state is AuthenticationInvalid) {
return const LoginScreen(
invalidCredentials: true,
);
} else if (state is AuthenticationLoading ||
state is AuthenticationError) {
return const LoadingScreen();
} else {
return const LoadingScreen();
switch (state.status) {
case AuthenticationStatus.uninitialized:
return const SplashPage();
case AuthenticationStatus.authenticated:
IntentRepository().handleIntent();
if (BlocProvider.of<CategoriesBloc>(context).state.status ==
CategoriesStatus.initial) {
BlocProvider.of<CategoriesBloc>(context)
.add(const CategoriesLoaded());
}
return const CategoryScreen();
case AuthenticationStatus.unauthenticated:
return const LoginScreen();
case AuthenticationStatus.invalid:
return const LoginScreen(
invalidCredentials: true,
);
case AuthenticationStatus.loading:
case AuthenticationStatus.error:
return const LoadingScreen();
}
},
),
Expand Down
3 changes: 0 additions & 3 deletions lib/src/blocs/authentication/authentication.dart

This file was deleted.

94 changes: 54 additions & 40 deletions lib/src/blocs/authentication/authentication_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
import 'dart:async';

import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:nextcloud_cookbook_flutter/src/blocs/authentication/authentication.dart';
import 'package:nextcloud_cookbook_flutter/src/models/app_authentication.dart';
import 'package:nextcloud_cookbook_flutter/src/services/user_repository.dart';

part 'authentication_event.dart';
part 'authentication_state.dart';

class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
final UserRepository userRepository = UserRepository();

AuthenticationBloc() : super(AuthenticationUninitialized());
AuthenticationBloc() : super(AuthenticationState()) {
on<AppStarted>(_mapAppStartedEventToState);
on<LoggedIn>(_mapLoggedInEventToState);
on<LoggedOut>(_mapLoggedOutEventToState);
}

@override
Stream<AuthenticationState> mapEventToState(
AuthenticationEvent event,
) async* {
if (event is AppStarted) {
final bool hasToken = await userRepository.hasAppAuthentication();
Future<void> _mapAppStartedEventToState(
AppStarted event,
Emitter<AuthenticationState> emit,
) async {
final bool hasToken = await userRepository.hasAppAuthentication();

if (hasToken) {
yield AuthenticationLoading();
await userRepository.loadAppAuthentication();
bool validCredentials = false;
try {
validCredentials = await userRepository.checkAppAuthentication();
} catch (e) {
yield AuthenticationError(e.toString());
return;
}
if (validCredentials) {
await userRepository.fetchApiVersion();
yield AuthenticationAuthenticated();
} else {
await userRepository.deleteAppAuthentication();
yield AuthenticationInvalid();
}
if (hasToken) {
emit(AuthenticationState(status: AuthenticationStatus.loading));
await userRepository.loadAppAuthentication();
bool validCredentials = false;
try {
validCredentials = await userRepository.checkAppAuthentication();
} catch (e) {
emit(
AuthenticationState(
status: AuthenticationStatus.error,
error: e.toString(),
),
);
return;
}
if (validCredentials) {
await userRepository.fetchApiVersion();
emit(AuthenticationState(status: AuthenticationStatus.authenticated));
} else {
yield AuthenticationUnauthenticated();
await userRepository.deleteAppAuthentication();
emit(AuthenticationState(status: AuthenticationStatus.invalid));
}
} else {
emit(AuthenticationState(status: AuthenticationStatus.unauthenticated));
}
}

if (event is LoggedIn) {
yield AuthenticationLoading();
await userRepository.persistAppAuthentication(event.appAuthentication);
await userRepository.fetchApiVersion();
yield AuthenticationAuthenticated();
}
Future<void> _mapLoggedInEventToState(
LoggedIn event,
Emitter<AuthenticationState> emit,
) async {
emit(AuthenticationState(status: AuthenticationStatus.loading));
await userRepository.persistAppAuthentication(event.appAuthentication);
await userRepository.fetchApiVersion();
emit(AuthenticationState(status: AuthenticationStatus.authenticated));
}

if (event is LoggedOut) {
yield AuthenticationLoading();
await userRepository.deleteAppAuthentication();
yield AuthenticationUnauthenticated();
}
Future<void> _mapLoggedOutEventToState(
LoggedOut event,
Emitter<AuthenticationState> emit,
) async {
emit(AuthenticationState(status: AuthenticationStatus.loading));
await userRepository.deleteAppAuthentication();
emit(AuthenticationState(status: AuthenticationStatus.unauthenticated));
}
}
11 changes: 7 additions & 4 deletions lib/src/blocs/authentication/authentication_event.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:nextcloud_cookbook_flutter/src/models/app_authentication.dart';
part of 'authentication_bloc.dart';

abstract class AuthenticationEvent extends Equatable {
const AuthenticationEvent();
Expand All @@ -8,7 +7,9 @@ abstract class AuthenticationEvent extends Equatable {
List<Object> get props => [];
}

class AppStarted extends AuthenticationEvent {}
class AppStarted extends AuthenticationEvent {
const AppStarted();
}

class LoggedIn extends AuthenticationEvent {
final AppAuthentication appAuthentication;
Expand All @@ -22,4 +23,6 @@ class LoggedIn extends AuthenticationEvent {
String toString() => appAuthentication.toString();
}

class LoggedOut extends AuthenticationEvent {}
class LoggedOut extends AuthenticationEvent {
const LoggedOut();
}
41 changes: 20 additions & 21 deletions lib/src/blocs/authentication/authentication_state.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import 'package:equatable/equatable.dart';

abstract class AuthenticationState extends Equatable {
const AuthenticationState();

@override
List<Object> get props => [];
part of 'authentication_bloc.dart';

enum AuthenticationStatus {
unauthenticated,
uninitialized,
authenticated,
invalid,
loading,
error;
}

class AuthenticationUninitialized extends AuthenticationState {}

class AuthenticationAuthenticated extends AuthenticationState {}
class AuthenticationState extends Equatable {
final AuthenticationStatus status;
final String? error;

class AuthenticationUnauthenticated extends AuthenticationState {}

class AuthenticationInvalid extends AuthenticationState {}

class AuthenticationError extends AuthenticationState {
final String errorMsg;

const AuthenticationError(this.errorMsg);
const AuthenticationState({
this.status = AuthenticationStatus.uninitialized,
this.error,
}) : assert(
(status != AuthenticationStatus.error && error == null) ||
(status == AuthenticationStatus.error && error != null),
);

@override
List<Object> get props => [errorMsg];
List<Object?> get props => [status, error];
}

class AuthenticationLoading extends AuthenticationState {}
3 changes: 0 additions & 3 deletions lib/src/blocs/categories/categories.dart

This file was deleted.

43 changes: 29 additions & 14 deletions lib/src/blocs/categories/categories_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import 'package:nextcloud_cookbook_flutter/src/blocs/categories/categories.dart';
import 'package:nextcloud_cookbook_flutter/src/models/category.dart';
import 'package:nextcloud_cookbook_flutter/src/services/data_repository.dart';

part 'categories_event.dart';
part 'categories_state.dart';

class CategoriesBloc extends Bloc<CategoriesEvent, CategoriesState> {
final DataRepository dataRepository = DataRepository();

CategoriesBloc() : super(CategoriesInitial());

@override
Stream<CategoriesState> mapEventToState(CategoriesEvent event) async* {
if (event is CategoriesLoaded) {
yield* _mapCategoriesLoadedToState();
}
CategoriesBloc() : super(CategoriesState()) {
on<CategoriesLoaded>(_mapCategoriesLoadedEventToState);
}

Stream<CategoriesState> _mapCategoriesLoadedToState() async* {
Future<void> _mapCategoriesLoadedEventToState(
CategoriesLoaded event,
Emitter<CategoriesState> emit,
) async {
try {
yield CategoriesLoadInProgress();
emit(CategoriesState(status: CategoriesStatus.loadInProgress));
final List<Category> categories = await dataRepository.fetchCategories();
dataRepository.updateCategoryNames(categories);
yield CategoriesLoadSuccess(categories: categories);
emit(
CategoriesState(
status: CategoriesStatus.loadSuccess,
categories: categories,
),
);
final List<Category> categoriesWithImage =
await dataRepository.fetchCategoryMainRecipes(categories);
yield CategoriesImageLoadSuccess(categories: categoriesWithImage);
emit(
CategoriesState(
status: CategoriesStatus.imageLoadSuccess,
categories: categoriesWithImage,
),
);
} on Exception catch (e) {
yield CategoriesLoadFailure(e.toString());
emit(
CategoriesState(
status: CategoriesStatus.loadFailure,
error: e.toString(),
),
);
}
}
}
6 changes: 4 additions & 2 deletions lib/src/blocs/categories/categories_event.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:equatable/equatable.dart';
part of 'categories_bloc.dart';

abstract class CategoriesEvent extends Equatable {
const CategoriesEvent();
Expand All @@ -7,4 +7,6 @@ abstract class CategoriesEvent extends Equatable {
List<Object> get props => [];
}

class CategoriesLoaded extends CategoriesEvent {}
class CategoriesLoaded extends CategoriesEvent {
const CategoriesLoaded();
}
Loading