Skip to content

Commit

Permalink
Merge pull request #8 from qwertyyb/develop
Browse files Browse the repository at this point in the history
feat: add lazy load for list
  • Loading branch information
qwertyyb authored Oct 21, 2019
2 parents f023c34 + 0740822 commit a296604
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 14 deletions.
8 changes: 8 additions & 0 deletions YPaste.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
4596AE712322B5CB00F63EBF /* MainWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4596AE702322B5CB00F63EBF /* MainWindowController.swift */; };
4596AE732323CF5800F63EBF /* SummaryTransformer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4596AE722323CF5800F63EBF /* SummaryTransformer.swift */; };
67136E302359E04F00EFB448 /* Preferences.xib in Resources */ = {isa = PBXBuildFile; fileRef = 67136E322359E04F00EFB448 /* Preferences.xib */; };
67904080235D8C1D00B0C74E /* PasteItemsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6790407F235D8C1D00B0C74E /* PasteItemsController.swift */; };
67C3262C2359D86300248CDE /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67C3262B2359D86300248CDE /* PreferencesWindowController.swift */; };
67C640E2235DE41D00649FD3 /* ScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67C640E1235DE41D00649FD3 /* ScrollView.swift */; };
67CFA4872351F4FB0058CF29 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 67CFA4862351F4FB0058CF29 /* README.md */; };
89E4606D59274DC83FCCFEB8 /* libPods-YPasteTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = FB920036F40D7A752C57AFCF /* libPods-YPasteTests.a */; };
90659D2688F1F0D071B53FBE /* libPods-YPaste.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5798607827522D99BA309E4B /* libPods-YPaste.a */; };
Expand Down Expand Up @@ -51,7 +53,9 @@
5798607827522D99BA309E4B /* libPods-YPaste.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-YPaste.a"; sourceTree = BUILT_PRODUCTS_DIR; };
67136E332359E05E00EFB448 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/Preferences.xib; sourceTree = "<group>"; };
67136E372359E66C00EFB448 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Preferences.strings"; sourceTree = "<group>"; };
6790407F235D8C1D00B0C74E /* PasteItemsController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasteItemsController.swift; sourceTree = "<group>"; };
67C3262B2359D86300248CDE /* PreferencesWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = "<group>"; };
67C640E1235DE41D00649FD3 /* ScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ScrollView.swift; path = MainWindow/ScrollView.swift; sourceTree = "<group>"; };
67CFA4862351F4FB0058CF29 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
67F313B62355F66B008BAEB7 /* zh-Hans */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-Hans"; path = "zh-Hans.lproj/Main.strings"; sourceTree = "<group>"; };
8D203870767222ECB87119A1 /* Pods-YPaste.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-YPaste.release.xcconfig"; path = "Target Support Files/Pods-YPaste/Pods-YPaste.release.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -128,6 +132,8 @@
4596AE722323CF5800F63EBF /* SummaryTransformer.swift */,
67136E322359E04F00EFB448 /* Preferences.xib */,
67C3262B2359D86300248CDE /* PreferencesWindowController.swift */,
6790407F235D8C1D00B0C74E /* PasteItemsController.swift */,
67C640E1235DE41D00649FD3 /* ScrollView.swift */,
);
path = YPaste;
sourceTree = "<group>";
Expand Down Expand Up @@ -345,13 +351,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
67904080235D8C1D00B0C74E /* PasteItemsController.swift in Sources */,
4596AE4D232025A700F63EBF /* YPaste.xcdatamodeld in Sources */,
4596AE6123216A8500F63EBF /* YPaste.swift in Sources */,
4596AE732323CF5800F63EBF /* SummaryTransformer.swift in Sources */,
4596AE4A232025A700F63EBF /* ViewController.swift in Sources */,
4596AE48232025A700F63EBF /* AppDelegate.swift in Sources */,
67C3262C2359D86300248CDE /* PreferencesWindowController.swift in Sources */,
4596AE712322B5CB00F63EBF /* MainWindowController.swift in Sources */,
67C640E2235DE41D00649FD3 /* ScrollView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
6 changes: 3 additions & 3 deletions YPaste/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</searchFieldCell>
<connections>
<binding destination="YWq-Kq-AiD" name="predicate" keyPath="filterPredicate" id="wuC-PI-5el">
<binding destination="YWq-Kq-AiD" name="predicate" keyPath="fetchPredicate" id="oEg-4y-qRo">
<dictionary key="options">
<string key="NSDisplayName">predicate</string>
<string key="NSPredicateFormat">self.value contains[cd] $value</string>
Expand All @@ -107,7 +107,7 @@
<outlet property="nextKeyView" destination="R8r-dv-iTb" id="Scd-zc-cmO"/>
</connections>
</searchField>
<scrollView fixedFrame="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="R8r-dv-iTb">
<scrollView fixedFrame="YES" autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" translatesAutoresizingMaskIntoConstraints="NO" id="R8r-dv-iTb" customClass="ScrollView" customModule="YPaste" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="480" height="250"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<clipView key="contentView" ambiguous="YES" drawsBackground="NO" id="BQc-S7-5MY">
Expand Down Expand Up @@ -237,7 +237,7 @@
</connections>
</viewController>
<customObject id="rPt-NT-nkU" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<arrayController mode="entity" entityName="PasteItem" usesLazyFetching="YES" fetchPredicateFormat="" automaticallyPreparesContent="YES" automaticallyRearrangesObjects="YES" id="YWq-Kq-AiD">
<arrayController mode="entity" entityName="PasteItem" usesLazyFetching="YES" fetchPredicateFormat="" automaticallyPreparesContent="YES" automaticallyRearrangesObjects="YES" id="YWq-Kq-AiD" customClass="PasteItemsController" customModule="YPaste" customModuleProvider="target">
<connections>
<binding destination="XfG-lQ-9wD" name="managedObjectContext" keyPath="managedObjectContext" id="Ujc-1J-jRW"/>
<binding destination="XfG-lQ-9wD" name="sortDescriptors" keyPath="sortByUpdateTime" id="zAk-2D-OPt"/>
Expand Down
2 changes: 1 addition & 1 deletion YPaste/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>Explosion v0.1.8</string>
<key>CFBundleVersion</key>
<string>100331</string>
<string>100356</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>
Expand Down
33 changes: 33 additions & 0 deletions YPaste/MainWindow/ScrollView.swift
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)
}
}



}
4 changes: 2 additions & 2 deletions YPaste/MainWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MainWindowController: NSWindowController, NSWindowDelegate {
}
print("init")
appDelegate.app.onHistoryChange = {
(self.contentViewController as! ViewController).arrayController.fetch(self)
let arrayController = (self.contentViewController as! ViewController).arrayController
arrayController?.resetPage()
}
}

Expand All @@ -37,7 +38,6 @@ class MainWindowController: NSWindowController, NSWindowDelegate {

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "arrangedObjects" {
print("select index 0")
(self.contentViewController as! ViewController).arrayController.setSelectionIndex(0)
}

Expand Down
52 changes: 52 additions & 0 deletions YPaste/PasteItemsController.swift
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)
}
}
}
16 changes: 8 additions & 8 deletions YPaste/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Cocoa
import HotKey

class ViewController: NSViewController, NSTableViewDelegate {
class ViewController: NSViewController {

var selectedPasteItem: PasteItem?
@objc let managedObjectContext: NSManagedObjectContext
Expand All @@ -19,6 +19,9 @@ class ViewController: NSViewController, NSTableViewDelegate {
self.managedObjectContext = (NSApp.delegate as! AppDelegate).persistentContainer.viewContext
super.init(coder: coder)
ValueTransformer.setValueTransformer(SummaryTransformer(), forName: .summaryTransformerName)
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "fetchPredicateChanged"), object: nil, queue: nil) { (notification) in
self.tableView.scrollRowToVisible(0)
}
}

@objc let sortByUpdateTime = [NSSortDescriptor(key: "updated_at", ascending: false)]
Expand All @@ -28,19 +31,16 @@ class ViewController: NSViewController, NSTableViewDelegate {
}

@IBOutlet weak var searchField: NSSearchField!
@IBOutlet var arrayController: NSArrayController!

override func viewDidLoad() {
super.viewDidLoad()
}
@IBOutlet var arrayController: PasteItemsController!

override func viewWillAppear() {
arrayController.resetPage()
tableView.scrollRowToVisible(0)
arrayController.setSelectionIndex(0)
}

override func viewDidDisappear() {
arrayController.filterPredicate = nil
arrayController.fetchPredicate = nil
}

@IBOutlet weak var tableView: NSTableView!
Expand All @@ -57,8 +57,8 @@ class ViewController: NSViewController, NSTableViewDelegate {
app().app.paste(pasteItem: (pasteItems?.first)!)
}
if key == Key.downArrow {

}
}

}

0 comments on commit a296604

Please sign in to comment.