-
Notifications
You must be signed in to change notification settings - Fork 2
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 #8 from qwertyyb/develop
feat: add lazy load for list
- Loading branch information
Showing
7 changed files
with
107 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ScrollView.swift | ||
// YPaste | ||
// | ||
// Created by marchyang on 2019/10/21. | ||
// Copyright © 2019 qwertyyb. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
class ScrollView: NSScrollView { | ||
|
||
override func draw(_ dirtyRect: NSRect) { | ||
super.draw(dirtyRect) | ||
|
||
// Drawing code here. | ||
} | ||
|
||
override func scrollWheel(with event: NSEvent) { | ||
super.scrollWheel(with: event) | ||
guard let documentView = self.documentView, let scroller = self.verticalScroller, | ||
event.scrollingDeltaY < 0 | ||
else { return } | ||
let scrollDistance = CGFloat(scroller.doubleValue) * documentView.frame.height | ||
if (documentView.frame.height - scrollDistance < 100) { | ||
let notification = Notification(name: Notification.Name(rawValue: "scrollerview-ToReachBottom"), object: nil) | ||
NotificationCenter.default.post(notification) | ||
} | ||
} | ||
|
||
|
||
|
||
} |
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 @@ | ||
// | ||
// PasteItemsController.swift | ||
// YPaste | ||
// | ||
// Created by marchyang on 2019/10/21. | ||
// Copyright © 2019 qwertyyb. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
|
||
class PasteItemsController: NSArrayController { | ||
override init(content: Any?) { | ||
super.init(content: content) | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
NotificationCenter.default.addObserver(self, selector: #selector(fetchNextPage), name: Notification.Name(rawValue: "scrollerview-ToReachBottom"), object: nil) | ||
self.addObserver(self, forKeyPath: "fetchPredicate", options: [.new], context: nil) | ||
} | ||
|
||
var page = 1 | ||
var total: Int { | ||
get { | ||
return try! (managedObjectContext?.count(for: defaultFetchRequest()))! | ||
} | ||
} | ||
|
||
override func fetch(with fetchRequest: NSFetchRequest<NSFetchRequestResult>?, merge: Bool) throws { | ||
fetchRequest?.fetchOffset = 0 | ||
fetchRequest?.fetchLimit = page * 30 | ||
try! super.fetch(with: fetchRequest, merge: merge) | ||
} | ||
func resetPage () { | ||
page = 1 | ||
self.fetch(nil) | ||
} | ||
@objc func fetchNextPage() { | ||
if (arrangedObjects as! [PasteItem]).count >= self.total { | ||
return | ||
} | ||
page += 1 | ||
self.fetch(nil) | ||
} | ||
|
||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { | ||
if (keyPath == "fetchPredicate") { | ||
self.resetPage() | ||
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "fetchPredicateChanged"), object: nil) | ||
} | ||
} | ||
} |
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