-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): bring back iOS17 support
- Loading branch information
Showing
7 changed files
with
168 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
@available(iOS 18.0, *) | ||
struct CountdownIOS18: View { | ||
let date: Date | ||
|
||
init(_ date: Date) { | ||
self.date = date | ||
} | ||
|
||
var body: some View { | ||
Text( | ||
.currentDate, | ||
format: | ||
.reference( | ||
to: date, | ||
allowedFields: [.second, .minute, .hour], | ||
maxFieldCount: 2 | ||
) | ||
) | ||
} | ||
} | ||
|
||
struct CountdownLegacy: View { | ||
let date: Date | ||
@State var countdownText: String = " - " | ||
|
||
init(_ date: Date) { | ||
self.date = date | ||
} | ||
|
||
func updateCountdownText() { | ||
let formatter = RelativeDateTimeFormatter() | ||
formatter.unitsStyle = .abbreviated | ||
countdownText = formatter.localizedString(for: date, relativeTo: .now) | ||
} | ||
|
||
let timer = Timer.publish(every: 0.5, on: .main, in: .common).autoconnect() | ||
|
||
var body: some View { | ||
Text(countdownText) | ||
.onAppear { | ||
updateCountdownText() | ||
} | ||
.onReceive(timer) { _ in | ||
updateCountdownText() | ||
} | ||
} | ||
} | ||
|
||
struct Countdown: View { | ||
let date: Date | ||
|
||
init(_ date: Date) { | ||
self.date = date | ||
} | ||
|
||
var body: some View { | ||
Group { | ||
if #available(iOS 18.0, *) { | ||
CountdownIOS18(date) | ||
} else { | ||
CountdownLegacy(date) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview("Seconds") { | ||
Countdown(Date(timeIntervalSinceNow: 59)) | ||
} | ||
|
||
#Preview("Seconds in past") { | ||
Countdown(Date(timeIntervalSinceNow: 0)) | ||
} | ||
|
||
#Preview("Minutes") { | ||
Countdown(Date(timeIntervalSinceNow: 59 * 60)) | ||
} | ||
|
||
#Preview("Minutes in past") { | ||
Countdown(Date(timeIntervalSinceNow: -1 * 60)) | ||
} | ||
|
||
#Preview("Hours") { | ||
Countdown(Date(timeIntervalSinceNow: 10 * 60 * 60)) | ||
} | ||
|
||
|
||
#Preview("Hours in past") { | ||
Countdown(Date(timeIntervalSinceNow: -1 * 2 * 60 * 60)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters