ProHUD/Example-Old/Example/TestAlertVC.swift

273 lines
12 KiB
Swift
Raw Normal View History

2019-08-12 17:59:40 +08:00
//
// TestAlertVC.swift
// Example
//
// Created by xaoxuu on 2019/8/12.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
import ProHUD
2020-06-23 20:16:12 +08:00
// 2
func loadingSuccessAfter2Seconds() {
DispatchQueue.main.asyncAfter(deadline: .now()+2) {
Alert.find("loading", last: { (a) in
a.update { (vm) in
vm.scene = .success
vm.title = "同步成功"
vm.message = nil
}
})
}
}
2019-08-12 17:59:40 +08:00
class TestAlertVC: BaseListVC {
override func viewDidLoad() {
super.viewDidLoad()
2020-06-23 20:16:12 +08:00
vm.addSection(title: "最简单的写法") { (sec) in
sec.addRow(title: "Alert.push(scene: .loading)") {
Alert.push(scene: .loading)
2019-08-12 17:59:40 +08:00
}
2020-06-23 20:16:12 +08:00
sec.addRow(title: "Alert.push(scene: .success)") {
Alert.push(scene: .success)
2019-08-12 20:20:07 +08:00
}
2020-06-23 20:16:12 +08:00
sec.addRow(title: "Alert.push(scene: .warning)") {
Alert.push(scene: .warning)
2019-08-12 17:59:40 +08:00
}
2020-06-23 20:16:12 +08:00
sec.addRow(title: "Alert.push(scene: .error)") {
Alert.push(scene: .error)
2019-08-12 20:20:07 +08:00
}
2020-06-23 20:16:12 +08:00
sec.addRow(title: "Alert.push(scene: .failure)") {
Alert.push(scene: .failure)
2019-08-12 20:20:07 +08:00
}
2020-06-23 20:16:12 +08:00
sec.footer = "这些是自带的场景,可以重写已有场景,或者扩展新的场景。"
}
vm.addSection(title: "常用场景示例") { (sec) in
// MARK: 1
sec.addRow(title: "场景同步成功写法1") {
Alert.push("loading") { (a) in
2019-08-12 20:20:07 +08:00
a.update { (vm) in
2020-06-23 20:16:12 +08:00
vm.scene = .loading
vm.title = "正在同步"
vm.message = "请稍等片刻"
2019-08-12 20:20:07 +08:00
}
2020-06-23 20:16:12 +08:00
a.startRotate()
}
loadingSuccessAfter2Seconds()
2019-08-12 20:20:07 +08:00
}
2020-06-23 20:16:12 +08:00
// MARK: 2
sec.addRow(title: "场景同步成功写法2") {
let a = Alert.push("loading") { (a) in
a.startRotate()
}
a.update { (vm) in
vm.scene = .loading
vm.title = "正在同步"
vm.message = "请稍等片刻"
}
loadingSuccessAfter2Seconds()
2019-08-12 17:59:40 +08:00
}
2020-06-23 20:16:12 +08:00
// MARK:
sec.addRow(title: "场景:正在同步(更新进度)") {
Alert.push("progress") { (a) in
2019-08-12 17:59:40 +08:00
a.update { (vm) in
vm.scene = .loading
vm.title = "正在同步"
vm.message = "请稍等片刻"
}
2020-06-22 13:27:54 +08:00
a.startRotate()
2020-06-23 20:16:12 +08:00
a.update(progress: 0)
let s = DispatchSemaphore(value: 1)
DispatchQueue.global().async {
for i in 0 ... 100 {
s.wait()
DispatchQueue.main.async {
Alert.find("progress", last: { (a) in
a.update(progress: CGFloat(i)/100)
print("\(CGFloat(i)/100)")
if i == 100 {
DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
a.update { (vm) in
vm.scene = .success
vm.title = "同步成功"
vm.message = nil
}
}
}
})
DispatchQueue.main.asyncAfter(deadline: .now()+0.03) {
s.signal()
}
2019-08-12 17:59:40 +08:00
}
}
}
2020-06-23 20:16:12 +08:00
}
}
// MARK:
sec.addRow(title: "场景:同步失败和重试(布局变化)") {
Alert.push("loading", scene: .loading)
func loading() {
Alert.find("loading") { (a) in
2020-06-16 16:50:53 +08:00
a.update { (vm) in
vm.scene = .loading
2020-06-23 20:16:12 +08:00
vm.title = "正在同步"
vm.message = "请稍等片刻"
vm.remove(action: 0, 1)
2020-06-16 16:50:53 +08:00
}
2020-06-22 13:27:54 +08:00
a.startRotate()
2020-06-23 20:16:12 +08:00
DispatchQueue.main.asyncAfter(deadline: .now()+2) {
a.update { (vm) in
vm.scene = .error
vm.title = "同步失败"
vm.message = "请检查网络是否通畅"
vm.add(action: .default, title: "重试") {
loading()
}
vm.add(action: .cancel, title: "取消", handler: nil)
}
}
2020-06-16 16:50:53 +08:00
}
}
2020-06-23 20:16:12 +08:00
loading()
2020-06-16 16:50:53 +08:00
}
2020-06-23 20:16:12 +08:00
sec.footer = "两种写法1和写法2动画效果略微不同"
}
vm.addSection(title: "为了解决代码逻辑疏漏导致程序卡死、弹窗重叠等问题") { (sec) in
// MARK:
sec.addRow(title: "极端场景:正在同步(超时未处理)", subtitle: "超时未处理的意外情况弹窗可以手动关闭。在config中配置所需时间") {
func f() {
Alert.push(scene: .loading, title: "正在同步", message: "请稍等片刻") { (a) in
a.identifier = "loading"
a.startRotate()
a.didForceQuit {
let t = Toast.push(scene: .loading, title: "正在同步", message: "请稍等片刻点击展开为Alert") { (vm) in
vm.identifier = "loading"
}
t.startRotate()
t.didTapped { [weak t] in
t?.pop()
f()
}
}
}
}
f()
2020-06-16 16:50:53 +08:00
}
2020-06-23 20:16:12 +08:00
// MARK:
sec.addRow(title: "极端场景:短时间内调用了多次同一个弹窗", subtitle: "多次调用页面弹窗应该是毫无变化的。本例中4秒内调用了6次") {
func loading(_ index: Int = 1) {
if Alert.find("loading").count == 0 {
Alert.push("loading", scene: .loading) { (a) in
a.update { (vm) in
vm.title = "正在加载"
}
a.startRotate()
}
}
}
loading(1)
DispatchQueue.main.asyncAfter(deadline: .now()+1) {
loading(2)
}
DispatchQueue.main.asyncAfter(deadline: .now()+2) {
loading(3)
}
DispatchQueue.main.asyncAfter(deadline: .now()+3) {
loading(4)
}
DispatchQueue.main.asyncAfter(deadline: .now()+3.5) {
loading(5)
}
DispatchQueue.main.asyncAfter(deadline: .now()+4) {
loading(6)
}
2020-06-16 16:50:53 +08:00
}
2020-06-23 20:16:12 +08:00
// MARK:
sec.addRow(title: "极端场景:多个不同的弹窗重叠", subtitle: "多个弹窗不得不重叠的时候ProHUD的景深处理可以使其看起来舒服一些。") {
func f(_ i: Int) {
Alert.push() { (a) in
a.startRotate()
a.update { (vm) in
vm.scene = .loading
vm.title = "正在同步" + String(i)
vm.message = "请稍等片刻"
}
}
}
f(1)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
f(2)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
f(3)
}
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
f(4)
}
2020-06-16 16:50:53 +08:00
}
2020-06-23 20:16:12 +08:00
}
vm.addSection(title: "") { (sec) in
// MARK:
sec.addRow(title: "测试较长的标题和内容") {
2019-08-13 11:32:27 +08:00
Alert.push() { (a) in
a.update { (vm) in
2020-06-23 20:16:12 +08:00
vm.scene = .confirm
vm.title = "正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过"
vm.message = "正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过"
vm.add(action: .default, title: "我知道了", handler: nil)
2019-08-13 11:32:27 +08:00
}
}
}
2020-06-23 20:16:12 +08:00
// MARK:
sec.addRow(title: "测试特别长的标题和内容", subtitle: "这种情况编码阶段就可以避免,所以没有做特别处理,请控制内容长度不要过长。") {
Alert.push() { (a) in
a.update { (vm) in
vm.scene = .warning
vm.title = "正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过"
vm.message = "正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过正在同步看到了你撒地方快乐撒的肌肤轮廓啊就是;来的跨省的人格人格离开那地方离开过"
vm.add(action: .default, title: "我知道了", handler: nil)
}
}
}
2020-06-23 20:16:12 +08:00
// MARK:
sec.addRow(title: "只有标题") {
Alert.push(scene: .default, title: "标题")
}
2020-06-23 20:16:12 +08:00
// MARK:
sec.addRow(title: "只有消息") {
Alert.push(scene: .default, message: "这是消息")
}
// MARK:
sec.addRow(title: "既没有标题也没有消息") {
Alert.push("a")
2020-06-22 16:34:21 +08:00
}
2019-08-12 17:59:40 +08:00
}
2020-06-23 20:16:12 +08:00
2019-08-12 17:59:40 +08:00
}
2020-06-23 20:16:12 +08:00
2019-08-12 17:59:40 +08:00
func simulateSync() {
DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
2019-08-12 20:20:07 +08:00
Alert.find("loading", last: { (a) in
a.update { (vm) in
2019-08-12 17:59:40 +08:00
vm.scene = .success
vm.title = "同步成功"
vm.message = "啊哈哈哈哈哈哈哈哈"
}
2019-08-12 20:20:07 +08:00
})
2019-08-12 17:59:40 +08:00
}
}
}