Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ dist/
keys/
*.jks
*.keystore

# iOS archives and unsigned IPAs
ios/build/
16 changes: 16 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ val keystorePassword: String? = System.getenv("MCCLOCK_KEYSTORE_PASSWORD")
val keystoreAlias: String? = System.getenv("MCCLOCK_KEY_ALIAS")
val keystoreKeyPassword: String? = System.getenv("MCCLOCK_KEY_PASSWORD")

val robolectricRuntimeConfiguration = configurations.create("robolectricAndroidAllRuntime")
val robolectricCompileConfiguration = configurations.create("robolectricAndroidAllCompile")
val robolectricJarDir = layout.buildDirectory.dir("robolectric-jars")

android {
namespace = "com.coobyk.mc.clock"
compileSdk = 35
Expand Down Expand Up @@ -82,7 +86,17 @@ android {
// and CI uploads that directory. Declaring it as a task output means the Gradle build cache restores
// the PNGs on a cache hit; left undeclared they simply do not exist whenever the test task is not
// actually executed, which is how the upload step came to fail on an otherwise green build.
// Resolve the Robolectric runtime through Gradle and point tests at the copy: MavenArtifactFetcher
// otherwise downloads it itself at test time, outside Gradle's dependency cache and retry handling.
val copyRobolectricAndroidAll = tasks.register<Copy>("copyRobolectricAndroidAll") {
from(robolectricRuntimeConfiguration)
from(robolectricCompileConfiguration)
into(robolectricJarDir)
}

tasks.withType<Test>().configureEach {
dependsOn(copyRobolectricAndroidAll)
systemProperty("robolectric.dependency.dir", robolectricJarDir.get().asFile.absolutePath)
outputs.dir(layout.buildDirectory.dir("widget-renders"))
}

Expand All @@ -101,5 +115,7 @@ dependencies {

testImplementation(libs.junit)
testImplementation(libs.robolectric)
add("robolectricAndroidAllRuntime", "org.robolectric:android-all-instrumented:14-robolectric-10818077-i7")
add("robolectricAndroidAllCompile", "org.robolectric:android-all-instrumented:15-robolectric-12650502-i7")
testImplementation(libs.androidx.test.core)
}
498 changes: 498 additions & 0 deletions ios/MCClock.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

106 changes: 106 additions & 0 deletions ios/MCClock.xcodeproj/xcshareddata/xcschemes/MCClock.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic"
runPostActionsOnFailure = "NO">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3B4B8F5EF5264D8DF8B8588A"
BuildableName = "MCClock.app"
BlueprintName = "MCClock"
ReferencedContainer = "container:MCClock.xcodeproj">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "FFA2430F77047B8B084E92C2"
BuildableName = "MCClockWidget.appex"
BlueprintName = "MCClockWidget"
ReferencedContainer = "container:MCClock.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
onlyGenerateCoverageForSpecifiedTargets = "NO">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3B4B8F5EF5264D8DF8B8588A"
BuildableName = "MCClock.app"
BlueprintName = "MCClock"
ReferencedContainer = "container:MCClock.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3B4B8F5EF5264D8DF8B8588A"
BuildableName = "MCClock.app"
BlueprintName = "MCClock"
ReferencedContainer = "container:MCClock.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "3B4B8F5EF5264D8DF8B8588A"
BuildableName = "MCClock.app"
BlueprintName = "MCClock"
ReferencedContainer = "container:MCClock.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
138 changes: 138 additions & 0 deletions ios/MCClock/AlarmModels.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import Foundation

struct AlarmModel: Codable, Identifiable, Equatable {
var id: Int64 = Int64(Date().timeIntervalSince1970 * 1000)
var hour: Int = 7
var minute: Int = 0
var label: String = ""
var enabled: Bool = true
var days: Int = 0
var snoozeMinutes: Int = 10
var createdAt: Date = Date()

enum CodingKeys: String, CodingKey {
case id, hour, minute, label, enabled, days, snoozeMinutes, createdAt
}

init() {}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decodeIfPresent(Int64.self, forKey: .id) ?? Int64(Date().timeIntervalSince1970 * 1000)
hour = try container.decodeIfPresent(Int.self, forKey: .hour) ?? 7
minute = try container.decodeIfPresent(Int.self, forKey: .minute) ?? 0
label = try container.decodeIfPresent(String.self, forKey: .label) ?? ""
enabled = try container.decodeIfPresent(Bool.self, forKey: .enabled) ?? true
days = try container.decodeIfPresent(Int.self, forKey: .days) ?? 0
snoozeMinutes = try container.decodeIfPresent(Int.self, forKey: .snoozeMinutes) ?? 10
createdAt = try container.decodeIfPresent(Date.self, forKey: .createdAt) ?? Date()
}

var isSpentOneTime: Bool {
days == 0 && nextTriggerDate(from: createdAt) <= Date()
}

func nextTriggerDate(from date: Date = Date()) -> Date {
let calendar = Calendar.current
for offset in 0...7 {
guard let day = calendar.date(byAdding: .day, value: offset, to: date) else { continue }
var components = calendar.dateComponents([.year, .month, .day, .weekday], from: day)
components.hour = hour
components.minute = minute
components.second = 0
guard let candidate = calendar.date(from: components), candidate > date else { continue }
let bit = 1 << ((components.weekday ?? 1) - 1)
if days == 0 || (days & bit) != 0 { return candidate }
}
let tomorrow = calendar.date(byAdding: .day, value: 1, to: date) ?? date.addingTimeInterval(86400)
var components = calendar.dateComponents([.year, .month, .day], from: tomorrow)
components.hour = hour
components.minute = minute
components.second = 0
return calendar.date(from: components) ?? tomorrow
}

var timeText: String {
String(format: "%02d:%02d", hour, minute)
}

var daysText: String {
if days == 0 { return "Once" }
let names = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
let selected = (0..<7).compactMap { (days & (1 << $0)) != 0 ? names[$0] : nil }
return selected.isEmpty ? "Once" : selected.joined(separator: " ")
}
}

final class AlarmStore: ObservableObject {
@Published private(set) var alarms: [AlarmModel] = []
private let key = "alarms-json"

init() {
load()
}

func load() {
guard let data = UserDefaults.standard.data(forKey: key),
let decoded = try? JSONDecoder().decode([AlarmModel].self, from: data) else {
alarms = []
return
}
var migrated = decoded
for index in migrated.indices where migrated[index].isSpentOneTime && migrated[index].enabled {
migrated[index].enabled = false
}
alarms = migrated
if migrated != decoded { persist() }
}

func save(_ alarm: AlarmModel) {
if let index = alarms.firstIndex(where: { $0.id == alarm.id }) {
alarms[index] = alarm
} else {
alarms.append(alarm)
}
persist()
}

func delete(_ alarm: AlarmModel) {
alarms.removeAll { $0.id == alarm.id }
persist()
}

private func persist() {
if let data = try? JSONEncoder().encode(alarms) {
UserDefaults.standard.set(data, forKey: key)
}
}
}

enum TimerStorage {
private static let endAtKey = "timer-end-at"
private static let remainingKey = "timer-remaining-ms"
private static let runningKey = "timer-running"

static var endAt: Date? {
get {
let value = UserDefaults.standard.double(forKey: endAtKey)
return value > 0 ? Date(timeIntervalSince1970: value) : nil
}
set { UserDefaults.standard.set(newValue?.timeIntervalSince1970 ?? 0, forKey: endAtKey) }
}

static var remainingMilliseconds: Int64 {
get { Int64(UserDefaults.standard.double(forKey: remainingKey)) }
set { UserDefaults.standard.set(Double(newValue), forKey: remainingKey) }
}

static var running: Bool {
get { UserDefaults.standard.bool(forKey: runningKey) }
set { UserDefaults.standard.set(newValue, forKey: runningKey) }
}

static func reset() {
endAt = nil
remainingMilliseconds = 0
running = false
}
}
Loading
Loading