Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FulcrumOne committed Mar 10, 2024
0 parents commit fecd1af
Show file tree
Hide file tree
Showing 14 changed files with 1,132 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: [https://www.buymeacoffee.com/mijickteam]
17 changes: 17 additions & 0 deletions .github/release-cocoapods.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Release CocoaPods
on:
push:
branches:
- master
workflow_dispatch:

jobs:
build:
runs-on: macOS-latest
steps:
- uses: actions/checkout@v1
- name: Publish to CocoaPod register
env:
COCOAPODS_TRUNK_TOKEN: ${{ secrets.COCOAPODS_TRUNK_TOKEN }}
run: |
pod trunk push MijickTimer.podspec
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
18 changes: 18 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "MijickTimer",
platforms: [
.iOS(.v13)
],
products: [
.library(name: "MijickTimer", targets: ["MijickTimer"]),
],
targets: [
.target(name: "MijickTimer", dependencies: [], path: "Sources"),
.testTarget(name: "MijickTimerTests", dependencies: ["MijickTimer"], path: "Tests")
]
)
176 changes: 176 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
<br>

<p align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/Mijick/Assets/blob/main/Timer/Logotype/On%20Dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://github.com/Mijick/Assets/blob/main/Timer/Logotype/On%20Light.svg">
<img alt="Timer Logo" src="https://github.com/Mijick/Assets/blob/main/Timer/Logotype/On%20Dark.svg" width="76%"">
</picture>
</p>

<h3 style="font-size: 5em" align="center">
Modern API for Timer
</h3>

<p align="center">
Easy to use yet powerful Timer library. Keep your code clean
</p>

<p align="center">
<a href="https://github.com/Mijick/Timer-Demo" rel="nofollow">Try demo we prepared</a>
</p>

<br>

<p align="center">
<img alt="SwiftUI logo" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/Language.svg"/>
<img alt="Platforms: iOS, iPadOS, macOS, tvOS" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/Platforms.svg"/>
<img alt="Current Version" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/Version.svg"/>
<img alt="License: MIT" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/License.svg"/>
</p>

<p align="center">
<img alt="Made in Kraków" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/Origin.svg"/>
<a href="https://twitter.com/MijickTeam">
<img alt="Follow us on X" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/X.svg"/>
</a>
<a href=mailto:[email protected]?subject=Hello>
<img alt="Let's work together" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/Work%20with%20us.svg"/>
</a>
<a href="https://github.com/Mijick/Timer/stargazers">
<img alt="Stargazers" src="https://github.com/Mijick/Assets/blob/main/Timer/Labels/Stars.svg"/>
</a>
</p>

<p align="center">
<img alt="Timer Examples" src="https://github.com/Mijick/Assets/blob/main/Timer/GIFs/Timer.gif"/>
</p>

<br>

Timer is a free and open-source library dedicated for Swift that makes the process of handling timers easier and much cleaner.
* **Improves code quality.** Start timer using the `publish().start()` method. Stop the timer with `stop()`. Simple as never.
* **Run your timer in both directions.** Our Timer can operate in both modes (increasing or decreasing).
* **Supports background mode.** Don't worry about the timer when the app goes into the background. We handled it!
* **And much more.** Our library allows you to convert the current time to a string or to display the timer progress in no time.

<br>

# Getting Started
### ✋ Requirements

| **Platforms** | **Minimum Swift Version** |
|:----------|:----------|
| iOS 13+ | 5.0 |

### ⏳ Installation

#### [Swift package manager][spm]
Swift package manager is a tool for automating the distribution of Swift code and is integrated into the Swift compiler.

Once you have your Swift package set up, adding Timer as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.

```Swift
dependencies: [
.package(url: "https://github.com/Mijick/Timer", branch(“main”))
]
```
<br>

# Usage

### 1. Initialise the timer
Call the `publish()` method that has three parameters:
* **time** - The number of seconds between firings of the timer.
* **tolerance** - The number of seconds after the update date that the timer may fire.
* **currentTime** - The current timer time.
```Swift
try! MTimer.publish(every: 1, currentTime: $currentTime)
```

### 2. Start the timer
Start the timer using the `start()` method. You can customise the start and end time using the parameters of this method.
```Swift
try! MTimer
.publish(every: 1, currentTime: $currentTime)
.start(from: .init(minutes: 21, seconds: 37), to: .zero)
```

### 3. *(Optional)* Observe TimerStatus and TimerProgress
You can observe changes in both values by calling either of the methods
```Swift
try! MTimer
.publish(every: 1, currentTime: $currentTime)
.bindTimerStatus(isTimerRunning: $isTimerRunning)
.bindTimerProgress(progress: $timerProgress)
.start(from: .init(minutes: 21, seconds: 37), to: .zero)
```

