From 4a65a37017b3c4f76aa3033a8faee385be286282 Mon Sep 17 00:00:00 2001 From: Efrain Date: Sat, 5 Mar 2022 20:34:15 -0500 Subject: [PATCH] Github action fixes. Track some useful data --- .github/workflows/tests.yml | 2 +- lib/application/donations/donations_bloc.dart | 6 +++++- lib/domain/services/telemetry_service.dart | 4 ++++ .../telemetry/telemetry_service.dart | 19 +++++++++---------- lib/injection.dart | 3 ++- .../donations/donations_bloc_test.dart | 18 +++++++++++------- 6 files changed, 32 insertions(+), 20 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index af6981bc7..4e55cefc3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,7 @@ jobs: - name: Create secrets file run: | - echo "class Secrets { static String appCenterKey = '1234'; }" > lib/infrastructure/telemetry/secrets.dart + echo "class Secrets { static String appCenterKey = '1234'; static String androidPurchasesKey = '1234' }" > lib/infrastructure/secrets.dart - name: Install and set flutter version uses: subosito/flutter-action@v2.3.0 diff --git a/lib/application/donations/donations_bloc.dart b/lib/application/donations/donations_bloc.dart index d0d9f3c87..37585eaa1 100644 --- a/lib/application/donations/donations_bloc.dart +++ b/lib/application/donations/donations_bloc.dart @@ -4,6 +4,7 @@ import 'package:shiori/domain/extensions/string_extensions.dart'; import 'package:shiori/domain/models/models.dart'; import 'package:shiori/domain/services/network_service.dart'; import 'package:shiori/domain/services/purchase_service.dart'; +import 'package:shiori/domain/services/telemetry_service.dart'; part 'donations_bloc.freezed.dart'; part 'donations_event.dart'; @@ -12,13 +13,14 @@ part 'donations_state.dart'; class DonationsBloc extends Bloc { final PurchaseService _purchaseService; final NetworkService _networkService; + final TelemetryService _telemetryService; static int maxUserIdLength = 20; //The user id must be something like 12345_xyz static String appUserIdRegex = '([a-zA-Z0-9]{5,20})'; - DonationsBloc(this._purchaseService, this._networkService) : super(const DonationsState.loading()); + DonationsBloc(this._purchaseService, this._networkService, this._telemetryService) : super(const DonationsState.loading()); @override Stream mapEventToState(DonationsEvent event) async* { @@ -88,6 +90,7 @@ class DonationsBloc extends Bloc { throw Exception('AppUserId is not valid'); } final restored = await _purchaseService.restorePurchases(userId); + await _telemetryService.trackRestore(userId, restored); return DonationsState.restoreCompleted(error: !restored); } @@ -105,6 +108,7 @@ class DonationsBloc extends Bloc { } final succeed = await _purchaseService.purchase(e.userId, e.identifier, e.offeringIdentifier); + await _telemetryService.trackPurchase(e.userId, e.identifier, succeed); return DonationsState.purchaseCompleted(error: !succeed); } } diff --git a/lib/domain/services/telemetry_service.dart b/lib/domain/services/telemetry_service.dart index 12e9db175..11371caaf 100644 --- a/lib/domain/services/telemetry_service.dart +++ b/lib/domain/services/telemetry_service.dart @@ -57,4 +57,8 @@ abstract class TelemetryService { Future trackCustomBuildSaved(String charKey, CharacterRoleType roleType, CharacterRoleSubType subType); Future trackCustomBuildScreenShootTaken(String charKey, CharacterRoleType roleType, CharacterRoleSubType subType); + + Future trackRestore(String userId, bool succeed); + + Future trackPurchase(String userId, String identifier, bool succeed); } diff --git a/lib/infrastructure/telemetry/telemetry_service.dart b/lib/infrastructure/telemetry/telemetry_service.dart index 3cf3d1c2a..d20f5f621 100644 --- a/lib/infrastructure/telemetry/telemetry_service.dart +++ b/lib/infrastructure/telemetry/telemetry_service.dart @@ -144,20 +144,19 @@ class TelemetryServiceImpl implements TelemetryService { @override Future trackCustomBuildSaved(String charKey, CharacterRoleType roleType, CharacterRoleSubType subType) => trackEventAsync( 'Custom-Build-Saved', - { - 'CharKey': charKey, - 'RoleType': EnumToString.convertToString(roleType), - 'SubType': EnumToString.convertToString(subType), - }, + {'Char_RoleType_SubType': '${charKey}_${EnumToString.convertToString(roleType)}_${EnumToString.convertToString(subType)}'}, ); @override Future trackCustomBuildScreenShootTaken(String charKey, CharacterRoleType roleType, CharacterRoleSubType subType) => trackEventAsync( 'Custom-Build-ScreenShootTaken', - { - 'CharKey': charKey, - 'RoleType': EnumToString.convertToString(roleType), - 'SubType': EnumToString.convertToString(subType), - }, + {'Char_RoleType_SubType': '${charKey}_${EnumToString.convertToString(roleType)}_${EnumToString.convertToString(subType)}'}, ); + + @override + Future trackPurchase(String userId, String identifier, bool succeed) => + trackEventAsync('Donations-Purchase', {'UserId_Identifier_Succeed': '${userId}_${identifier}_$succeed'}); + + @override + Future trackRestore(String userId, bool succeed) => trackEventAsync('Donations-Restore', {'UserId_Succeed': '${userId}_$succeed'}); } diff --git a/lib/injection.dart b/lib/injection.dart index 185947fb8..19589bdac 100644 --- a/lib/injection.dart +++ b/lib/injection.dart @@ -140,7 +140,8 @@ class Injection { static DonationsBloc get donationsBloc { final purchaseService = getIt(); final networkService = getIt(); - return DonationsBloc(purchaseService, networkService); + final telemetryService = getIt(); + return DonationsBloc(purchaseService, networkService, telemetryService); } //TODO: USE THIS PROP diff --git a/test/application/donations/donations_bloc_test.dart b/test/application/donations/donations_bloc_test.dart index 008a55cc0..e4a61145e 100644 --- a/test/application/donations/donations_bloc_test.dart +++ b/test/application/donations/donations_bloc_test.dart @@ -18,7 +18,10 @@ void main() { TestWidgetsFlutterBinding.ensureInitialized(); }); - test('Initial state', () => expect(DonationsBloc(MockPurchaseService(), MockNetworkService()).state, const DonationsState.loading())); + test( + 'Initial state', + () => expect(DonationsBloc(MockPurchaseService(), MockNetworkService(), MockTelemetryService()).state, const DonationsState.loading()), + ); group('init', () { blocTest( @@ -27,7 +30,7 @@ void main() { final networkService = MockNetworkService(); final purchaseService = MockPurchaseService(); when(networkService.isInternetAvailable()).thenAnswer((_) => Future.value(false)); - return DonationsBloc(purchaseService, networkService); + return DonationsBloc(purchaseService, networkService, MockTelemetryService()); }, act: (bloc) => bloc.add(const DonationsEvent.init()), expect: () => const [ @@ -48,7 +51,7 @@ void main() { final purchaseService = MockPurchaseService(); when(networkService.isInternetAvailable()).thenAnswer((_) => Future.value(true)); when(purchaseService.isPlatformSupported()).thenAnswer((_) => Future.value(false)); - return DonationsBloc(purchaseService, networkService); + return DonationsBloc(purchaseService, networkService, MockTelemetryService()); }, act: (bloc) => bloc.add(const DonationsEvent.init()), expect: () => const [ @@ -72,7 +75,7 @@ void main() { when(purchaseService.isInitialized).thenReturn(true); when(purchaseService.init()).thenAnswer((_) => Future.value(true)); when(purchaseService.canMakePurchases()).thenAnswer((_) => Future.value(false)); - return DonationsBloc(purchaseService, networkService); + return DonationsBloc(purchaseService, networkService, MockTelemetryService()); }, act: (bloc) => bloc.add(const DonationsEvent.init()), expect: () => const [ @@ -97,7 +100,7 @@ void main() { when(purchaseService.init()).thenAnswer((_) => Future.value(true)); when(purchaseService.canMakePurchases()).thenAnswer((_) => Future.value(true)); when(purchaseService.getInAppPurchases()).thenAnswer((_) => Future.value(_packages)); - return DonationsBloc(purchaseService, networkService); + return DonationsBloc(purchaseService, networkService, MockTelemetryService()); }, act: (bloc) => bloc.add(const DonationsEvent.init()), expect: () => const [ @@ -123,7 +126,7 @@ void main() { when(purchaseService.canMakePurchases()).thenAnswer((_) => Future.value(true)); when(purchaseService.getInAppPurchases()).thenAnswer((_) => Future.value(_packages)); when(purchaseService.restorePurchases(_validUserId)).thenAnswer((_) => Future.value(restoreSucceeds)); - return DonationsBloc(purchaseService, networkService); + return DonationsBloc(purchaseService, networkService, MockTelemetryService()); } blocTest( @@ -162,6 +165,7 @@ void main() { DonationsBloc _getBloc({bool purchaseSucceeds = true}) { final networkService = MockNetworkService(); final purchaseService = MockPurchaseService(); + final telemetryService = MockTelemetryService(); when(networkService.isInternetAvailable()).thenAnswer((_) => Future.value(true)); when(purchaseService.isPlatformSupported()).thenAnswer((_) => Future.value(true)); when(purchaseService.isInitialized).thenReturn(true); @@ -170,7 +174,7 @@ void main() { when(purchaseService.getInAppPurchases()).thenAnswer((_) => Future.value(_packages)); when(purchaseService.purchase(_validUserId, _packages.first.identifier, _packages.first.offeringIdentifier)) .thenAnswer((_) => Future.value(purchaseSucceeds)); - return DonationsBloc(purchaseService, networkService); + return DonationsBloc(purchaseService, networkService, telemetryService); } blocTest(