Skip to content

Commit

Permalink
switched to popup view
Browse files Browse the repository at this point in the history
  • Loading branch information
danyberlin committed Jun 13, 2020
1 parent 3a437b8 commit bc0c042
Show file tree
Hide file tree
Showing 44 changed files with 819 additions and 87 deletions.
12 changes: 8 additions & 4 deletions MBweather.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

/* Begin PBXBuildFile section */
3B3CD9732491283B00B8E8FA /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B3CD9722491283B00B8E8FA /* AppDelegate.swift */; };
3B3CD9752491283B00B8E8FA /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B3CD9742491283B00B8E8FA /* ViewController.swift */; };
3B3CD9782491283B00B8E8FA /* MBweather.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 3B3CD9762491283B00B8E8FA /* MBweather.xcdatamodeld */; };
3B3CD97A2491283D00B8E8FA /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3B3CD9792491283D00B8E8FA /* Assets.xcassets */; };
3B3CD97D2491283D00B8E8FA /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 3B3CD97B2491283D00B8E8FA /* Main.storyboard */; };
3B3CD9892491283D00B8E8FA /* MBweatherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B3CD9882491283D00B8E8FA /* MBweatherTests.swift */; };
3B3CD9942491283D00B8E8FA /* MBweatherUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B3CD9932491283D00B8E8FA /* MBweatherUITests.swift */; };
3BB0FF952492E06C00AA0999 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB0FF942492E06C00AA0999 /* ViewController.swift */; };
3BB0FF972492E50400AA0999 /* EventMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BB0FF962492E50400AA0999 /* EventMonitor.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -36,7 +37,6 @@
/* Begin PBXFileReference section */
3B3CD96F2491283B00B8E8FA /* MBweather.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MBweather.app; sourceTree = BUILT_PRODUCTS_DIR; };
3B3CD9722491283B00B8E8FA /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
3B3CD9742491283B00B8E8FA /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
3B3CD9772491283B00B8E8FA /* MBweather.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = MBweather.xcdatamodel; sourceTree = "<group>"; };
3B3CD9792491283D00B8E8FA /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
3B3CD97C2491283D00B8E8FA /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
Expand All @@ -48,6 +48,8 @@
3B3CD98F2491283D00B8E8FA /* MBweatherUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MBweatherUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3B3CD9932491283D00B8E8FA /* MBweatherUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MBweatherUITests.swift; sourceTree = "<group>"; };
3B3CD9952491283D00B8E8FA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3BB0FF942492E06C00AA0999 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
3BB0FF962492E50400AA0999 /* EventMonitor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventMonitor.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -99,7 +101,8 @@
isa = PBXGroup;
children = (
3B3CD9722491283B00B8E8FA /* AppDelegate.swift */,
3B3CD9742491283B00B8E8FA /* ViewController.swift */,
3BB0FF962492E50400AA0999 /* EventMonitor.swift */,
3BB0FF942492E06C00AA0999 /* ViewController.swift */,
3B3CD9792491283D00B8E8FA /* Assets.xcassets */,
3B3CD97B2491283D00B8E8FA /* Main.storyboard */,
3B3CD97E2491283D00B8E8FA /* Info.plist */,
Expand Down Expand Up @@ -257,9 +260,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3B3CD9752491283B00B8E8FA /* ViewController.swift in Sources */,
3BB0FF952492E06C00AA0999 /* ViewController.swift in Sources */,
3B3CD9732491283B00B8E8FA /* AppDelegate.swift in Sources */,
3B3CD9782491283B00B8E8FA /* MBweather.xcdatamodeld in Sources */,
3BB0FF972492E50400AA0999 /* EventMonitor.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "F84223C5-5706-46A2-B578-068137C2A034"
type = "1"
version = "2.0">
</Bucket>
202 changes: 133 additions & 69 deletions MBweather/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,148 @@ import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
// let viewDelegate : ViewController? = NSApplication.shared.delegate as? ViewController
let viewDelegate = ViewController()
var weatherTimer : Timer?
var currentWeather = -99.9
let menu = NSMenu()
// let menu = NSMenu()
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

let popover = NSPopover()
var eventMonitor: EventMonitor?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
getWeather()
if let button = statusItem.button {
button.title = String(currentWeather) + ""
button.action = #selector(togglePopover(_:))
}
constuctMenu()
popover.contentViewController = ViewController.freshController()
popover.animates = false

// constuctMenu()
weatherTimer = Timer.scheduledTimer(withTimeInterval: 60*30, repeats: true) { time in
self.getWeather()
}
eventMonitor = EventMonitor(mask: [.leftMouseDown, .rightMouseDown]) { [weak self] event in
if let strongSelf = self, strongSelf.popover.isShown {
strongSelf.closePopover(sender: event)
}
}
}
@objc func togglePopover(_ sender: Any?) {
if popover.isShown {
closePopover(sender: sender)
} else {
showPopover(sender: sender)
}
}

func showPopover(sender: Any?) {
if let button = statusItem.button {
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
eventMonitor?.start()
}

func closePopover(sender: Any?) {
popover.performClose(sender)
eventMonitor?.stop()
}

@objc func updateWeather(_ sender: Any?) {
getWeather()
}

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

func getWeather(){
let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?id=2950157&APPID=386371523446a5a1ef1272512c75f28b")

public struct Response: Decodable { // or Decodable
var main : Main
var weather : [Weather]
var base: String
var visibility : Int
var cod: Int
var coord: Coord
var sys: Sys
var clouds: Clouds
var name: String
init() {
main = Main()
weather = [Weather()]
base = "response_base"
cod = 0
coord = Coord()
sys = Sys()
clouds = Clouds()
name = "response_name"
visibility = 0
}
}
struct Clouds: Decodable {
var all: Int
init() {
all = 0
}
}
struct Coord: Decodable {
var lat: Double
var lon: Double
init() {
lat = 0
lon = 0
}
}
struct Weather: Decodable {
var id: Int
var main: String
var description: String
var icon: String
init() {
id = 0
main = "weather_main"
description = "weather_description"
icon = "weather_icon"
}
}
struct Sys: Decodable {
var country: String
var id: Int
var sunrise: UInt64
var sunset: UInt64
var type: Int
init() {
country = "sys_country"
id = 0
sunrise = 0
sunset = 0
type = 0
}
}
struct Main: Decodable {
var humidity: Int
var pressure: Int
var temp: Double
var temp_max: Double
var temp_min: Double
var feels_like : Double
init() {
humidity = 0
pressure = 0
temp = 0
temp_max = 0
temp_min = 0
feels_like = 0
}
}
public var fullResponse = Response()
public var date = Date()
public func getWeather(){
let urlPart1 = "https://api.openweathermap.org/data/2.5/weather?id="
let urlPartCity = "2950157"
let urlPart3 = "&APPID="
let urlPartApiKey = "386371523446a5a1ef1272512c75f28b"
let urlComplete = urlPart1 + urlPartCity + urlPart3 + urlPartApiKey
let url = URL(string: urlComplete)
guard let requestUrl = url else { fatalError() }
// Create URL Request
var request = URLRequest(url: requestUrl)
Expand All @@ -54,71 +169,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
print("Response HTTP Status code: \(response.statusCode)")
}
// Convert HTTP Response Data to JSON
struct Response: Decodable { // or Decodable
let main : Main
let weather : [Weather]
let base: String
let cod: Int
let coord: Coord
let sys: Sys
let clouds: Clouds

}
struct Clouds: Decodable {
let all: Int
}
struct Coord: Decodable {
let lat: Double
let lon: Double
}
struct Weather: Decodable {
// let id: Int
// let main: String
let description: String
let icon: String
}
struct Sys: Decodable {
let country: String
let id: Int
let sunrise: UInt64
let sunset: UInt64
let type: Int
}
struct Main: Decodable {
// let humidity: Int
// let pressure: Int
let temp: Double
// let tempMax: Int
// let tempMin: Int
// private enum CodingKeys: String, CodingKey {
// case humidity, pressure, temp, tempMax = "temp_max", tempMin = "temp_min"
// }
}
if let data = data {
do {

let res = try JSONDecoder().decode(Response.self, from: data)
self.currentWeather = res.main.temp
print("Main: \(res.main)")
print("Weather: \(res.weather[0])")
print("Base: \(res.base)")
print("Cod: \(res.cod)")
print("Coord: \(res.coord)")
print("Sys: \(res.sys)")
print("Clouds: \(res.clouds)")
self.fullResponse = try JSONDecoder().decode(Response.self, from: data)
print("Main: \(self.fullResponse)")

DispatchQueue.main.async {
self.statusItem.button?.title = String(format: " %.1f",(self.currentWeather - 273.15)) + ""
self.statusItem.button?.title = String(format: " %.1f",(self.fullResponse.main.temp - 273.15)) + ""
}
} catch let error {
print("Error! : \(error)")
}
}
let date = Date()
let dateFormater = DateFormatter()
dateFormater.dateFormat = "HH:mm"
self.menu.item(at: 0)?.title = "Refresh (Last: " + dateFormater.string(from: date) + ")"
}
date = Date()
task.resume()
}
// MARK: - Core Data stack
Expand Down Expand Up @@ -217,12 +281,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// If we got here, it is time to quit.
return .terminateNow
}
func constuctMenu() {
menu.addItem(NSMenuItem(title: "Refresh", action: #selector(AppDelegate.updateWeather(_:)), keyEquivalent: ""))
menu.addItem(NSMenuItem.separator())
menu.addItem(NSMenuItem(title: "Quit MBweather", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))
statusItem.menu = menu
}
// func constuctMenu() {
//
// menu.addItem(NSMenuItem(title: "Refresh", action: #selector(AppDelegate.updateWeather(_:)), keyEquivalent: ""))
// menu.addItem(NSMenuItem.separator())
// menu.addItem(NSMenuItem(title: "Quit MBweather", action: #selector(NSApplication.terminate(_:)), keyEquivalent: ""))
// statusItem.menu = menu
// }
}

Binary file added MBweather/Assets.xcassets/.DS_Store
Binary file not shown.
21 changes: 21 additions & 0 deletions MBweather/Assets.xcassets/01d.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "[email protected]",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions MBweather/Assets.xcassets/01n.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "Image.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file added MBweather/Assets.xcassets/01n.imageset/Image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions MBweather/Assets.xcassets/02d.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "Image.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file added MBweather/Assets.xcassets/02d.imageset/Image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit bc0c042

Please sign in to comment.