Skip to content

Commit

Permalink
add: source code
Browse files Browse the repository at this point in the history
  • Loading branch information
SNQ-2001 committed Nov 23, 2022
1 parent 74b3e6e commit 74fc07a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
3 changes: 3 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import PackageDescription

let package = Package(
name: "ScreenTimeout",
platforms: [
.iOS(.v13)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
Expand Down
54 changes: 51 additions & 3 deletions Sources/ScreenTimeout/ScreenTimeout.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,54 @@
public struct ScreenTimeout {
public private(set) var text = "Hello, World!"
import SwiftUI

public init() {
public extension View {
func onTimeout(seconds: TimeInterval = 30, perform: ((ScreenStates) -> Void)?) -> some View {
Timeout(content: self, seconds: seconds) { status in
if let perform {
perform(status)
}
}
}
}

public enum ScreenStates {
case active
case timeout
}

fileprivate struct Timeout<T: View>: View {
var content: T
var seconds: TimeInterval
var perform: ((ScreenStates) -> Void)

@State var timer: Timer? = nil
@State var states: ScreenStates = .timeout

var body: some View {
content
.onTapGesture {
start()
}
.onAppear {
start()
}
.onDisappear {
stop()
}
}

private func start() {
if states == .timeout {
states = .active
perform(.active)
}
timer?.invalidate()
timer = Timer.scheduledTimer(withTimeInterval: seconds, repeats: false) { _ in
states = .timeout
perform(.timeout)
}
}

private func stop() {
timer?.invalidate()
}
}

0 comments on commit 74fc07a

Please sign in to comment.