Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Application] Added initial donations bloc
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 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
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,63 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:shiori/domain/models/models.dart'; | ||
import 'package:shiori/domain/services/network_service.dart'; | ||
import 'package:shiori/domain/services/purchase_service.dart'; | ||
|
||
part 'donations_bloc.freezed.dart'; | ||
part 'donations_event.dart'; | ||
part 'donations_state.dart'; | ||
|
||
class DonationsBloc extends Bloc<DonationsEvent, DonationsState> { | ||
final PurchaseService _purchaseService; | ||
final NetworkService _networkService; | ||
|
||
static int maxUserIdLength = 20; | ||
|
||
DonationsBloc(this._purchaseService, this._networkService) : super(const DonationsState.loading()); | ||
|
||
@override | ||
Stream<DonationsState> mapEventToState(DonationsEvent event) async* { | ||
if (!await _networkService.isInternetAvailable()) { | ||
yield const DonationsState.initial(packages: [], isInitialized: false, noInternetConnection: true); | ||
return; | ||
} | ||
|
||
if (!await _purchaseService.isPlatformSupported()) { | ||
yield const DonationsState.initial(packages: [], isInitialized: false, noInternetConnection: false); | ||
return; | ||
} | ||
|
||
if (!_purchaseService.isInitialized) { | ||
await _purchaseService.init(); | ||
} | ||
|
||
final currentState = state; | ||
final s = await event.map( | ||
init: (_) => _init(), | ||
restorePurchases: (e) => _restorePurchases(e.userId), | ||
purchase: (e) => _purchase(e), | ||
); | ||
|
||
yield s; | ||
|
||
if ((s is _PurchaseCompleted && s.error) || (s is _RestoreCompleted && s.error)) { | ||
yield currentState; | ||
} | ||
} | ||
|
||
Future<DonationsState> _init() async { | ||
final packages = await _purchaseService.getInAppPurchases(); | ||
return DonationsState.initial(packages: packages, isInitialized: _purchaseService.isInitialized, noInternetConnection: false); | ||
} | ||
|
||
Future<DonationsState> _restorePurchases(String userId) async { | ||
final restored = await _purchaseService.restorePurchases(userId); | ||
return DonationsState.restoreCompleted(error: !restored); | ||
} | ||
|
||
Future<DonationsState> _purchase(_Purchase e) async { | ||
final succeed = await _purchaseService.purchase(e.userId, e.identifier, e.offeringIdentifier); | ||
return DonationsState.purchaseCompleted(error: !succeed); | ||
} | ||
} |
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,14 @@ | ||
part of 'donations_bloc.dart'; | ||
|
||
@freezed | ||
class DonationsEvent with _$DonationsEvent { | ||
const factory DonationsEvent.init() = _Init; | ||
|
||
const factory DonationsEvent.restorePurchases({required String userId}) = _RestorePurchases; | ||
|
||
const factory DonationsEvent.purchase({ | ||
required String userId, | ||
required String identifier, | ||
required String offeringIdentifier, | ||
}) = _Purchase; | ||
} |
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,20 @@ | ||
part of 'donations_bloc.dart'; | ||
|
||
@freezed | ||
class DonationsState with _$DonationsState { | ||
const factory DonationsState.loading() = _Loading; | ||
|
||
const factory DonationsState.initial({ | ||
required List<PackageItemModel> packages, | ||
required bool isInitialized, | ||
required bool noInternetConnection, | ||
}) = _InitialState; | ||
|
||
const factory DonationsState.purchaseCompleted({ | ||
required bool error, | ||
}) = _PurchaseCompleted; | ||
|
||
const factory DonationsState.restoreCompleted({ | ||
required bool error, | ||
}) = _RestoreCompleted; | ||
} |
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