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
2 changes: 1 addition & 1 deletion Sources/ScrollkeeperUI/Views/LibraryWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct LibraryWindow: View {
var body: some View {
@Bindable var store = store
NavigationSplitView {
SidebarView()
SidebarView(showsOneColumn: showsOneColumn)
.navigationSplitViewColumnWidth(min: 190, ideal: 220)
} detail: {
catalog
Expand Down
29 changes: 24 additions & 5 deletions Sources/ScrollkeeperUI/Views/SidebarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import SwiftUI

/// The scopes of the library with their title counts.
struct SidebarView: View {
/// Whether the window shows one column at a time, as on an iPhone, where the sidebar is a page of its own.
let showsOneColumn: Bool
@Environment(LibraryStore.self) private var store
/// Set when Back leaves the library for the sidebar on an iPhone. The scope is kept, so the library still
/// shows it, but the list shows no selection: a selection there would open the library again.
@State private var isShowingSidebarAlone = false

var body: some View {
@Bindable var store = store
List(selection: scopeSelection($store.query.scope)) {
List(selection: scopeSelection) {
Section("Library") {
row(.all, "All Titles", symbol: "books.vertical")
row(.downloaded, "Downloaded", symbol: "arrow.down.circle")
Expand All @@ -31,6 +35,8 @@ struct SidebarView: View {
}
}
.listStyle(.sidebar)
// The layout can widen, as when an iPad leaves Slide Over, and then the list shows the scope again.
.onChange(of: showsOneColumn) { isShowingSidebarAlone = false }
}

private func row(_ scope: LibraryScope, _ title: String, symbol: String) -> some View {
Expand All @@ -39,8 +45,21 @@ struct SidebarView: View {
.tag(scope)
}

/// The list needs an optional selection; an emptied selection falls back to all titles.
private func scopeSelection(_ scope: Binding<LibraryScope>) -> Binding<LibraryScope?> {
Binding(get: { scope.wrappedValue }, set: { scope.wrappedValue = $0 ?? .all })
/// The list needs an optional selection. An emptied selection means Back on an iPhone, which shows the sidebar;
/// elsewhere it falls back to all titles.
private var scopeSelection: Binding<LibraryScope?> {
// A closure, not `set: choose`: Swift 6.3's compiler crashes generating the method reference's thunk.
Binding(get: { isShowingSidebarAlone ? nil : store.query.scope }, set: { choose($0) })
}

private func choose(_ scope: LibraryScope?) {
if let scope {
isShowingSidebarAlone = false
store.query.scope = scope
} else if showsOneColumn {
isShowingSidebarAlone = true
} else {
store.query.scope = .all
}
}
}
22 changes: 22 additions & 0 deletions ios/UITests/PhoneLayoutUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ final class PhoneLayoutUITests: XCTestCase {
XCTAssertTrue(app.buttons["View on paizo.com"].waitForExistence(timeout: 5), app.debugDescription)
}

/// Back from the library shows the sidebar and stays there, rather than bouncing back to All Titles.
@MainActor
func testBackFromTheLibraryStaysOnTheSidebar() {
launchDemo(viewMode: "covers")

goBackToTheSidebar(from: "All Titles")
app.staticTexts["Rulebooks"].firstMatch.tap()
XCTAssertTrue(app.navigationBars["Rulebooks"].waitForExistence(timeout: 5), app.debugDescription)
goBackToTheSidebar(from: "Rulebooks")
}

@MainActor
private func goBackToTheSidebar(from scope: String) {
let library = app.navigationBars[scope]
library.buttons["Back"].tap()
XCTAssertTrue(library.waitForNonExistence(timeout: 5), app.debugDescription)
// The bounce came a moment after the sidebar appeared, so check that the library does not come back.
XCTAssertFalse(app.navigationBars["All Titles"].waitForExistence(timeout: 3),
"went back to the library: \(app.debugDescription)")
XCTAssertTrue(app.staticTexts["Downloaded"].firstMatch.isHittable, app.debugDescription)
}

@MainActor
private func launchDemo(viewMode: String?) {
app = XCUIApplication()
Expand Down
Loading