rating_helper.dart 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'dart:io';
  2. import 'package:logging/logging.dart';
  3. import 'package:url_launcher/url_launcher.dart';
  4. import '../persistence/persistence.dart';
  5. import '../remote_config/remote_config.dart';
  6. final Logger _log = Logger('RatingHelper');
  7. class RatingHelper {
  8. static const int _maxShowTimes = 3; //最多弹框次数
  9. static const int _showTimeHours = 24; // 24小时
  10. static const String _googlePlayUrl = "https://play.google.com/store/apps/details?id=jigsort.solitaire.jigsaw.match.games";
  11. static const String _appStoreUrl = "https://apps.apple.com/us/app/jigsort.solitaire.jigsaw.match.games/id6499138123";
  12. static String get appStoreUrl {
  13. if (Platform.isAndroid) {
  14. return _googlePlayUrl;
  15. }
  16. if (Platform.isIOS) {
  17. return _appStoreUrl;
  18. }
  19. return _googlePlayUrl;
  20. }
  21. static Future<bool> shouldShowRateDialog(int doneLevelCount) async {
  22. // 1. 是否已经评过分
  23. double rating = Persistence().rating;
  24. if (rating > 0) {
  25. _log.info("shouldShowRateDialog false. already rated, rating: $rating");
  26. return false;
  27. }
  28. // 2. 检查已经弹过几次框
  29. int rateShowTimes = Persistence().rateShowTimes;
  30. if (rateShowTimes >= _maxShowTimes) {
  31. _log.info("shouldShowRateDialog false. rateShowTimes limit");
  32. return false;
  33. }
  34. // ios 只弹框一次
  35. if (Platform.isIOS && rateShowTimes >= 1) {
  36. _log.info("shouldShowRateDialog false. iOS rateShowTimes limit");
  37. return false;
  38. }
  39. // 3. 检查上次弹框时间,需要超过24小时才能再次弹框
  40. DateTime now = DateTime.now();
  41. DateTime lastRateShowTime = Persistence().rateLastShowTime;
  42. if (now.difference(lastRateShowTime).inHours < _showTimeHours) {
  43. _log.info("shouldShowRateDialog false. show time in 24 hours");
  44. return false;
  45. }
  46. //4. 用户完成了n个关卡才弹框
  47. int rateLevelsCount = RemoteConfig().rateUsLevelCount;
  48. int completedCount = doneLevelCount;
  49. if (completedCount < rateLevelsCount) {
  50. _log.info("shouldShowRateDialog false. must finish at lease $rateLevelsCount works");
  51. return false;
  52. }
  53. _log.info("shouldShowRateDialog true!");
  54. return true;
  55. }
  56. // 用户点了取消按钮未评分
  57. static onRateCancel() async {
  58. // 写入弹框次数+1
  59. Persistence().rateShowTimes++;
  60. // 写入弹框时间
  61. Persistence().rateLastShowTime = DateTime.now();
  62. }
  63. // 用户选择评分并正常提交
  64. static onRateSubmmit(double rating) async {
  65. // 写入弹框次数+1
  66. Persistence().rateShowTimes++;
  67. // 写入弹框时间
  68. Persistence().rateLastShowTime = DateTime.now();
  69. // 写入评分
  70. Persistence().rating = rating;
  71. // 5星评分,跳转到应用商店评价页面
  72. if (rating >= 5.0 && Platform.isAndroid) {
  73. _linkLaunch(appStoreUrl);
  74. }
  75. }
  76. // 页面跳转
  77. static _linkLaunch(String link) async {
  78. Uri url = Uri.parse(link);
  79. if (!await launchUrl(url, mode: LaunchMode.externalApplication)) {
  80. throw Exception('Could not launch $url');
  81. }
  82. }
  83. }