rewarded ads

This commit is contained in:
Dmitry 2024-07-27 16:13:42 +03:00
parent ffe18b2ffb
commit 5a37539554
8 changed files with 141 additions and 4 deletions

View file

@ -0,0 +1,3 @@
class BuySubscriptionModel {
}

View 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;
}
}

View file

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:developer'; import 'dart:developer';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
@ -8,7 +9,7 @@ class YandexAds {
BoxConstraints constraints, { BoxConstraints constraints, {
required String id, required String id,
String? key, String? key,
VoidCallback? onLoaded, VoidCallback? onLoaded,
}) { }) {
return BannerAd( return BannerAd(
adUnitId: id, adUnitId: id,
@ -39,4 +40,56 @@ class YandexAds {
// Called when an impression is recorded for an ad. // 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;
}
} }

View file

@ -89,7 +89,7 @@ void main() async {
await locator.previewPackPoller.init(); await locator.previewPackPoller.init();
await locator.packUpdater.init(); await locator.packUpdater.init();
print('initing complete ${stopwatch.elapsedMilliseconds}'); print('initing complete ${stopwatch.elapsedMilliseconds}');
if (!kIsWeb) await MobileAds.initialize(); if (!kIsWeb) unawaited(MobileAds.initialize());
print('mobile ads ${stopwatch.elapsedMilliseconds}'); print('mobile ads ${stopwatch.elapsedMilliseconds}');
print('runApp ${stopwatch.elapsedMilliseconds}'); print('runApp ${stopwatch.elapsedMilliseconds}');
runApp(const MyApp()); runApp(const MyApp());

View file

@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.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.dart';
import 'package:mnemo_cards/domain/router/app_router.gr.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/flags.dart';
import 'package:mnemo_cards/main.dart'; import 'package:mnemo_cards/main.dart';
import 'package:mnemo_cards_frontend_common/mnemo_cards_frontend_common.dart'; import 'package:mnemo_cards_frontend_common/mnemo_cards_frontend_common.dart';
@ -145,6 +146,17 @@ class ProfilePage extends StatelessWidget {
height: 5, height: 5,
color: borderGray.withOpacity(0.2), 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( SimpleTile.text(
text: 'Написать в телеграм', text: 'Написать в телеграм',
onTap: () => launchUrl( onTap: () => launchUrl(

View 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();
}

View file

@ -5,8 +5,6 @@ import '../di/locator.dart';
class MobileEnterPromoCodeWidget extends StatelessWidget { class MobileEnterPromoCodeWidget extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return EnterPromoCodeWidget(onApplyPromoCode: onApplyPromoCode); return EnterPromoCodeWidget(onApplyPromoCode: onApplyPromoCode);