Skip to content

Commit

Permalink
Github action fixes.
Browse files Browse the repository at this point in the history
Track some useful data
  • Loading branch information
Wolfteam committed Mar 6, 2022
1 parent 907fcdc commit 4a65a37
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/application/donations/donations_bloc.dart
Expand Up @@ -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';
Expand All @@ -12,13 +13,14 @@ part 'donations_state.dart';
class DonationsBloc extends Bloc<DonationsEvent, DonationsState> {
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<DonationsState> mapEventToState(DonationsEvent event) async* {
Expand Down Expand Up @@ -88,6 +90,7 @@ class DonationsBloc extends Bloc<DonationsEvent, DonationsState> {
throw Exception('AppUserId is not valid');
}
final restored = await _purchaseService.restorePurchases(userId);
await _telemetryService.trackRestore(userId, restored);
return DonationsState.restoreCompleted(error: !restored);
}

Expand All @@ -105,6 +108,7 @@ class DonationsBloc extends Bloc<DonationsEvent, DonationsState> {
}

final succeed = await _purchaseService.purchase(e.userId, e.identifier, e.offeringIdentifier);
await _telemetryService.trackPurchase(e.userId, e.identifier, succeed);
return DonationsState.purchaseCompleted(error: !succeed);
}
}
4 changes: 4 additions & 0 deletions lib/domain/services/telemetry_service.dart
Expand Up @@ -57,4 +57,8 @@ abstract class TelemetryService {
Future<void> trackCustomBuildSaved(String charKey, CharacterRoleType roleType, CharacterRoleSubType subType);

Future<void> trackCustomBuildScreenShootTaken(String charKey, CharacterRoleType roleType, CharacterRoleSubType subType);

Future<void> trackRestore(String userId, bool succeed);

Future<void> trackPurchase(String userId, String identifier, bool succeed);
}
19 changes: 9 additions & 10 deletions lib/infrastructure/telemetry/telemetry_service.dart
Expand Up @@ -144,20 +144,19 @@ class TelemetryServiceImpl implements TelemetryService {
@override
Future<void> 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<void> 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<void> trackPurchase(String userId, String identifier, bool succeed) =>
trackEventAsync('Donations-Purchase', {'UserId_Identifier_Succeed': '${userId}_${identifier}_$succeed'});

@override
Future<void> trackRestore(String userId, bool succeed) => trackEventAsync('Donations-Restore', {'UserId_Succeed': '${userId}_$succeed'});
}
3 changes: 2 additions & 1 deletion lib/injection.dart
Expand Up @@ -140,7 +140,8 @@ class Injection {
static DonationsBloc get donationsBloc {
final purchaseService = getIt<PurchaseService>();
final networkService = getIt<NetworkService>();
return DonationsBloc(purchaseService, networkService);
final telemetryService = getIt<TelemetryService>();
return DonationsBloc(purchaseService, networkService, telemetryService);
}

//TODO: USE THIS PROP
Expand Down
18 changes: 11 additions & 7 deletions test/application/donations/donations_bloc_test.dart
Expand Up @@ -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<DonationsBloc, DonationsState>(
Expand All @@ -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 [
Expand All @@ -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 [
Expand All @@ -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 [
Expand All @@ -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 [
Expand All @@ -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<DonationsBloc, DonationsState>(
Expand Down Expand Up @@ -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);
Expand All @@ -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<DonationsBloc, DonationsState>(
Expand Down

0 comments on commit 4a65a37

Please sign in to comment.