rewarded ads
This commit is contained in:
parent
ffe18b2ffb
commit
5a37539554
8 changed files with 141 additions and 4 deletions
Binary file not shown.
3
lib/features/subscription/buy_subscription_model.dart
Normal file
3
lib/features/subscription/buy_subscription_model.dart
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
class BuySubscriptionModel {
|
||||
|
||||
}
|
||||
40
lib/features/subscription/subscription_api.dart
Normal file
40
lib/features/subscription/subscription_api.dart
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:mnemo_cards_common/mnemo_cards_common.dart';
|
||||
|
||||
import '../../managers/repository/api.dart';
|
||||
|
||||
class SubscriptionApi with Api {
|
||||
final Dio _dio;
|
||||
|
||||
SubscriptionApi(this._dio);
|
||||
|
||||
// buy page dto if not available
|
||||
Future<CardPackBuyDto?> getPackBuy(String id) async {
|
||||
final r = await _dio.get<String>(
|
||||
'$path/pack/buy/$id',
|
||||
options: Options(
|
||||
sendTimeout: Duration(seconds: 5),
|
||||
receiveTimeout: Duration(seconds: 5),
|
||||
),
|
||||
);
|
||||
return (r.data as String).decode(CardPackBuyDto.fromJson);
|
||||
}
|
||||
|
||||
/// cards zip archive for available pack
|
||||
/// unavailable packs transfer images by base64
|
||||
Future<Uint8List> packImagesArchive(String packId) async {
|
||||
final response = await _dio.get<Uint8List>(
|
||||
'$path/pack/cards/$packId',
|
||||
options: Options(
|
||||
responseType: ResponseType.bytes,
|
||||
),
|
||||
);
|
||||
final data = response.data;
|
||||
if (data == null) {
|
||||
return Uint8List.fromList([]);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
|
@ -8,7 +9,7 @@ class YandexAds {
|
|||
BoxConstraints constraints, {
|
||||
required String id,
|
||||
String? key,
|
||||
VoidCallback? onLoaded,
|
||||
VoidCallback? onLoaded,
|
||||
}) {
|
||||
return BannerAd(
|
||||
adUnitId: id,
|
||||
|
|
@ -39,4 +40,56 @@ class YandexAds {
|
|||
// Called when an impression is recorded for an ad.
|
||||
});
|
||||
}
|
||||
|
||||
static RewardedAd? _preloadedAd;
|
||||
|
||||
static Future<RewardedAd?> loadRewardedAd() async {
|
||||
final completer = Completer<RewardedAd?>();
|
||||
final _rewardedAdLoader = await RewardedAdLoader.create(
|
||||
onAdLoaded: (RewardedAd rewardedAd) {
|
||||
_preloadedAd = rewardedAd;
|
||||
completer.complete(rewardedAd);
|
||||
},
|
||||
onAdFailedToLoad: (error) {
|
||||
completer.complete(null);
|
||||
},
|
||||
);
|
||||
await _rewardedAdLoader.loadAd(
|
||||
adRequestConfiguration: AdRequestConfiguration(
|
||||
adUnitId: 'R-M-3761037-1',
|
||||
),
|
||||
);
|
||||
return completer.future;
|
||||
}
|
||||
|
||||
static Future<bool> showRewardedAd(
|
||||
Function(Reward reward) onRewarded, {
|
||||
maxAttempts = 2,
|
||||
}) async {
|
||||
if (_preloadedAd == null) {
|
||||
final _loadedAd = await loadRewardedAd();
|
||||
if (_loadedAd == null) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
final _ad = _preloadedAd;
|
||||
_preloadedAd = null;
|
||||
_ad?.setAdEventListener(
|
||||
eventListener: RewardedAdEventListener(onAdDismissed: () {
|
||||
_ad.destroy();
|
||||
}, onAdFailedToShow: (e) {
|
||||
_ad.destroy();
|
||||
if (maxAttempts-- > 0) {
|
||||
showRewardedAd(onRewarded, maxAttempts: maxAttempts);
|
||||
}
|
||||
}, onAdShown: () {
|
||||
// _ad.destroy();
|
||||
}, onRewarded: (r) {
|
||||
onRewarded(r);
|
||||
_ad.destroy();
|
||||
}));
|
||||
await _ad?.show();
|
||||
final reward = await _ad?.waitForDismiss();
|
||||
return reward != null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,7 +89,7 @@ void main() async {
|
|||
await locator.previewPackPoller.init();
|
||||
await locator.packUpdater.init();
|
||||
print('initing complete ${stopwatch.elapsedMilliseconds}');
|
||||
if (!kIsWeb) await MobileAds.initialize();
|
||||
if (!kIsWeb) unawaited(MobileAds.initialize());
|
||||
print('mobile ads ${stopwatch.elapsedMilliseconds}');
|
||||
print('runApp ${stopwatch.elapsedMilliseconds}');
|
||||
runApp(const MyApp());
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
|||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||||
import 'package:mnemo_cards/domain/router/app_router.dart';
|
||||
import 'package:mnemo_cards/domain/router/app_router.gr.dart';
|
||||
import 'package:mnemo_cards/features/yandex_ads/yandex_ads.dart';
|
||||
import 'package:mnemo_cards/flags.dart';
|
||||
import 'package:mnemo_cards/main.dart';
|
||||
import 'package:mnemo_cards_frontend_common/mnemo_cards_frontend_common.dart';
|
||||
|
|
@ -145,6 +146,17 @@ class ProfilePage extends StatelessWidget {
|
|||
height: 5,
|
||||
color: borderGray.withOpacity(0.2),
|
||||
),
|
||||
if (SHOW_ADMIN)
|
||||
SimpleTile(title: Text('Посмотреть рекламу'), onTap: () {
|
||||
YandexAds.showRewardedAd((r) {
|
||||
showErrorDialog('Вы получили\n${r.amount} ${r.type}');
|
||||
});
|
||||
},),
|
||||
if (SHOW_ADMIN)
|
||||
Divider(
|
||||
height: 5,
|
||||
color: borderGray.withOpacity(0.2),
|
||||
),
|
||||
SimpleTile.text(
|
||||
text: 'Написать в телеграм',
|
||||
onTap: () => launchUrl(
|
||||
|
|
|
|||
31
lib/widgets/buy_subscription_widget.dart
Normal file
31
lib/widgets/buy_subscription_widget.dart
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:mnemo_cards/main.dart';
|
||||
import 'package:mnemo_cards_frontend_common/mnemo_cards_frontend_common.dart';
|
||||
|
||||
import '../di/injector.dart';
|
||||
import '../di/locator.dart';
|
||||
import '../domain/router/app_router.dart';
|
||||
import '../features/packs/packs_api.dart';
|
||||
|
||||
class MobileBuySubscriptionWidget extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BuySubscriptionWidget(onBuySubscription: onBuySubscription);
|
||||
}
|
||||
|
||||
Future<void> onBuySubscription() async {
|
||||
final user = locator.userManager.userStateHolder.user;
|
||||
if (user == null) {
|
||||
AppRouter.openAuthOrProfile();
|
||||
return;
|
||||
}
|
||||
if (user.subscription) {
|
||||
return;
|
||||
}
|
||||
final packsApi = getIt.get<PacksApi>();
|
||||
// final buyDto = await packsApi.getPackBuy(id).catchError((_) => null);
|
||||
}
|
||||
|
||||
const MobileBuySubscriptionWidget();
|
||||
}
|
||||
|
|
@ -5,8 +5,6 @@ import '../di/locator.dart';
|
|||
|
||||
class MobileEnterPromoCodeWidget extends StatelessWidget {
|
||||
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return EnterPromoCodeWidget(onApplyPromoCode: onApplyPromoCode);
|
||||
|
|
|
|||
Loading…
Reference in a new issue