Skip to content

Commit

Permalink
Code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka committed Mar 13, 2024
1 parent 2f6b438 commit 5561a51
Show file tree
Hide file tree
Showing 13 changed files with 250 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ struct CreateHtmlEpubAnnotationsDbRequest: DbRequest {
var needsWrite: Bool { return true }

func process(in database: Realm) throws {
guard let parent = database.objects(RItem.self).filter(.key(self.attachmentKey, in: self.libraryId)).first else { return }
guard let parent = database.objects(RItem.self).filter(.key(attachmentKey, in: libraryId)).first else { return }

for annotation in self.annotations {
self.create(annotation: annotation, parent: parent, in: database)
for annotation in annotations {
create(annotation: annotation, parent: parent, in: database)
}
}

private func create(annotation: HtmlEpubAnnotation, parent: RItem, in database: Realm) {
let item: RItem

if let _item = database.objects(RItem.self).filter(.key(annotation.key, in: self.libraryId)).first {
if let _item = database.objects(RItem.self).filter(.key(annotation.key, in: libraryId)).first {
if !_item.deleted {
// If item exists and is not deleted locally, we can ignore this request
return
Expand All @@ -45,8 +45,8 @@ struct CreateHtmlEpubAnnotationsDbRequest: DbRequest {
item = RItem()
item.key = annotation.key
item.rawType = ItemTypes.annotation
item.localizedType = self.schemaController.localized(itemType: ItemTypes.annotation) ?? ""
item.libraryId = self.libraryId
item.localizedType = schemaController.localized(itemType: ItemTypes.annotation) ?? ""
item.libraryId = libraryId
item.dateAdded = annotation.dateCreated
database.add(item)
}
Expand All @@ -58,11 +58,11 @@ struct CreateHtmlEpubAnnotationsDbRequest: DbRequest {
item.parent = parent

if annotation.isAuthor {
item.createdBy = database.object(ofType: RUser.self, forPrimaryKey: self.userId)
item.createdBy = database.object(ofType: RUser.self, forPrimaryKey: userId)
}

// We need to submit tags on creation even if they are empty, so we need to mark them as changed
self.addFields(for: annotation, to: item, database: database)
addFields(for: annotation, to: item, database: database)
let changes: RItemChanges = [.parent, .fields, .type, .tags]
item.changes.append(RObjectChange.create(changes: changes))
}
Expand Down
18 changes: 9 additions & 9 deletions Zotero/Scenes/Detail/DetailCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ final class DetailCoordinator: Coordinator {
private func show(attachment: Attachment, parentKey: String?, library: Library, sourceView: UIView, sourceRect: CGRect?) {
switch attachment.type {
case .url(let url):
self.show(url: url)
show(url: url)

case .file(let filename, let contentType, _, _, _):
let file = Files.attachmentFile(in: library.identifier, key: attachment.key, filename: filename, contentType: contentType)
Expand All @@ -192,39 +192,39 @@ final class DetailCoordinator: Coordinator {
switch contentType {
case "application/pdf":
DDLogInfo("DetailCoordinator: show PDF \(attachment.key)")
self.showPdf(at: url, key: attachment.key, parentKey: parentKey, library: library)
showPdf(at: url, key: attachment.key, parentKey: parentKey, library: library)

case "text/html", "application/epub+zip":
DDLogInfo("DetailCoordinator: show HTML / EPUB \(attachment.key)")
self.showHtmlEpubReader(for: url, key: attachment.key, library: library)
showHtmlEpubReader(for: url, key: attachment.key, library: library)

case "text/plain":
let text = try? String(contentsOf: url, encoding: .utf8)
if let text = text {
DDLogInfo("DetailCoordinator: show plain text \(attachment.key)")
self.show(text: text, title: filename)
show(text: text, title: filename)
} else {
DDLogInfo("DetailCoordinator: share plain text \(attachment.key)")
self.share(item: url, sourceView: .view(sourceView, rect))
share(item: url, sourceView: .view(sourceView, rect))
}

case _ where contentType.contains("image"):
let image = (contentType == "image/gif") ? (try? Data(contentsOf: url)).flatMap({ try? UIImage(gifData: $0) }) : UIImage(contentsOfFile: url.path)
if let image = image {
DDLogInfo("DetailCoordinator: show image \(attachment.key)")
self.show(image: image, title: filename)
show(image: image, title: filename)
} else {
DDLogInfo("DetailCoordinator: share image \(attachment.key)")
self.share(item: url, sourceView: .view(sourceView, rect))
share(item: url, sourceView: .view(sourceView, rect))
}

default:
if AVURLAsset(url: url).isPlayable {
DDLogInfo("DetailCoordinator: show video \(attachment.key)")
self.showVideo(for: url)
showVideo(for: url)
} else {
DDLogInfo("DetailCoordinator: share attachment \(attachment.key)")
self.share(item: file.createUrl(), sourceView: .view(sourceView, rect))
share(item: file.createUrl(), sourceView: .view(sourceView, rect))
}
}
}
Expand Down
50 changes: 23 additions & 27 deletions Zotero/Scenes/Detail/HTML:EPUB/HtmlEpubCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ final class HtmlEpubCoordinator: Coordinator {
self.url = url
self.navigationController = navigationController
self.controllers = controllers
self.childCoordinators = []
self.disposeBag = DisposeBag()
childCoordinators = []
disposeBag = DisposeBag()

navigationController.dismissHandler = {
self.parentCoordinator?.childDidFinish(self)
navigationController.dismissHandler = { [weak self] in
guard let self else { return }
parentCoordinator?.childDidFinish(self)
}
}

Expand All @@ -75,10 +76,10 @@ final class HtmlEpubCoordinator: Coordinator {

func start(animated: Bool) {
let username = Defaults.shared.username
guard let dbStorage = self.controllers.userControllers?.dbStorage,
let userId = self.controllers.sessionController.sessionData?.userId,
guard let dbStorage = controllers.userControllers?.dbStorage,
let userId = controllers.sessionController.sessionData?.userId,
!username.isEmpty,
let parentNavigationController = self.parentCoordinator?.navigationController
let parentNavigationController = parentCoordinator?.navigationController
else { return }

let handler = HtmlEpubReaderActionHandler(
Expand All @@ -94,8 +95,7 @@ final class HtmlEpubCoordinator: Coordinator {
compactSize: UIDevice.current.isCompactWidth(size: parentNavigationController.view.frame.size)
)
controller.coordinatorDelegate = self

self.navigationController?.setViewControllers([controller], animated: false)
navigationController?.setViewControllers([controller], animated: false)
}
}

Expand Down Expand Up @@ -128,7 +128,7 @@ extension HtmlEpubCoordinator: HtmlEpubReaderCoordinatorDelegate {

let controller = UIAlertController(title: title, message: message, preferredStyle: .alert)
controller.addAction(UIAlertAction(title: L10n.ok, style: .default))
self.navigationController?.present(controller, animated: true)
navigationController?.present(controller, animated: true)
}

func showToolSettings(tool: AnnotationTool, colorHex: String?, sizeValue: Float?, sender: SourceView, userInterfaceStyle: UIUserInterfaceStyle, valueChanged: @escaping (String?, Float?) -> Void) {
Expand All @@ -141,14 +141,15 @@ extension HtmlEpubCoordinator: HtmlEpubReaderCoordinatorDelegate {
case .pad:
controller.overrideUserInterfaceStyle = userInterfaceStyle
controller.modalPresentationStyle = .popover

switch sender {
case .view(let view, _):
controller.popoverPresentationController?.sourceView = view

case .item(let item):
controller.popoverPresentationController?.barButtonItem = item
}
self.navigationController?.present(controller, animated: true, completion: nil)
navigationController?.present(controller, animated: true, completion: nil)

default:
let navigationController = UINavigationController(rootViewController: controller)
Expand All @@ -162,7 +163,7 @@ extension HtmlEpubCoordinator: HtmlEpubReaderCoordinatorDelegate {
extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {
func showTagPicker(libraryId: LibraryIdentifier, selected: Set<String>, userInterfaceStyle: UIUserInterfaceStyle?, picked: @escaping ([Tag]) -> Void) {
guard let navigationController else { return }
(self.parentCoordinator as? DetailCoordinator)?.showTagPicker(
(parentCoordinator as? DetailCoordinator)?.showTagPicker(
libraryId: libraryId,
selected: selected,
userInterfaceStyle: userInterfaceStyle,
Expand All @@ -184,7 +185,7 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {
navigationController.overrideUserInterfaceStyle = userInterfaceStyle

let coordinator = AnnotationEditCoordinator(
data: AnnotationEditState.AnnotationData(
data: AnnotationEditState.Data(
type: annotation.type,
isEditable: annotation.editability(currentUserId: userId, library: library) == .editable,
color: annotation.color,
Expand All @@ -195,10 +196,10 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {
saveAction: saveAction,
deleteAction: deleteAction,
navigationController: navigationController,
controllers: self.controllers
controllers: controllers
)
coordinator.parentCoordinator = self
self.childCoordinators.append(coordinator)
childCoordinators.append(coordinator)
coordinator.start(animated: false)

if UIDevice.current.userInterfaceIdiom == .pad {
Expand All @@ -216,21 +217,19 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {
popoverDelegate: UIPopoverPresentationControllerDelegate,
userInterfaceStyle: UIUserInterfaceStyle
) -> PublishSubject<AnnotationPopoverState>? {
guard let currentNavigationController = self.navigationController, let annotation = viewModel.state.selectedAnnotationKey.flatMap({ viewModel.state.annotations[$0] }) else { return nil }
guard let currentNavigationController = navigationController, let annotation = viewModel.state.selectedAnnotationKey.flatMap({ viewModel.state.annotations[$0] }) else { return nil }

DDLogInfo("HtmlEpubCoordinator: show annotation popover")

if let coordinator = self.childCoordinators.last, coordinator is AnnotationPopoverCoordinator {
if let coordinator = childCoordinators.last, coordinator is AnnotationPopoverCoordinator {
return nil
}

let navigationController = NavigationViewController()
navigationController.overrideUserInterfaceStyle = userInterfaceStyle

let author = viewModel.state.library.identifier == .custom(.myLibrary) ? "" : annotation.author
let comment = viewModel.state.comments[annotation.key] ?? NSAttributedString()
let editability = annotation.editability(currentUserId: viewModel.state.userId, library: viewModel.state.library)

let data = AnnotationPopoverState.Data(
libraryId: viewModel.state.library.identifier,
type: annotation.type,
Expand All @@ -246,7 +245,7 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {
)
let coordinator = AnnotationPopoverCoordinator(data: data, navigationController: navigationController, controllers: self.controllers)
coordinator.parentCoordinator = self
self.childCoordinators.append(coordinator)
childCoordinators.append(coordinator)
coordinator.start(animated: false)

if UIDevice.current.userInterfaceIdiom == .pad {
Expand Down Expand Up @@ -274,17 +273,16 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {

let navigationController = NavigationViewController()
navigationController.overrideUserInterfaceStyle = userInterfaceStyle

let coordinator = AnnotationsFilterPopoverCoordinator(
initialFilter: filter,
availableColors: availableColors,
availableTags: availableTags,
navigationController: navigationController,
controllers: self.controllers,
controllers: controllers,
completionHandler: completed
)
coordinator.parentCoordinator = self
self.childCoordinators.append(coordinator)
childCoordinators.append(coordinator)
coordinator.start(animated: false)

if UIDevice.current.userInterfaceIdiom == .pad {
Expand All @@ -301,20 +299,18 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {

let state = ReaderSettingsState(settings: settings)
let viewModel = ViewModel(initialState: state, handler: ReaderSettingsActionHandler())
let baseController = ReaderSettingsViewController(visibleRows: [.appearance, .sleep], viewModel: viewModel)

let baseController = ReaderSettingsViewController(rows: [.appearance, .sleep], viewModel: viewModel)
let controller: UIViewController
if UIDevice.current.userInterfaceIdiom == .pad {
controller = baseController
} else {
controller = UINavigationController(rootViewController: baseController)
}

controller.modalPresentationStyle = UIDevice.current.userInterfaceIdiom == .pad ? .popover : .formSheet
controller.popoverPresentationController?.barButtonItem = sender
controller.preferredContentSize = CGSize(width: 480, height: 92)
controller.overrideUserInterfaceStyle = settings.appearance.userInterfaceStyle
self.navigationController?.present(controller, animated: true, completion: nil)
navigationController?.present(controller, animated: true, completion: nil)

return viewModel
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct HtmlEpubAnnotation {
if !library.metadataEditable {
return .notEditable
}
return self.isAuthor ? .editable : .deletable
return isAuthor ? .editable : .deletable
}
}
}
20 changes: 10 additions & 10 deletions Zotero/Scenes/Detail/HTML:EPUB/Models/HtmlEpubReaderState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ struct HtmlEpubReaderState: ViewModelState {
self.library = library
self.userId = userId
self.username = username
self.commentFont = PDFReaderLayout.annotationLayout.font
self.sortedKeys = []
self.annotations = [:]
self.comments = [:]
self.sidebarEditingEnabled = false
self.selectedAnnotationCommentActive = false
self.toolColors = [
commentFont = PDFReaderLayout.annotationLayout.font
sortedKeys = []
annotations = [:]
comments = [:]
sidebarEditingEnabled = false
selectedAnnotationCommentActive = false
toolColors = [
.highlight: UIColor(hex: Defaults.shared.highlightColorHex),
.note: UIColor(hex: Defaults.shared.noteColorHex)
]
self.changes = []
self.deletionEnabled = false
self.selectedAnnotationsDuringEditing = []
changes = []
deletionEnabled = false
selectedAnnotationsDuringEditing = []
}

mutating func cleanup() {
Expand Down
6 changes: 3 additions & 3 deletions Zotero/Scenes/Detail/HTML:EPUB/Models/HtmlEpubSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ extension HtmlEpubSettings: Codable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let appearanceRaw = try container.decode(UInt.self, forKey: .appearance)
self.appearance = ReaderSettingsState.Appearance(rawValue: appearanceRaw) ?? .automatic
appearance = ReaderSettingsState.Appearance(rawValue: appearanceRaw) ?? .automatic
// This setting is not persisted, always defaults to false
self.idleTimerDisabled = false
idleTimerDisabled = false
}

func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(self.appearance.rawValue, forKey: .appearance)
try container.encode(appearance.rawValue, forKey: .appearance)
}
}
Loading

0 comments on commit 5561a51

Please sign in to comment.