Skip to content

Commit

Permalink
finish board UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrzzzz committed Jan 25, 2021
1 parent 713f35a commit 96ea668
Show file tree
Hide file tree
Showing 14 changed files with 973 additions and 779 deletions.
20 changes: 20 additions & 0 deletions EzFund.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
2DA8A76A7E8C1300DD2C7A8A /* Pods_EzFund.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7244A951C49F76904BE66EBB /* Pods_EzFund.framework */; };
D3B8D14625BDCBFF0070D1BC /* BoardViewController+DragDrop.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3B8D14525BDCBFF0070D1BC /* BoardViewController+DragDrop.swift */; };
D3E83B3325BA708A00CC3AE7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83B3225BA708A00CC3AE7 /* AppDelegate.swift */; };
D3E83B3725BA708C00CC3AE7 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3E83B3625BA708C00CC3AE7 /* Assets.xcassets */; };
D3E83B3A25BA708C00CC3AE7 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3E83B3925BA708C00CC3AE7 /* Preview Assets.xcassets */; };
Expand All @@ -18,11 +19,15 @@
D3E83B5625BAB8FF00CC3AE7 /* Storage.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83B5525BAB8FF00CC3AE7 /* Storage.swift */; };
D3E83B5A25BB378400CC3AE7 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83B5925BB378400CC3AE7 /* Extensions.swift */; };
D3E83B5D25BBF31600CC3AE7 /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83B5C25BBF31500CC3AE7 /* Logging.swift */; };
D3E83B9625BC6D0E00CC3AE7 /* SearchBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83B9525BC6D0E00CC3AE7 /* SearchBar.swift */; };
D3E83B9925BC730E00CC3AE7 /* BoardViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83B9825BC730E00CC3AE7 /* BoardViewController.swift */; };
D3E83BA325BD823E00CC3AE7 /* DataStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = D3E83BA225BD823E00CC3AE7 /* DataStruct.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
5140FC12E63944816A6DC60E /* Pods-EzFund.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EzFund.release.xcconfig"; path = "Target Support Files/Pods-EzFund/Pods-EzFund.release.xcconfig"; sourceTree = "<group>"; };
7244A951C49F76904BE66EBB /* Pods_EzFund.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_EzFund.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D3B8D14525BDCBFF0070D1BC /* BoardViewController+DragDrop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BoardViewController+DragDrop.swift"; sourceTree = "<group>"; };
D3E83B2F25BA708A00CC3AE7 /* EzFund.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = EzFund.app; sourceTree = BUILT_PRODUCTS_DIR; };
D3E83B3225BA708A00CC3AE7 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
D3E83B3625BA708C00CC3AE7 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand All @@ -36,6 +41,9 @@
D3E83B5525BAB8FF00CC3AE7 /* Storage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Storage.swift; sourceTree = "<group>"; };
D3E83B5925BB378400CC3AE7 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
D3E83B5C25BBF31500CC3AE7 /* Logging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
D3E83B9525BC6D0E00CC3AE7 /* SearchBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchBar.swift; sourceTree = "<group>"; };
D3E83B9825BC730E00CC3AE7 /* BoardViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoardViewController.swift; sourceTree = "<group>"; };
D3E83BA225BD823E00CC3AE7 /* DataStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStruct.swift; sourceTree = "<group>"; };
D4344EDF342A40337B3F8F9C /* Pods-EzFund.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-EzFund.debug.xcconfig"; path = "Target Support Files/Pods-EzFund/Pods-EzFund.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -91,10 +99,14 @@
children = (
D3E83B3225BA708A00CC3AE7 /* AppDelegate.swift */,
D3E83B4925BA955800CC3AE7 /* PopoverViewController.swift */,
D3E83B9825BC730E00CC3AE7 /* BoardViewController.swift */,
D3B8D14525BDCBFF0070D1BC /* BoardViewController+DragDrop.swift */,
D3E83B5C25BBF31500CC3AE7 /* Logging.swift */,
D3E83B5525BAB8FF00CC3AE7 /* Storage.swift */,
D3E83BA225BD823E00CC3AE7 /* DataStruct.swift */,
D3E83B4C25BA9D5300CC3AE7 /* EventMonitor.swift */,
D3E83B4F25BAB59F00CC3AE7 /* ContentView.swift */,
D3E83B9525BC6D0E00CC3AE7 /* SearchBar.swift */,
D3E83B5925BB378400CC3AE7 /* Extensions.swift */,
D3E83B3625BA708C00CC3AE7 /* Assets.xcassets */,
D3E83B3B25BA708C00CC3AE7 /* Main.storyboard */,
Expand Down Expand Up @@ -229,11 +241,15 @@
files = (
D3E83B4D25BA9D5300CC3AE7 /* EventMonitor.swift in Sources */,
D3E83B5A25BB378400CC3AE7 /* Extensions.swift in Sources */,
D3B8D14625BDCBFF0070D1BC /* BoardViewController+DragDrop.swift in Sources */,
D3E83B5D25BBF31600CC3AE7 /* Logging.swift in Sources */,
D3E83B4A25BA955800CC3AE7 /* PopoverViewController.swift in Sources */,
D3E83B9625BC6D0E00CC3AE7 /* SearchBar.swift in Sources */,
D3E83B5025BAB59F00CC3AE7 /* ContentView.swift in Sources */,
D3E83BA325BD823E00CC3AE7 /* DataStruct.swift in Sources */,
D3E83B3325BA708A00CC3AE7 /* AppDelegate.swift in Sources */,
D3E83B5625BAB8FF00CC3AE7 /* Storage.swift in Sources */,
D3E83B9925BC730E00CC3AE7 /* BoardViewController.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -373,9 +389,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = EzFund/EzFund.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_ASSET_PATHS = "\"EzFund/Preview Content\"";
DEVELOPMENT_TEAM = 68G6J2P6XH;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = EzFund/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand All @@ -396,9 +414,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = EzFund/EzFund.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_ASSET_PATHS = "\"EzFund/Preview Content\"";
DEVELOPMENT_TEAM = 68G6J2P6XH;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = EzFund/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "F6122A22-5679-4861-B23C-CA626331D4E6"
shouldBeEnabled = "Yes"
uuid = "9FF887FB-8B22-463E-A885-E8BBF1B6E7DE"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "EzFund/PopoverViewController.swift"
filePath = "EzFund/AppDelegate.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "180"
endingLineNumber = "180"
landmarkName = "tableView(_:viewFor:row:)"
startingLineNumber = "40"
endingLineNumber = "40"
landmarkName = "initTimer()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "EB7C7751-4408-444E-8D44-F76017848EB6"
shouldBeEnabled = "No"
uuid = "0446DAF9-5B89-4E75-9DFA-9EDBA66CF84D"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "EzFund/Extensions.swift"
filePath = "EzFund/AppDelegate.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "84"
endingLineNumber = "84"
landmarkName = "hex(_:)"
startingLineNumber = "76"
endingLineNumber = "76"
landmarkName = "initTimer()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand Down
124 changes: 99 additions & 25 deletions EzFund/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,98 @@

import Cocoa
import SwiftUI
import Alamofire

@main
class AppDelegate: NSObject, NSApplicationDelegate {

// var window: NSWindow!
var statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)


var window: NSWindow!
var statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
@IBOutlet weak var menu: NSMenu!
let popover = NSPopover()
var eventMonitor: EventMonitor?

func applicationDidFinishLaunching(_ aNotification: Notification) {
initStatusItem()
initWindow()
initTimer()
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

private func initTimer() {
var times: Int = 0
let timer = Timer.scheduledTimer(withTimeInterval: 60, repeats: true) { (_) in
let fundData = Storage.retreive(LocalFileName.fund, from: .documents, as: LocalFund.self)?.fundData ?? []
guard let button = self.statusItem.button else { return }
if !fundData.isEmpty {
button.imagePosition = .imageLeft
let fund = fundData[times % fundData.count]
let url = URL(string: "http://fundgz.1234567.com.cn/js/\(fund[0]).js")!
AF.request(url).response { (response) in
// print(response.response?.statusCode)
switch response.result {
case .success(let data):
if let data = data {
let jsonStr = String(data: data, encoding: .utf8)
if var jsStr = jsonStr {
jsStr = jsStr.find("jsonpgz\\((.+)\\);")
let jsonData = jsStr.data(using: .utf8)
let fund = try? JSONDecoder().decode(Fund.self, from: jsonData ?? Data())
if let fund = fund {
button.attributedTitle = NSAttributedString(string: "\(fund.name) \(fund.gszzl)%", attributes: [NSAttributedString.Key.foregroundColor: ((Double(fund.gszzl) ?? 0) > 0 ? NSColor.hex(0xc21f30) : NSColor.hex(0x1a6840))])
}
} else {
button.attributedTitle = NSAttributedString(string: "\(fund[2]) 未知")
}
}
case .failure(let err):
SLogError("failed to get fund data.\n \(err)")
button.attributedTitle = .init(string: "")
button.imagePosition = .imageOnly
times = 0
}
}
times += 1
} else {
button.imagePosition = .imageLeft
let url = URL(string: "http://73.push2.eastmoney.com/api/qt/clist/get?pn=1&pz=21&po=1&np=1&fltt=2&invt=2&fields=f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f12,f13,f14,f15,f16,f17,f18,f20,f21,f23,f24,f25,f26,f22,f33,f11,f62,f128,f136,f115,f152,f124,f107&fs=i:1.000001,i:0.399001,i:0.399005,i:0.399006,i:1.000300")!
AF.request(url).response { response in
switch response.result {
case .success(let data):
if let data = data {
let market = try? JSONDecoder().decode(Market.self, from: data)
if let marketData = market?.data.diff {
let market = marketData[times % marketData.count]
let color: NSColor = market.f3 > 0 ? .hex(0xc21f30) : NSColor.hex(0x1a6840)
button.attributedTitle = NSAttributedString(string: String(format: "%@ %.2f%%", market.f14, market.f3),
attributes: [
NSAttributedString.Key.foregroundColor: color
])
} else {
button.attributedTitle = NSAttributedString(string: "未获取到市场数据")
}
}
case .failure(let err):
SLogError("failed to get Market Data.\n \(err)")
button.attributedTitle = .init(string: "")
button.imagePosition = .imageOnly
times = 0
}
}
times += 1
}
}

timer.fire()
}

private func initStatusItem() {
statusItem.menu = nil
menu.delegate = self

if let button = statusItem.button {
button.image = NSImage(named: "statusIcon")
button.action = #selector(mouseClickHandler)
Expand All @@ -34,37 +111,31 @@ class AppDelegate: NSObject, NSApplicationDelegate {
strongSelf.popover.performClose(event)
}
})
// Create the SwiftUI view that provides the window contents.
// let contentView = ContentView()
//
// // Create the window and set the content view.
// window = NSWindow(
// contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
// styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
// backing: .buffered, defer: false)
// window.isReleasedWhenClosed = false
// window.center()
// window.setFrameAutosaveName("Main Window")
// window.contentView = NSHostingView(rootView: contentView)
// window.makeKeyAndOrderFront(nil)
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application

private func initWindow() {
// for board
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 480, height: 400),
styleMask: [.titled, .closable, .miniaturizable, .fullSizeContentView],
backing: .buffered, defer: false)
window.center()
window.title = "控制台"
window.contentViewController = BoardViewController.freshController()
window.isReleasedWhenClosed = false
window.hidesOnDeactivate = true
}

@objc func mouseClickHandler(_ sender: Any?) {
if let event = NSApp.currentEvent {
if event.type == .leftMouseUp {
togglePopover(sender )
togglePopover(sender)
} else if event.type == .rightMouseUp {
statusItem.menu = menu
statusItem.button?.performClick(nil)
}
}
}


}
//MARK: - 左键菜单
extension AppDelegate {
Expand All @@ -91,10 +162,13 @@ extension AppDelegate {

//MARK: - 右键菜单
extension AppDelegate: NSMenuDelegate {

@IBAction func openBoard(_ sender: Any) {

NSApp.activate(ignoringOtherApps: true)
if let w = NSApp.windows.first(where: {$0 == window}) {
w.makeKeyAndOrderFront(self)
}
}

@IBAction func quitApp(_ sender: Any) {
NSApplication.shared.terminate(self)
}
Expand Down
Loading

0 comments on commit 96ea668

Please sign in to comment.