Skip to content

Commit

Permalink
init and coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsche committed Aug 28, 2023
1 parent b87ec65 commit 4a97496
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 25 deletions.
37 changes: 37 additions & 0 deletions LocationManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// LocationManager.swift
// Weather App
//
// Created by John Suman on 2023/08/28.
//

import Foundation
import CoreLocation

class LocationManager: NSObject, ObservableObject, CLLocationManagerDelegate {
let manager = CLLocationManager()

@Published var location: CLLocationCoordinate2D?
@Published var isLoading = false

override init() {
super.init()
manager.delegate = self
}

func requestLocation() {
isLoading = true
manager.requestLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
location = locations.first?.coordinate
isLoading = false
}

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error getting location", error)
isLoading = false
}

}
32 changes: 31 additions & 1 deletion Weather App.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
55BF58BF2A9C87B300B90043 /* Weather_AppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55BF58BE2A9C87B300B90043 /* Weather_AppTests.swift */; };
55BF58C92A9C87B300B90043 /* Weather_AppUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55BF58C82A9C87B300B90043 /* Weather_AppUITests.swift */; };
55BF58CB2A9C87B300B90043 /* Weather_AppUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55BF58CA2A9C87B300B90043 /* Weather_AppUITestsLaunchTests.swift */; };
55BF58D92A9C899A00B90043 /* LocationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55BF58D82A9C899A00B90043 /* LocationManager.swift */; };
55BF58DC2A9C8C4500B90043 /* WelcomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55BF58DB2A9C8C4500B90043 /* WelcomeView.swift */; };
55BF58DE2A9C912E00B90043 /* LoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55BF58DD2A9C912E00B90043 /* LoadingView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -44,6 +47,9 @@
55BF58C42A9C87B300B90043 /* Weather AppUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Weather AppUITests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
55BF58C82A9C87B300B90043 /* Weather_AppUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weather_AppUITests.swift; sourceTree = "<group>"; };
55BF58CA2A9C87B300B90043 /* Weather_AppUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weather_AppUITestsLaunchTests.swift; sourceTree = "<group>"; };
55BF58D82A9C899A00B90043 /* LocationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationManager.swift; sourceTree = SOURCE_ROOT; };
55BF58DB2A9C8C4500B90043 /* WelcomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WelcomeView.swift; sourceTree = "<group>"; };
55BF58DD2A9C912E00B90043 /* LoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadingView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -94,8 +100,9 @@
55BF58AC2A9C87B100B90043 /* Weather App */ = {
isa = PBXGroup;
children = (
55BF58DA2A9C8BED00B90043 /* Views */,
55BF58D72A9C897900B90043 /* Managers */,
55BF58AD2A9C87B100B90043 /* Weather_AppApp.swift */,
55BF58AF2A9C87B100B90043 /* ContentView.swift */,
55BF58B12A9C87B300B90043 /* Assets.xcassets */,
55BF58B32A9C87B300B90043 /* Preview Content */,
);
Expand Down Expand Up @@ -127,6 +134,24 @@
path = "Weather AppUITests";
sourceTree = "<group>";
};
55BF58D72A9C897900B90043 /* Managers */ = {
isa = PBXGroup;
children = (
55BF58D82A9C899A00B90043 /* LocationManager.swift */,
);
path = Managers;
sourceTree = "<group>";
};
55BF58DA2A9C8BED00B90043 /* Views */ = {
isa = PBXGroup;
children = (
55BF58AF2A9C87B100B90043 /* ContentView.swift */,
55BF58DB2A9C8C4500B90043 /* WelcomeView.swift */,
55BF58DD2A9C912E00B90043 /* LoadingView.swift */,
);
path = Views;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -259,6 +284,9 @@
files = (
55BF58B02A9C87B100B90043 /* ContentView.swift in Sources */,
55BF58AE2A9C87B100B90043 /* Weather_AppApp.swift in Sources */,
55BF58D92A9C899A00B90043 /* LocationManager.swift in Sources */,
55BF58DE2A9C912E00B90043 /* LoadingView.swift in Sources */,
55BF58DC2A9C8C4500B90043 /* WelcomeView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -430,6 +458,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down Expand Up @@ -459,6 +488,7 @@
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight";
IPHONEOS_DEPLOYMENT_TARGET = 16.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"images" : [
{
"filename" : "1024x1024.png",
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
Expand Down
24 changes: 0 additions & 24 deletions Weather App/ContentView.swift

This file was deleted.

37 changes: 37 additions & 0 deletions Weather App/Views/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// ContentView.swift
// Weather App
//
// Created by John Suman on 2023/08/28.
//

import SwiftUI

struct ContentView: View {
@StateObject var locationManager = LocationManager()

var body: some View {
VStack {

if let location = locationManager.location {
Text("Your coordinates are: \(location.longitude), \(location.latitude)")
} else {
if locationManager.isLoading {
LoadingView()
} else {
WelcomeView()
.environmentObject(locationManager)
}
}


}
.background(/*@START_MENU_TOKEN@*//*@PLACEHOLDER=View@*/Color(hue: 0.673, saturation: 0.885, brightness: 0.441)/*@END_MENU_TOKEN@*/)
.preferredColorScheme(/*@START_MENU_TOKEN@*/.dark/*@END_MENU_TOKEN@*/)

}
}

#Preview {
ContentView()
}
20 changes: 20 additions & 0 deletions Weather App/Views/LoadingView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// LoadingView.swift
// Weather App
//
// Created by John Suman on 2023/08/28.
//

import SwiftUI

struct LoadingView: View {
var body: some View {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/, maxHeight: .infinity)
}
}

#Preview {
LoadingView()
}
39 changes: 39 additions & 0 deletions Weather App/Views/WelcomeView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// WelcomeView.swift
// Weather App
//
// Created by John Suman on 2023/08/28.
//

import SwiftUI
import CoreLocationUI

struct WelcomeView: View {
@EnvironmentObject var locationManager: LocationManager

var body: some View {
VStack {
VStack(spacing: 20) {
Text("Welcome to the Weather App")
.bold()
.font(.title)
Text("Please share your current location to get the weather in your spot")
.padding()
}
.multilineTextAlignment(.center)
.padding()

LocationButton(.shareCurrentLocation) {
locationManager.requestLocation()
}
.cornerRadius(30)
.symbolVariant(.fill)
.foregroundColor(.white)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

#Preview {
WelcomeView()
}

0 comments on commit 4a97496

Please sign in to comment.