-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Nuxify/feature/refactor-api-cubit
feat: refactor api cubit
- Loading branch information
Showing
10 changed files
with
119 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:flirt/core/application/service/cubit/quote_dto.dart'; | ||
import 'package:flirt/core/domain/models/api_error_response.dart'; | ||
import 'package:flirt/core/domain/models/api_response.dart'; | ||
import 'package:flirt/core/domain/models/quote/quote_response.dart'; | ||
import 'package:flirt/core/domain/repository/quote_repository.dart'; | ||
import 'package:flirt/internal/utils.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
part 'quote_api_state.dart'; | ||
|
||
class QuoteAPICubit extends Cubit<QuoteAPIState> { | ||
QuoteAPICubit({required this.quoteRepository}) : super(QuoteAPIState()); | ||
final IQuoteRepository quoteRepository; | ||
|
||
/// Get Quote | ||
Future<void> fetchQuote() async { | ||
try { | ||
emit(FetchQuoteLoading()); | ||
|
||
final APIListResponse<QuoteResponse> response = | ||
await quoteRepository.fetchQuote(); | ||
|
||
final QuoteResponseDTO quote = QuoteResponseDTO( | ||
author: response.data.first.author, | ||
content: response.data.first.quote, | ||
); | ||
|
||
// emit FetchQuoteSuccess event | ||
// this set `quotes` in base class and emits an event | ||
emit(FetchQuoteSuccess(quote)); | ||
} on APIErrorResponse catch (error) { | ||
emit( | ||
FetchQuoteFailed( | ||
errorCode: error.errorCode!, | ||
message: error.message, | ||
), | ||
); | ||
} catch (e, stackTrace) { | ||
logError(e, stackTrace); | ||
|
||
emit( | ||
FetchQuoteFailed( | ||
errorCode: '$e', | ||
message: 'Something went wrong.', | ||
), | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
part of 'quote_api_cubit.dart'; | ||
|
||
class QuoteAPIState {} | ||
|
||
class FetchQuoteLoading extends QuoteAPIState {} | ||
|
||
class FetchQuoteSuccess extends QuoteAPIState { | ||
FetchQuoteSuccess(this.quoteResponse); | ||
|
||
final QuoteResponseDTO quoteResponse; | ||
} | ||
|
||
class FetchQuoteFailed extends QuoteAPIState { | ||
FetchQuoteFailed({ | ||
required this.errorCode, | ||
required this.message, | ||
}); | ||
|
||
final String errorCode; | ||
final String message; | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 1 addition & 23 deletions
24
lib/core/module/home/application/service/cubit/home_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,9 @@ | ||
part of 'home_cubit.dart'; | ||
|
||
class HomeState { | ||
const HomeState({ | ||
HomeState({ | ||
required this.data, | ||
}); | ||
|
||
final QuoteStateDTO data; | ||
} | ||
|
||
class FetchQuoteLoading extends HomeState { | ||
FetchQuoteLoading(QuoteStateDTO data) : super(data: data); | ||
} | ||
|
||
class FetchQuoteSuccess extends HomeState { | ||
const FetchQuoteSuccess(QuoteStateDTO data, this.quoteResponse) | ||
: super(data: data); | ||
|
||
final QuoteResponseDTO quoteResponse; | ||
} | ||
|
||
class FetchQuoteFailed extends HomeState { | ||
const FetchQuoteFailed({ | ||
required this.errorCode, | ||
required this.message, | ||
required super.data, | ||
}); | ||
|
||
final String errorCode; | ||
final String message; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.