IdleDetector is a Swift library for iOS that helps you track user activity and detect idle time in your SwiftUI applications. It provides a simple way to add idle time detection to your views and react to changes in user activity.
- Easy integration with SwiftUI views
- Customizable idle time threshold
- Automatic tracking of user interactions
- Access to idle time and idle state through environment objects
- Thread-safe implementation using Swift's concurrency model
- iOS 14.0+
- Swift 5.5+
- Xcode 13.0+
You can install IdleDetector using the Swift Package Manager:
- In Xcode, select "File" → "Swift Packages" → "Add Package Dependency"
- Enter the repository URL:
https://github.com/SeikoLai/SwiftUI_IdleDetector.git
- Select the version you want to use
Alternatively, you can add the following to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/SeikoLai/SwiftUI_IdleDetector.git", from: "1.0.0")
]
- Import the IdleDetector module in your SwiftUI view file:
import SwiftUI
import IdleDetector
- Apply the idle detection to your view:
struct ContentView: View {
var body: some View {
IdleDetector.trackUserActivity(
Text("Hello, World!")
)
}
}
You can access the idle time information in child views using the @EnvironmentObject
property wrapper:
struct IdleTimeView: View {
@EnvironmentObject var idleTimeManager: IdleTimeManager
var body: some View {
VStack {
Text("Idle Time: \(Int(idleTimeManager.idleTime)) seconds")
Text("Is Idle: \(idleTimeManager.isIdle ? "Yes" : "No")")
}
}
}
You can customize the idle time threshold when applying the tracker:
IdleDetector.trackUserActivity(
YourView(),
idleThreshold: 30 // Set idle threshold to 30 seconds
)
The main interface for the IdleDetector library.
-
static func trackUserActivity<Content: View>(_ content: Content, idleThreshold: TimeInterval = 10) -> some View
Applies the user activity tracker to a view.
Manages and tracks idle time in an application.
-
var idleTime: TimeInterval
(read-only)The current idle time in seconds.
-
var isIdle: Bool
(read-only)Indicates whether the application is considered idle.
-
func resetIdleTime()
Resets the idle time when user interaction is detected.
IdleDetector uses Swift's concurrency model to ensure thread safety. The main components are marked with @MainActor
to ensure they're always accessed on the main thread, preventing potential race conditions and ensuring smooth integration with SwiftUI's view lifecycle.
IdleDetector is released under the MIT License. See LICENSE for details.
Contributions are welcome! Please feel free to submit a Pull Request.
If you have any questions or issues, please open an issue on the GitHub repository.
- Improved thread safety using Swift's actor model
- Fixed issues related to accessing UI components from background threads
- Initial release