-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #913 from DroidKaigi/ios-staffs-base
[iOS] Implement staffs base
- Loading branch information
Showing
11 changed files
with
206 additions
and
17 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
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
39 changes: 39 additions & 0 deletions
39
app-ios/Modules/Sources/KMPContainer/StaffsDataProvider.swift
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,39 @@ | ||
import Dependencies | ||
import shared | ||
|
||
public struct StaffsDataProvider { | ||
private static var staffRepository: StaffRepository { | ||
Container.shared.get(type: StaffRepository.self) | ||
} | ||
|
||
public let refresh: () async throws -> Void | ||
public let staffs: () -> AsyncThrowingStream<[Staff], Error> | ||
} | ||
|
||
extension StaffsDataProvider: DependencyKey { | ||
@MainActor | ||
public static var liveValue: StaffsDataProvider = StaffsDataProvider( | ||
refresh: { @MainActor in | ||
try await staffRepository.refresh() | ||
}, | ||
staffs: { | ||
staffRepository.staffs().stream() | ||
} | ||
) | ||
|
||
public static var testValue: StaffsDataProvider = StaffsDataProvider( | ||
refresh: {}, | ||
staffs: { | ||
.init { | ||
Staff.companion.fakes() | ||
} | ||
} | ||
) | ||
} | ||
|
||
public extension DependencyValues { | ||
var staffsData: StaffsDataProvider { | ||
get { self[StaffsDataProvider.self] } | ||
set { self[StaffsDataProvider.self] = newValue } | ||
} | ||
} |
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,13 @@ | ||
import Foundation | ||
|
||
public struct IdentifiableURL: Identifiable { | ||
public var id: URL? | ||
|
||
public init(_ id: URL?) { | ||
self.id = id | ||
} | ||
|
||
public init(string: String) { | ||
self.id = URL(string: string) | ||
} | ||
} |
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,52 @@ | ||
import Component | ||
import Model | ||
import shared | ||
import SwiftUI | ||
|
||
public struct StaffView: View { | ||
@State var presentingURL: IdentifiableURL? | ||
@ObservedObject var viewModel: StaffViewModel = .init() | ||
|
||
public init() {} | ||
|
||
public var body: some View { | ||
Group { | ||
switch viewModel.state.staffs { | ||
case .initial, .loading: | ||
ProgressView() | ||
.task { | ||
await viewModel.load() | ||
} | ||
case .failed: | ||
EmptyView() | ||
case .loaded(let staffs): | ||
ScrollView { | ||
LazyVStack(spacing: 20) { | ||
ForEach(staffs, id: \.id) { staff in | ||
Button { | ||
presentingURL = IdentifiableURL(string: staff.profileUrl) | ||
} label: { | ||
PersonLabel( | ||
name: staff.username, | ||
iconUrlString: staff.iconUrl | ||
) | ||
} | ||
} | ||
} | ||
.padding(16) | ||
} | ||
} | ||
} | ||
.navigationTitle("Staff") | ||
.sheet(item: $presentingURL) { url in | ||
if let url = url.id { | ||
SafariView(url: url) | ||
.ignoresSafeArea() | ||
} | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
StaffView() | ||
} |
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,26 @@ | ||
import Dependencies | ||
import Foundation | ||
import KMPContainer | ||
import Model | ||
import shared | ||
|
||
struct StaffState: ViewModelState { | ||
var staffs: LoadingState<[Staff]> = .initial | ||
} | ||
|
||
@MainActor | ||
final class StaffViewModel: ObservableObject { | ||
@Dependency(\.staffsData) var staffsData | ||
@Published private(set) var state: StaffState = .init() | ||
|
||
func load() async { | ||
state.staffs = .loading | ||
do { | ||
for try await staffs in staffsData.staffs() { | ||
state.staffs = .loaded(staffs) | ||
} | ||
} catch let error { | ||
state.staffs = .failed(error) | ||
} | ||
} | ||
} |
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,11 @@ | ||
@testable import Staff | ||
import XCTest | ||
|
||
final class StaffTests: XCTestCase { | ||
func testExample() throws { | ||
// This is an example of a functional test case. | ||
// Use XCTAssert and related functions to verify your tests produce the correct | ||
// results. | ||
XCTAssertEqual("Hello, World!", "Hello, World!") | ||
} | ||
} |