Skip to content

Commit

Permalink
Maintenance (#171)
Browse files Browse the repository at this point in the history
- Renamed from `TKServer.developmentServer` to `TKServer.customBaseURL` and added docs
- Update Japanese translation
  • Loading branch information
nighthawk authored Apr 27, 2022
1 parent 12e224c commit 907ff3d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Sources/TripKit/Resources/ja.lproj/Shared.strings
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
"Don't have an account? Sign up" = "アカウントを持っていますか・サインアップ";

/* Done action */
"Done" = "行われます";
"Done" = "実行";

/* Drop new pin */
"Drop new pin" = "新しいピンをドロップしてください";
Expand Down Expand Up @@ -200,7 +200,7 @@
"Not wheelchair accessible" = "車椅子対応不可";

/* Countdown cell 'now' indicator */
"Now" = "";
"Now" = "現在時刻";

/* Ok action */
"OK" = "わかりました";
Expand Down
8 changes: 4 additions & 4 deletions Sources/TripKit/Resources/ja.lproj/TripKit.strings
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"Arrive at %@ at %@" = "%2$@に %1$@に到着";

/* Arrive before button */
"Arrive by" = "到着します。";
"Arrive by" = "到着";

/* Title for when you'll arrive when on a trip. Countdown to arrival will be displayed below. */
"Arrive in" = "到着します。";
Expand Down Expand Up @@ -198,7 +198,7 @@
"Get directions" = "道順します。";

/* Instruction to get a service towards provided destination */
"Get on service to %@" = "%@までサービスを開始します";
"Get on service to %@" = "%@まで";

/* Title of button to start a trip */
"Go" = "開始";
Expand Down Expand Up @@ -355,10 +355,10 @@
"Routing from %@ to %@ is not yet supported" = "%1$@ から %2$@ へのルートはまだサポートされていません";

/* Label to indicate that the time for a service is the scheduled time, i.e., displayed as on the timetable with no real-time data available. */
"Scheduled" = "予定済み";
"Scheduled" = "発着予定";

/* No comment provided by engineer. */
"Search for destination" = "宛先を検索";
"Search for destination" = "目的地を検索";

/* Error recovery suggestion for when when server could not find a given transit stop. */
"Search for this stop again or try again later.." = "この停止を再度検索または、後でもう一度試してください.";
Expand Down
8 changes: 4 additions & 4 deletions Sources/TripKit/server/TKServer+Regions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
extension TKRegionManager {

enum RegionError: Error {
case invalidDevelopmentServer
case invalidCustomBaseURL
}

/// Fetched the list of regions and updates `TKRegionManager`'s cache
Expand Down Expand Up @@ -47,9 +47,9 @@ extension TKRegionManager {

private func fetchRegions(forced: Bool) async throws {
let regionsURL: URL
if let developmentServer = TKServer.developmentServer {
guard let url = URL(string: developmentServer) else {
throw RegionError.invalidDevelopmentServer
if let customBaseURL = TKServer.customBaseURL {
guard let url = URL(string: customBaseURL) else {
throw RegionError.invalidCustomBaseURL
}
regionsURL = url.appendingPathComponent("regions.json")
} else {
Expand Down
27 changes: 23 additions & 4 deletions Sources/TripKit/server/TKServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension TKServer {

public static func imageURL(iconFileNamePart: String?, iconType: TKStyleModeIconType? = nil) -> URL? {
guard let iconFileNamePart = iconFileNamePart else { return nil }
let regionsURLString = TKServer.developmentServer ?? "https://api.tripgo.com/v1"
let regionsURLString = TKServer.customBaseURL ?? "https://api.tripgo.com/v1"

let isPNG: Bool
let fileNamePrefix: String
Expand Down Expand Up @@ -77,12 +77,19 @@ extension TKServer {
return URL(string: urlString)
}

@available(*, unavailable, renamed: "customBaseURL")
public static var developmentServer: String? {
get { customBaseURL }
set { customBaseURL = newValue }
}

/// Custom base URL to use, instead of hitting SkedGo's default servers directly.
public static var customBaseURL: String? {
get {
UserDefaults.shared.string(forKey: "developmentServer")
}
set {
let oldValue = developmentServer
let oldValue = customBaseURL
if var newValue = newValue, !newValue.isEmpty {
if !newValue.hasSuffix("/") {
newValue.append("/")
Expand All @@ -99,8 +106,10 @@ extension TKServer {
}
}

/// Base URL to use for server calls when `customBaseURL` is not set and the server calls
/// do not specify a ```TKRegion```.
public static var fallbackBaseURL: URL {
developmentServer.flatMap(URL.init) ?? URL(string: "https://api.tripgo.com/v1/")!
customBaseURL.flatMap(URL.init) ?? URL(string: "https://api.tripgo.com/v1/")!
}

}
Expand All @@ -125,7 +134,7 @@ extension TKServer {

@objc // so that subclasses can override
func baseURLs(for region: TKRegion?) -> [URL] {
if let dev = Self.developmentServer.flatMap(URL.init) {
if let dev = Self.customBaseURL.flatMap(URL.init) {
return [dev]
} else if let urls = region?.urls, !urls.isEmpty {
return urls
Expand Down Expand Up @@ -231,6 +240,7 @@ extension TKServer {
return try dataResult.orThrow(ServerError.noData).get()
}

/// :nodoc: - Public for Objective-C only
@objc(GET:paras:completion:)
public func _get(url: URL, parameters: [String: Any]? = nil, completion: @escaping (Int, [String: Any], Any?, Data?, Error?) -> Void) {
hit(.GET, url: url, parameters: parameters) { status, headers, result in
Expand All @@ -244,6 +254,7 @@ extension TKServer {
}
}

/// :nodoc: - Public for Objective-C only
@objc(POST:paras:completion:)
public func _post(url: URL, parameters: [String: Any]? = nil, completion: @escaping (Int, [String: Any], Any?, Data?, Error?) -> Void) {
hit(.POST, url: url, parameters: parameters) { status, headers, result in
Expand All @@ -262,9 +273,17 @@ extension TKServer {
// MARK: - Async/await

extension TKServer {
/// Captures server response with HTTP status code, headers and typed response
public struct Response<T> {

/// HTTP status code of response. Can be `nil` if request failed.
public var statusCode: Int?

/// HTTP response headers. Can be empty if request failed.
public var headers: [String: Any]

/// Types response, which can encapsulate a failure if the server returned an error
/// or if the server's data couldn't be decoded as the appropriate type.
public var result: Result<T, Error>
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/TripKitUITests/TKUIGeocoderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class TKUIGeocoderTest: XCTestCase {
override func setUpWithError() throws {
try super.setUpWithError()

if TKServer.developmentServer != nil {
TKServer.developmentServer = nil // make sure to hit production
if TKServer.customBaseURL != nil {
TKServer.customBaseURL = nil // make sure to hit production
}

geocoder = try aggregateGeocoder()
Expand Down

0 comments on commit 907ff3d

Please sign in to comment.