### 4. Stop the timer
Timer can be stopped with `stop()` method.
```Swift
MTimer.stop()
```

### 5. Additional timer controls
- Once stopped, the timer can be resumed - simply use the `resume()` method.
```Swift
try! MTimer.resume()
```
- To stop and reset the timer to its initial values, use the `reset()` method.
```Swift
MTimer.reset()
```

### 6. Displaying the current time as String
You can convert the current MTime to String by calling the `toString()` method. Use the `formatter` parameter to customise the output.
```Swift
currentTime.toString {
$0.unitsStyle = .full
$0.allowedUnits = [.hour, .minute]
return $0
}
```

### 7. Creating more timer instances
Create a new instance of the timer and assign it to a new variable. Use the above functions directly with it
```Swift
let newTimer = MTimer.createNewInstance()

try! newTimer
.publish(every: 1, currentTime: $currentTime)
.start()

newTimer.stop()
```

<br>

# Try our demo
See for yourself how does it work by cloning [project][Demo] we created

# License
Timer is released under the MIT license. See [LICENSE][License] for details.

<br><br>

# Our other open source SwiftUI libraries
[PopupView] - The most powerful popup library that allows you to present any popup
<br>
[Navigattie] - Easier and cleaner way of navigating through your app
<br>
[GridView] - Lay out your data with no effort




[MIT]: https://en.wikipedia.org/wiki/MIT_License
[SPM]: https://www.swift.org/package-manager
[Demo]: https://github.com/Mijick/Timer-Demo
[License]: https://github.com/Mijick/Timer/blob/main/LICENSE
[PopupView]: https://github.com/Mijick/PopupView
[Navigattie]: https://github.com/Mijick/Navigattie
[GridView]: https://github.com/Mijick/GridView
23 changes: 23 additions & 0 deletions Sources/Internal/Extensions/NotificationCenter++.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NotificationCenter++.swift of Timer
//
// Created by Tomasz Kurylik
// - Twitter: https://twitter.com/tkurylik
// - Mail: [email protected]
// - GitHub: https://github.com/FulcrumOne
//
// Copyright ©2023 Mijick. Licensed under MIT License.


import SwiftUI

extension NotificationCenter {
static func addAppStateNotifications(_ observer: Any, onDidEnterBackground backgroundNotification: Selector, onWillEnterForeground foregroundNotification: Selector) {
Self.default.addObserver(observer, selector: (backgroundNotification), name: UIApplication.didEnterBackgroundNotification, object: nil)
Self.default.addObserver(observer, selector: (foregroundNotification), name: UIApplication.willEnterForegroundNotification, object: nil)
}
static func removeAppStateChangedNotifications(_ observer: Any) {
Self.default.removeObserver(observer, name: UIApplication.didEnterBackgroundNotification, object: nil)
Self.default.removeObserver(observer, name: UIApplication.willEnterForegroundNotification, object: nil)
}
}
50 changes: 50 additions & 0 deletions Sources/Internal/MTime.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// MTime.swift of Timer
//
// Created by Tomasz Kurylik
// - Twitter: https://twitter.com/tkurylik
// - Mail: [email protected]
// - GitHub: https://github.com/FulcrumOne
//
// Copyright ©2023 Mijick. Licensed under MIT License.


import Foundation

public struct MTime: Equatable {
public let hours: Int
public let minutes: Int
public let seconds: Int
public let milliseconds: Int
}
extension MTime {
init(_ timeInterval: TimeInterval) {
let millisecondsInt = Int(timeInterval * 1000)

let hoursDiv = 1000 * 60 * 60
let minutesDiv = 1000 * 60
let secondsDiv = 1000
let millisecondsDiv = 1

hours = millisecondsInt / hoursDiv
minutes = (millisecondsInt % hoursDiv) / minutesDiv
seconds = (millisecondsInt % hoursDiv % minutesDiv) / secondsDiv
milliseconds = (millisecondsInt % hoursDiv % minutesDiv % secondsDiv) / millisecondsDiv
}
}

// MARK: - Helpers
extension MTime {
var defaultTimeFormatter: DateComponentsFormatter {
let formatter = DateComponentsFormatter()

formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .positional
formatter.zeroFormattingBehavior = .pad
formatter.maximumUnitCount = 0
formatter.allowsFractionalUnits = false
formatter.collapsesLargestUnit = false

return formatter
}
}
Loading

0 comments on commit fecd1af

Please sign in to comment.