Commit 29774a08 authored by CZ1004's avatar CZ1004

【新增】垃圾桶

parent b5f7ddad
//
// StaticColorString.swift
// PhoneManager
//
// Created by edy on 2025/5/27.
//
import Foundation
struct HexString {
static let color1 = "#2856F0"
static let color2 = "#E9EEFF"
static let color3 = "#333333"
}
struct TextString {
// MARK: 垃圾桶
static let countUnitString = "items"
static let emptyTrshString = "Empty Trash"
static let subTitleString = "in the trash can"
}
//
// TrashCardView.swift
// PhoneManager
//
// Created by edy on 2025/5/27.
//
import UIKit
class TrashCardView: UIView {
var mediaType : TrashTypeEnum?
var trashData : [AssetModel] = []
lazy var countlabel : UILabel = {
let label = UILabel()
label.font = UIFont.semiboldFont(with: 16)
label.textColor = UIColor.colorWithHex(hexStr: HexString.color1)
return label
}()
lazy var itemsLabel: UILabel = {
let label = UILabel()
label.text = TextString.countUnitString
label.font = UIFont.semiboldFont(with: 16)
label.textColor = UIColor.colorWithHex(hexStr: HexString.color3)
label.textAlignment = .left
return label
}()
lazy var subLabel: UILabel = {
let label = UILabel()
label.text = TextString.subTitleString
label.font = UIFont.regularFont(with: 16)
label.textColor = UIColor(red: 0.2, green: 0.2, blue: 0.2,alpha:1)
label.textAlignment = .left
return label
}()
lazy var emptyButton: UIButton = {
let button = UIButton(type: .custom)
button.setTitle(TextString.emptyTrshString, for: .normal)
button.setImage(UIImage(named: ""), for: .normal)
button.setTitleColor(UIColor(red: 0.16, green: 0.34, blue: 0.94, alpha: 1), for: .normal)
button.backgroundColor = UIColor.colorWithHex(hexStr: HexString.color2)
button.titleLabel?.font = UIFont.semiboldFont(with: 12)
button.layer.borderColor = UIColor.colorWithHex(hexStr: HexString.color1).cgColor
button.layer.borderWidth = 1.0
return button
}()
init(frame: CGRect, mediaType:TrashTypeEnum) {
super.init(frame: frame)
// 初始化垃圾桶的类型
self.mediaType = mediaType
// 设置页面数据
self.getCurrentPageData()
// 设置默认
self.setPageDefaultProperty()
// 添加视图
self.addViews()
// 设置位置
self.setLocation()
// 动态设置数量标签
self.getDataThenSetText()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension TrashCardView {
// 设置页面默认属性
private func setPageDefaultProperty() {
self.backgroundColor = .white
self.clipsToBounds = true
self.layer.cornerRadius = 12
}
// 设置UI
private func addViews() {
self.addSubview(self.countlabel)
self.addSubview(self.itemsLabel)
self.addSubview(self.subLabel)
self.addSubview(self.emptyButton)
}
// 设置位置
private func setLocation() {
self.countlabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(18)
make.top.equalToSuperview().offset(17)
make.height.equalTo(22)
}
self.itemsLabel.snp.makeConstraints { make in
make.left.equalTo(self.countlabel.snp.right).offset(11)
make.top.equalToSuperview().offset(17)
make.height.equalTo(22)
}
self.subLabel.snp.makeConstraints { make in
make.bottom.equalToSuperview().offset(-17)
make.left.equalToSuperview().offset(18)
make.height.equalTo(22)
}
self.emptyButton.snp.makeConstraints { make in
make.right.equalToSuperview().offset(-15)
make.height.equalTo(38)
make.centerY.equalToSuperview()
make.width.equalTo(129.88)
}
}
// 添加手势
func addGestureToSelf(){
self.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer()
tap.addTarget(self, action: #selector(tapHandle))
self.addGestureRecognizer(tap)
}
// 获取当前垃圾桶数据-直接从数据库获取
private func getCurrentPageDataCount() -> Int {
// 只需要获取数量就可以
return self.trashData.count
}
// 获取当前页面数据
private func getCurrentPageData() {
let allData : [(localIdentifier: String, assetSize: Double, createDate: Date, mediaType: Int)] = TrashDatabase.shared.queryAll()
var tempArray : [AssetModel] = []
for item in allData {
tempArray.append(AssetModel(localIdentifier: item.localIdentifier, assetSize: item.assetSize, createDate: item.createDate, mediaType: self.mediaType == TrashTypeEnum.video ? 2 : 1))
}
self.trashData = tempArray
}
// 设置当前数量
@MainActor private func setCountLabelText(with text: String) {
self.countlabel.text = text
}
// MARK: 事件
@objc func tapHandle(){
// 跳转垃圾桶页面
let trashVc : TrashViewController = TrashViewController()
self.responderViewController()?.present(trashVc, animated: true)
}
// MARK: 主要方法
// 主要方法-获取数据后设置数量文字
private func getDataThenSetText() {
let count = self.getCurrentPageDataCount()
self.setCountLabelText(with: String(count))
}
// 清空垃圾桶数据 - 涉及到广告逻辑
private func clearTrashData() {
// 是否提示垃圾桶数量为空
if self.trashData.count <= 0 { return }
// 开始广告逻辑-先看是否订阅(已经订阅的直接开始删除逻辑)
if IAPManager.share.isSubscribed == true {
self.deleteTrashData(trashData: self.trashData, needUpdateFreeTimes: true)
return
}
// 没有订阅
}
/// 删除垃圾桶数据
/// - Parameters:
/// - trashData: 指定的垃圾桶数据
/// - needUpdateFreeTimes: 是否需要更新免费次数
private func deleteTrashData(trashData: [AssetModel] , needUpdateFreeTimes: Bool){
TrashDataManager.clearTrashData(mediaType: self.mediaType,data: trashData) {
// 1、更新免费次数
if needUpdateFreeTimes {
self.updateFreeTimes()
}
// 2、跳转删除成功页面
self.showDeleteSuccess(array: self.trashData)
}
}
/// 更新免费次数
func updateFreeTimes(){
// 删除成功后更新次数
var times = UserDefaults.standard.object(forKey: "saveAdvTimes") as! Int
if times > 0 {
// 如果免费次数大于0
times = times - 1
UserDefaults.standard.set(times, forKey: "saveAdvTimes")
}
}
// 显示删除成功页面
func showDeleteSuccess(array: [AssetModel]){
var tempSize = array.reduce(0.0) {$0 + $1.assetSize}
let vc = DelSuccessViewController()
vc.delType = self.setSuccessDelTypeText(with: array.count)
vc.fileSzie = Int64(tempSize)
vc.fileCount = array.count
vc.modalPresentationStyle = .fullScreen
DispatchQueue.main.async {
self.responderViewController()?.present(vc, animated: true)
}
}
// 辅助方法-获取delType字段文字
private func setSuccessDelTypeText(with count: Int) -> String {
if self.mediaType == .video {
return count > 1 ? "videos" : "video"
}
return count > 1 ? "photos" : "photo"
}
}
......@@ -17,4 +17,20 @@ extension UIFont {
return .systemFont(ofSize: size*scale, weight: weight)
}
static func semiboldFont(with size: CGFloat) -> UIFont {
UIFont.systemFont(ofSize: size, weight: .semibold)
}
static func boldFont(with size: CGFloat) -> UIFont {
UIFont.systemFont(ofSize: size, weight: .bold)
}
static func regularFont(with size: CGFloat) -> UIFont {
UIFont.systemFont(ofSize: size, weight: .regular)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment