Skip to content

Commit

Permalink
Rename and restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
yalishanda42 committed Jun 7, 2023
1 parent b37ae15 commit 6d918c2
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 120 deletions.
8 changes: 8 additions & 0 deletions BDZDelays.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
742E26E02A2666450009092D /* DelaysWidget.intentdefinition in Sources */ = {isa = PBXBuildFile; fileRef = 742E26A22A26013E0009092D /* DelaysWidget.intentdefinition */; };
742E26E32A2667700009092D /* SharedModels in Frameworks */ = {isa = PBXBuildFile; productRef = 742E26E22A2667700009092D /* SharedModels */; };
742E26E52A266EC40009092D /* BGStation+fromName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 742E26E42A266EC40009092D /* BGStation+fromName.swift */; };
749D7F202A310A590088E161 /* TrainsTimelineProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749D7F1E2A310A590088E161 /* TrainsTimelineProvider.swift */; };
749D7F222A310A9D0088E161 /* TrainsEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 749D7F212A310A9D0088E161 /* TrainsEntry.swift */; };
74B48B642A11887D0061DD98 /* FavoritesServiceLive in Frameworks */ = {isa = PBXBuildFile; productRef = 74B48B632A11887D0061DD98 /* FavoritesServiceLive */; };
74ECD0952A0FED5000C577DD /* SettingsURLServiceLive in Frameworks */ = {isa = PBXBuildFile; productRef = 74ECD0942A0FED5000C577DD /* SettingsURLServiceLive */; };
74FB4A692A13FAE000FD7290 /* LogServiceLive in Frameworks */ = {isa = PBXBuildFile; productRef = 74FB4A682A13FAE000FD7290 /* LogServiceLive */; };
Expand Down Expand Up @@ -87,6 +89,8 @@
742E26C52A2666000009092D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
742E26CB2A2666010009092D /* IntentsUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = IntentsUI.framework; path = System/Library/Frameworks/IntentsUI.framework; sourceTree = SDKROOT; };
742E26E42A266EC40009092D /* BGStation+fromName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BGStation+fromName.swift"; sourceTree = "<group>"; };
749D7F1E2A310A590088E161 /* TrainsTimelineProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrainsTimelineProvider.swift; sourceTree = "<group>"; };
749D7F212A310A9D0088E161 /* TrainsEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrainsEntry.swift; sourceTree = "<group>"; };
74FB4A6E2A1584CA00FD7290 /* GoogleService-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
A067207B29F31B96008A4D15 /* BDZDelays.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BDZDelays.app; sourceTree = BUILT_PRODUCTS_DIR; };
A067207E29F31B96008A4D15 /* BDZDelaysApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BDZDelaysApp.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -141,6 +145,8 @@
742E26A22A26013E0009092D /* DelaysWidget.intentdefinition */,
742E269E2A26013E0009092D /* DelaysWidgetBundle.swift */,
742E26A02A26013E0009092D /* DelaysWidget.swift */,
749D7F212A310A9D0088E161 /* TrainsEntry.swift */,
749D7F1E2A310A590088E161 /* TrainsTimelineProvider.swift */,
742E26B62A26507E0009092D /* WidgetView.swift */,
742E26E42A266EC40009092D /* BGStation+fromName.swift */,
742E26A32A26013F0009092D /* Assets.xcassets */,
Expand Down Expand Up @@ -386,6 +392,8 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
749D7F222A310A9D0088E161 /* TrainsEntry.swift in Sources */,
749D7F202A310A590088E161 /* TrainsTimelineProvider.swift in Sources */,
742E26A62A26013F0009092D /* DelaysWidget.intentdefinition in Sources */,
742E26B72A26507E0009092D /* WidgetView.swift in Sources */,
742E26E52A266EC40009092D /* BGStation+fromName.swift in Sources */,
Expand Down
119 changes: 1 addition & 118 deletions DelaysWidget/DelaysWidget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,131 +9,14 @@ import WidgetKit
import SwiftUI
import Intents

import Dependencies

import SharedModels
import StationRepository
import StationRepositoryLive

struct Provider: IntentTimelineProvider {
typealias Entry = SimpleEntry

@Dependency(\.stationRepository) var repository

func placeholder(in context: Context) -> Entry {
Entry(date: Date(), configuration: SelectStationIntent(), result: .success(prevewiewData))
}

func getSnapshot(
for configuration: SelectStationIntent,
in context: Context,
completion: @escaping (Entry) -> ()
) {
let entry = Entry(
date: Date(),
configuration: configuration,
result: .success(prevewiewData)
)
completion(entry)
}

func getTimeline(
for configuration: SelectStationIntent,
in context: Context,
completion: @escaping (Timeline<Entry>) -> ()
) {
Task {
let trainsResult: Result<[TrainAtStation], Error>?

defer {
let now = Date()
let entry = Entry(date: now, configuration: configuration, result: trainsResult)
let refreshDate = Calendar.current.date(byAdding: .minute, value: 1, to: now)!
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}

guard
let name = configuration.station,
let station = BGStation(name: name)
else {
trainsResult = nil
return
}

do {
trainsResult = .success(try await repository.fetchTrainsAtStation(station))
} catch let error {
trainsResult = .failure(error)
}
}
}
}

struct SimpleEntry: TimelineEntry {
let date: Date
let configuration: SelectStationIntent
let result: Result<[TrainAtStation], Error>?
}

struct DelaysWidget: Widget {
let kind: String = "DelaysWidget"

var body: some WidgetConfiguration {
IntentConfiguration(kind: kind, intent: SelectStationIntent.self, provider: Provider()) { entry in
IntentConfiguration(kind: kind, intent: SelectStationIntent.self, provider: TrainsTimelineProvider()) { entry in
WidgetView(entry: entry)
}
.configurationDisplayName("БДЖ Закъснения")
.description("Тази джаджа ви показва движението на влаковете от/към избрана гара в реално време за следващите 6 часа.")
}
}

fileprivate let prevewiewData: [TrainAtStation] = [
.init(
number: TrainNumber(
type: .suburban,
number: 2112
),
from: .bulgarian(.sofia),
to: .bulgarian(.varna),
schedule: .departureOnly(Date(timeIntervalSince1970: 3600)),
delay: nil,
movement: .leavingStation
),
.init(
number: TrainNumber(
type: .suburban,
number: 21123
),
from: .bulgarian(.sofia),
to: .bulgarian(.burgas),
schedule: .departureOnly(Date(timeIntervalSince1970: 3600)),
delay: .seconds(240),
movement: .leavingStation
),
.init(
number: TrainNumber(
type: .fast,
number: 2212
),
from: .bulgarian(.sofia),
to: .bulgarian(.vraca),
schedule: .arrivalOnly(Date(timeIntervalSince1970: 3600)),
delay: .seconds(240),
movement: .inOperation
),
.init(
number: TrainNumber(
type: .normal,
number: 2312
),
from: .bulgarian(.plovdiv),
to: .bulgarian(.sofia),
schedule: .full(
arrival: Date(timeIntervalSince1970: 3600),
departure: Date(timeIntervalSince1970: 3660)
),
delay: nil,
movement: .notYetOperating
),
]
16 changes: 16 additions & 0 deletions DelaysWidget/TrainsEntry.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// TrainsEntry.swift
// DelaysWidgetExtension
//
// Created by AI on 7.06.23.
//

import WidgetKit

import SharedModels

struct TrainsEntry: TimelineEntry {
let date: Date
let configuration: SelectStationIntent
let result: Result<[TrainAtStation], Error>?
}
121 changes: 121 additions & 0 deletions DelaysWidget/TrainsTimelineProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
//
// TrainsTimelineProvider.swift
// BDZDelays
//
// Created by AI on 7.06.23.
//

import WidgetKit
import SwiftUI
import Intents

import Dependencies

import SharedModels
import StationRepository
import StationRepositoryLive

struct TrainsTimelineProvider: IntentTimelineProvider {
typealias Entry = TrainsEntry

@Dependency(\.stationRepository) var repository

func placeholder(in context: Context) -> Entry {
Entry(date: Date(), configuration: SelectStationIntent(), result: .success(prevewiewData))
}

func getSnapshot(
for configuration: SelectStationIntent,
in context: Context,
completion: @escaping (Entry) -> ()
) {
let entry = Entry(
date: Date(),
configuration: configuration,
result: .success(prevewiewData)
)
completion(entry)
}

func getTimeline(
for configuration: SelectStationIntent,
in context: Context,
completion: @escaping (Timeline<Entry>) -> ()
) {
Task {
let trainsResult: Result<[TrainAtStation], Error>?

defer {
let now = Date()
let entry = Entry(date: now, configuration: configuration, result: trainsResult)
let refreshDate = Calendar.current.date(byAdding: .minute, value: 1, to: now)!
let timeline = Timeline(entries: [entry], policy: .after(refreshDate))
completion(timeline)
}

guard
let name = configuration.station,
let station = BGStation(name: name)
else {
trainsResult = nil
return
}

do {
trainsResult = .success(try await repository.fetchTrainsAtStation(station))
} catch let error {
trainsResult = .failure(error)
}
}
}
}

fileprivate let prevewiewData: [TrainAtStation] = [
.init(
number: TrainNumber(
type: .suburban,
number: 2112
),
from: .bulgarian(.sofia),
to: .bulgarian(.varna),
schedule: .departureOnly(Date(timeIntervalSince1970: 3600)),
delay: nil,
movement: .leavingStation
),
.init(
number: TrainNumber(
type: .suburban,
number: 21123
),
from: .bulgarian(.sofia),
to: .bulgarian(.burgas),
schedule: .departureOnly(Date(timeIntervalSince1970: 3600)),
delay: .seconds(240),
movement: .leavingStation
),
.init(
number: TrainNumber(
type: .fast,
number: 2212
),
from: .bulgarian(.sofia),
to: .bulgarian(.vraca),
schedule: .arrivalOnly(Date(timeIntervalSince1970: 3600)),
delay: .seconds(240),
movement: .inOperation
),
.init(
number: TrainNumber(
type: .normal,
number: 2312
),
from: .bulgarian(.plovdiv),
to: .bulgarian(.sofia),
schedule: .full(
arrival: Date(timeIntervalSince1970: 3600),
departure: Date(timeIntervalSince1970: 3660)
),
delay: nil,
movement: .notYetOperating
),
]
4 changes: 2 additions & 2 deletions DelaysWidget/WidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import SharedModels
// MARK: - View

struct WidgetView: View {
let entry: Provider.Entry
let entry: TrainsTimelineProvider.Entry

@Environment(\.widgetFamily) private var size: WidgetFamily

Expand Down Expand Up @@ -179,7 +179,7 @@ struct WidgetView_Previews: PreviewProvider {

static var previews: some View {
ForEach(sizes, id: \.1) {
WidgetView(entry: SimpleEntry(
WidgetView(entry: TrainsEntry(
date: Date(),
configuration: SelectStationIntent(),
result: nil
Expand Down

0 comments on commit 6d918c2

Please sign in to comment.