popup_manager.dart 3.19 KB
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

import '../../models/popup_entity.dart';
import '../../pages/home/PopupType.dart';
import '../../route/route.dart';
import '../../utils/log_util.dart';
import '../../utils/sp_util.dart';
import '../widgets/ow_image_widget.dart';

class PopupManager {
  // 获取今日弹窗次数的键
  static String _getPopupCountKey(String popupId) {
    String today = DateFormat('yyyy-MM-dd').format(DateTime.now());
    return 'popup_count_$popupId\_$today';
  }

  // 获取今日弹窗次数
  static int _getPopupCount(String popupId) {
    String key = _getPopupCountKey(popupId);
    return SpUtil.getInstance().get(key) ?? 0;
  }

  // 更新今日弹窗次数
  static void _incrementPopupCount(String popupId) {
    String key = _getPopupCountKey(popupId);
    int currentCount = _getPopupCount(popupId);
    SpUtil.getInstance().setData(key, currentCount + 1);
  }

  // 检查是否可以弹窗
  static bool canShowPopup(PopupEntity popupEntity) {
    int todayPopupCount = _getPopupCount(popupEntity.id);
    return todayPopupCount < popupEntity.dayNum;
  }

  // 显示弹窗逻辑
  static Future<void> showPopupIfAllowed(
      BuildContext context, PopupEntity popupEntity) async {
    if (popupEntity.imageUrl.isEmpty) {
      return;
    }
    if (canShowPopup(popupEntity)) {
      /// 弹窗内容:图片和关闭按钮
      await showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return Dialog(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8.0)),
            child: Stack(
              alignment: Alignment.topRight,
              children: [
                // 图片内容区域
                ClipRRect(
                  borderRadius: BorderRadius.circular(16), // 可选:圆角
                  child: GestureDetector(
                    onTap: () {
                      if (popupEntity.actionType == PopupType.h5.name &&
                          popupEntity.actionValue.isNotEmpty) {
                        Navigator.of(context).pop();
                        Navigator.of(context).pushNamed(AppRouteName.webView,
                            arguments: {'urlStr': popupEntity.actionValue});
                      }
                    },
                    child: OwImageWidget(
                        height: MediaQuery.of(context).size.height * 0.8,
                        name: popupEntity.imageUrl,
                        fit: BoxFit.fitHeight),
                  ),
                ),
                Positioned(
                  top: 0,
                  right: 0,
                  child: IconButton(
                    icon: const Icon(Icons.close,
                        color: Colors.white,
                        opticalSize: 2,
                        shadows: [Shadow(color: Colors.black, blurRadius: 24)]),
                    onPressed: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ),
              ],
            ),
          );
        },
      );

      // 更新今日弹窗次数
      _incrementPopupCount(popupEntity.id);
    }
  }
}