Skip to content

Commit 712a723

Browse files
authored
TestApp: Update OutlineTableViewController to use SwiftUI (#42)
1 parent 1270198 commit 712a723

17 files changed

+403
-455
lines changed

TestApp/Sources/Data/Bookmark.swift

+3
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ final class BookmarkRepository {
6464
db.write { db in try Bookmark.deleteOne(db, key: id) }
6565
}
6666
}
67+
68+
// for the default SwiftUI support
69+
extension Bookmark: Hashable {}

TestApp/Sources/Data/Highlight.swift

+3
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,6 @@ final class HighlightRepository {
110110
db.write { db in try Highlight.deleteOne(db, key: id) }
111111
}
112112
}
113+
114+
// for the default SwiftUI support
115+
extension Highlight: Hashable {}

TestApp/Sources/Reader/Common/Bookmark/BookmarkCell.swift

-55
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Copyright 2022 Readium Foundation. All rights reserved.
3+
// Use of this source code is governed by the BSD-style license
4+
// available in the top-level LICENSE file of the project.
5+
//
6+
7+
import SwiftUI
8+
9+
struct BookmarkCellView: View {
10+
static var dateFormatter: DateFormatter {
11+
let formatter = DateFormatter()
12+
formatter.dateStyle = .medium
13+
return formatter
14+
}
15+
16+
let bookmark: Bookmark
17+
var body: some View {
18+
HStack {
19+
VStack {
20+
Text(bookmark.locator.title ?? "")
21+
.frame(maxWidth: .infinity, alignment: .leading)
22+
.font(.headline)
23+
Text(bookmark.positionText ?? "")
24+
.font(.footnote)
25+
.frame(maxWidth: .infinity, alignment: .leading)
26+
}
27+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
28+
29+
Text(BookmarkCellView.dateFormatter.string(from: bookmark.created))
30+
.font(.footnote)
31+
.frame(maxHeight: .infinity, alignment: .bottomTrailing)
32+
}
33+
.padding()
34+
}
35+
}
36+
37+
extension Bookmark {
38+
var positionText: String? {
39+
if let position = locator.locations.position {
40+
return String(format: NSLocalizedString("reader_outline_position_label", comment: "Outline bookmark label when the position is available"), position)
41+
} else if let progression = locator.locations.progression {
42+
return String(format: NSLocalizedString("reader_outline_progression_label", comment: "Outline bookmark label when the progression is available"), progression * 100)
43+
} else {
44+
return nil
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Copyright 2022 Readium Foundation. All rights reserved.
3+
// Use of this source code is governed by the BSD-style license
4+
// available in the top-level LICENSE file of the project.
5+
//
6+
7+
import SwiftUI
8+
import Combine
9+
import UIKit
10+
import R2Shared // for UserProperty
11+
12+
// This color scheme is passed to SwiftUI. In the (SwiftUI) future, EnvironmentObject can be injected and updated from UserDefaults in some reactive way.
13+
class ColorScheme {
14+
private(set) var mainColor: Color = Color.white
15+
private(set) var textColor: Color = Color.black
16+
17+
func update(with appearance: UserProperty) {
18+
mainColor = Color(AssociatedColors.getColors(for: appearance).mainColor)
19+
textColor = Color(AssociatedColors.getColors(for: appearance).textColor)
20+
}
21+
}
22+
23+
struct ColorModifier: ViewModifier {
24+
let colorScheme: ColorScheme
25+
func body(content: Content) -> some View {
26+
content
27+
.foregroundColor(colorScheme.textColor)
28+
.background(colorScheme.mainColor)
29+
}
30+
}
31+
32+
extension View {
33+
func colorStyle(_ colorScheme: ColorScheme) -> some View {
34+
modifier(ColorModifier(colorScheme: colorScheme))
35+
}
36+
}

TestApp/Sources/Reader/Common/Highlight/HighlightCellView.swift

+2-32
Original file line numberDiff line numberDiff line change
@@ -21,41 +21,11 @@ struct HighlightCellView: View {
2121
.fill(Color(highlight.color.uiColor))
2222
.frame(maxWidth: 20, maxHeight: .infinity)
2323

24-
Spacer()
25-
2624
Text(highlight.locator.text.sanitized().highlight ?? "")
27-
.frame(maxWidth: .infinity)
25+
.frame(maxWidth: .infinity, alignment: .topLeading)
2826
.padding()
29-
}
30-
}
31-
}
32-
33-
// This UITableView-SwiftUI wrapper is from here: https://stackoverflow.com/a/59474133
34-
class HostingTableViewCell<Content: View>: UITableViewCell {
35-
36-
private weak var controller: UIHostingController<Content>?
37-
38-
func host(_ view: Content, parent: UIViewController) {
39-
if let controller = controller {
40-
controller.rootView = view
41-
controller.view.layoutIfNeeded()
42-
} else {
43-
let swiftUICellViewController = UIHostingController(rootView: view)
44-
controller = swiftUICellViewController
45-
swiftUICellViewController.view.backgroundColor = .clear
4627

47-
layoutIfNeeded()
48-
49-
parent.addChild(swiftUICellViewController)
50-
contentView.addSubview(swiftUICellViewController.view)
51-
swiftUICellViewController.view.translatesAutoresizingMaskIntoConstraints = false
52-
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.leading, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.leading, multiplier: 1.0, constant: 0.0))
53-
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.trailing, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.trailing, multiplier: 1.0, constant: 0.0))
54-
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.top, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.top, multiplier: 1.0, constant: 0.0))
55-
contentView.addConstraint(NSLayoutConstraint(item: swiftUICellViewController.view!, attribute: NSLayoutConstraint.Attribute.bottom, relatedBy: NSLayoutConstraint.Relation.equal, toItem: contentView, attribute: NSLayoutConstraint.Attribute.bottom, multiplier: 1.0, constant: 0.0))
56-
57-
swiftUICellViewController.didMove(toParent: parent)
58-
swiftUICellViewController.view.layoutIfNeeded()
28+
Spacer()
5929
}
6030
}
6131
}

TestApp/Sources/Reader/Common/Highlight/HighlightContextMenu.swift

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Combine
1010
struct HighlightContextMenu: View {
1111
let colors: [HighlightColor]
1212
let systemFontSize: CGFloat
13+
let colorScheme: ColorScheme
1314

1415
private let colorSubject = PassthroughSubject<HighlightColor, Never>()
1516
var selectedColorPublisher: AnyPublisher<HighlightColor, Never> {
@@ -40,6 +41,7 @@ struct HighlightContextMenu: View {
4041
.font(.system(size: systemFontSize))
4142
}
4243
}
44+
.colorStyle(colorScheme)
4345
}
4446

4547
var preferredSize: CGSize {

TestApp/Sources/Reader/Common/Outline/Base.lproj/Outline.storyboard

-105
This file was deleted.

0 commit comments

Comments
 (0)