Skip to content

Commit

Permalink
Updated reader, got rid of idle timer setting
Browse files Browse the repository at this point in the history
  • Loading branch information
michalrentka committed Nov 29, 2024
1 parent 1da0451 commit 513da43
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 39 deletions.
5 changes: 1 addition & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@
url = https://github.com/zotero/translators.git
[submodule "note-editor"]
path = note-editor
url = https://github.com/zotero/note-editor
[submodule "reader"]
path = reader
url = https://github.com/zotero/reader
url = https://github.com/zotero/note-editor
2 changes: 1 addition & 1 deletion Zotero/Scenes/Detail/HTML:EPUB/HtmlEpubCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ extension HtmlEpubCoordinator: HtmlEpubSidebarCoordinatorDelegate {

let state = ReaderSettingsState(settings: settings)
let viewModel = ViewModel(initialState: state, handler: ReaderSettingsActionHandler())
let baseController = ReaderSettingsViewController(rows: [.appearance, .sleep], viewModel: viewModel)
let baseController = ReaderSettingsViewController(rows: [.appearance], viewModel: viewModel)
let controller: UIViewController
if UIDevice.current.userInterfaceIdiom == .pad {
controller = baseController
Expand Down
5 changes: 1 addition & 4 deletions Zotero/Scenes/Detail/HTML:EPUB/Models/HtmlEpubSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import UIKit

struct HtmlEpubSettings {
var appearance: ReaderSettingsState.Appearance
var idleTimerDisabled: Bool

static var `default`: HtmlEpubSettings {
return HtmlEpubSettings(appearance: .automatic, idleTimerDisabled: false)
return HtmlEpubSettings(appearance: .automatic)
}
}

Expand All @@ -26,8 +25,6 @@ extension HtmlEpubSettings: Codable {
let container = try decoder.container(keyedBy: Keys.self)
let appearanceRaw = try container.decode(UInt.self, forKey: .appearance)
appearance = ReaderSettingsState.Appearance(rawValue: appearanceRaw) ?? .automatic
// This setting is not persisted, always defaults to false
idleTimerDisabled = false
}

func encode(to encoder: Encoder) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ final class HtmlEpubReaderActionHandler: ViewModelActionHandler, BackgroundDbPro
set(settings: settings, in: viewModel)

case .changeIdleTimerDisabled(let disabled):
changeIdleTimer(disabled: disabled, in: viewModel)
if disabled {
idleTimerController.startCustomIdleTimer()
} else {
idleTimerController.stopCustomIdleTimer()
}
}
}

Expand All @@ -155,37 +159,11 @@ final class HtmlEpubReaderActionHandler: ViewModelActionHandler, BackgroundDbPro
}
}

private func changeIdleTimer(disabled: Bool, in viewModel: ViewModel<HtmlEpubReaderActionHandler>) {
guard viewModel.state.settings.idleTimerDisabled != disabled else { return }
var settings = viewModel.state.settings
settings.idleTimerDisabled = disabled

update(viewModel: viewModel) { state in
state.settings = settings
// Don't need to assign `changes` or update Defaults.shared.htmlEpubSettings, this setting is not stored and doesn't change anything else
}

if settings.idleTimerDisabled {
idleTimerController.disable()
} else {
idleTimerController.enable()
}
}

private func set(settings: HtmlEpubSettings, in viewModel: ViewModel<HtmlEpubReaderActionHandler>) {
if viewModel.state.settings.idleTimerDisabled != settings.idleTimerDisabled {
if settings.idleTimerDisabled {
idleTimerController.disable()
} else {
idleTimerController.enable()
}
}

update(viewModel: viewModel) { state in
state.settings = settings
state.changes = .settings
}

Defaults.shared.htmlEpubSettings = settings
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class HtmlEpubReaderViewController: UIViewController, ParentWithSidebarControlle
super.viewDidLoad()

setActivity()
viewModel.process(action: .changeIdleTimerDisabled(true))
view.backgroundColor = .systemBackground
observeViewModel()
setupNavigationBar()
Expand Down Expand Up @@ -248,6 +249,7 @@ class HtmlEpubReaderViewController: UIViewController, ParentWithSidebarControlle
}

deinit {
viewModel.process(action: .changeIdleTimerDisabled(false))
viewModel.process(action: .deinitialiseReader)
DDLogInfo("HtmlEpubReaderViewController deinitialized")
}
Expand Down Expand Up @@ -395,14 +397,13 @@ class HtmlEpubReaderViewController: UIViewController, ParentWithSidebarControlle
viewModel.stateObservable
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] state in
let settings = HtmlEpubSettings(appearance: state.appearance, idleTimerDisabled: state.idleTimerDisabled)
let settings = HtmlEpubSettings(appearance: state.appearance)
self?.viewModel.process(action: .setSettings(settings))
})
.disposed(by: disposeBag)
}

private func close() {
viewModel.process(action: .changeIdleTimerDisabled(false))
navigationController?.presentingViewController?.dismiss(animated: true)
}

Expand Down
1 change: 0 additions & 1 deletion Zotero/Scenes/General/Models/ReaderSettingsState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct ReaderSettingsState: ViewModelState {

init(settings: HtmlEpubSettings) {
appearance = settings.appearance
idleTimerDisabled = settings.idleTimerDisabled
// These don't apply to HTML/Epub, assign random values
transition = .curl
pageMode = .automatic
Expand Down

0 comments on commit 513da43

Please sign in to comment.