-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f340c6
commit f69d4aa
Showing
5 changed files
with
77 additions
and
71 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// The source to be used for a feed of notes. | ||
enum FeedSource: RawRepresentable, Hashable, Equatable { | ||
case following | ||
case relay(host: String, description: String?) | ||
case list(name: String, description: String?) | ||
|
||
var displayName: String { | ||
switch self { | ||
case .following: String(localized: "following") | ||
case .relay(let name, _), .list(let name, _): name | ||
} | ||
} | ||
|
||
var description: String? { | ||
switch self { | ||
case .following: nil | ||
case .relay(_, let description), .list(_, let description): description | ||
} | ||
} | ||
|
||
static func == (lhs: FeedSource, rhs: FeedSource) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.following, .following): true | ||
case (.relay(let name1, _), .relay(let name2, _)): name1 == name2 | ||
case (.list(let name1, _), .list(let name2, _)): name1 == name2 | ||
default: false | ||
} | ||
} | ||
|
||
// Note: RawRepresentable conformance is required for use of @AppStorage for persistence. | ||
var rawValue: String { | ||
switch self { | ||
case .following: | ||
"following" | ||
case .relay(let host, let description): | ||
"relay:|\(host):|\(description ?? "")" | ||
case .list(let name, let description): | ||
"list:|\(name):|\(description ?? "")" | ||
} | ||
} | ||
|
||
init?(rawValue: String) { | ||
let components = rawValue.split(separator: ":|").map { String($0) } | ||
guard let caseName = components.first else { | ||
return nil | ||
} | ||
|
||
switch caseName { | ||
case "following": | ||
self = .following | ||
case "relay": | ||
guard components.count >= 2 else { | ||
return nil | ||
} | ||
let description = components.count >= 3 ? components[2] : "" | ||
self = .relay(host: components[1], description: description) | ||
case "list": | ||
guard components.count >= 2 else { | ||
return nil | ||
} | ||
let description = components.count >= 3 ? components[2] : "" | ||
self = .list(name: components[1], description: description) | ||
default: | ||
return nil | ||
} | ||
} | ||
} |
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