File tree Expand file tree Collapse file tree 2 files changed +54
-3
lines changed
Expand file tree Collapse file tree 2 files changed +54
-3
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,9 @@ import PackageDescription
55
66let package = Package (
77 name: " ScreenTimeout " ,
8+ platforms: [
9+ . iOS( . v13)
10+ ] ,
811 products: [
912 // Products define the executables and libraries a package produces, and make them visible to other packages.
1013 . library(
Original file line number Diff line number Diff line change 1- public struct ScreenTimeout {
2- public private( set) var text = " Hello, World! "
1+ import SwiftUI
32
4- public init ( ) {
3+ public extension View {
4+ func onTimeout( seconds: TimeInterval = 30 , perform: ( ( ScreenStates ) -> Void ) ? ) -> some View {
5+ Timeout ( content: self , seconds: seconds) { status in
6+ if let perform {
7+ perform ( status)
8+ }
9+ }
10+ }
11+ }
12+
13+ public enum ScreenStates {
14+ case active
15+ case timeout
16+ }
17+
18+ fileprivate struct Timeout < T: View > : View {
19+ var content : T
20+ var seconds : TimeInterval
21+ var perform : ( ( ScreenStates ) -> Void )
22+
23+ @State var timer : Timer ? = nil
24+ @State var states : ScreenStates = . timeout
25+
26+ var body : some View {
27+ content
28+ . onTapGesture {
29+ start ( )
30+ }
31+ . onAppear {
32+ start ( )
33+ }
34+ . onDisappear {
35+ stop ( )
36+ }
37+ }
38+
39+ private func start( ) {
40+ if states == . timeout {
41+ states = . active
42+ perform ( . active)
43+ }
44+ timer? . invalidate ( )
45+ timer = Timer . scheduledTimer ( withTimeInterval: seconds, repeats: false ) { _ in
46+ states = . timeout
47+ perform ( . timeout)
48+ }
49+ }
50+
51+ private func stop( ) {
52+ timer? . invalidate ( )
553 }
654}
You can’t perform that action at this time.
0 commit comments