Skip to content

Commit

Permalink
Add city name in location picker
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed May 24, 2022
1 parent aeb7b66 commit 5cee23b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 93 deletions.
36 changes: 18 additions & 18 deletions Example/Shared/ComponentsList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,36 @@ struct ComponentsList: View {
Page(name: "Avatar", page: AnyView(AvatarDemo())),
Page(name: "GridSelect", page: AnyView(GridSelectDemo())),
Page(name: "TextField", page: AnyView(TextFieldDemo())),
//Page(name: "Icons", page: AnyView(IconsDemo())),
// Page(name: "Icons", page: AnyView(IconsDemo())),
Page(name: "Row", page: AnyView(RowDemo())),
Page(name: "SegmentedControl", page: AnyView(SegmentedControlDemo())),
Page(name: "Select", page: AnyView(SelectDemo())),
Page(name: "Surface", page: AnyView(SurfaceDemo())),
//Page(name: "Page", page: AnyView(PageDemo())),
// Page(name: "Page", page: AnyView(PageDemo())),
]

var body: some View {
#if os(iOS)
PageView("Example") {
ForEach(pages) { page in
HStack {
NavigationLink(page.name, destination: page.page)
.fontStyle(.button, color: .onSurfaceHighEmphasis)
PageView("Example") {
ForEach(pages) { page in
HStack {
NavigationLink(page.name, destination: page.page)
.fontStyle(.button, color: .onSurfaceHighEmphasis)

Spacer()
Spacer()
}
.padding()
}
.padding()
}
}
.leadingBar {
BarButton(type: .secondary("Settings", action: { isShowSetting.toggle() }))
}
.navigationable()
.sheet(isPresented: $isShowSetting) {
AppearanceSettingView()
}
.leadingBar {
BarButton(type: .secondary("Settings", action: { isShowSetting.toggle() }))
}
.navigationable()
.sheet(isPresented: $isShowSetting) {
AppearanceSettingView()
}
#else
Text("Support will be in the future")
Text("Support will be in the future")
#endif
}
}
Expand Down
40 changes: 20 additions & 20 deletions Example/Shared/DemoPages/IconsDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ import OversizeUI
import SwiftUI

#if os(iOS)
struct IconsDemo: View {
let grid = [GridItem(),
GridItem(),
GridItem(),
GridItem(),
GridItem(),
GridItem()]
var body: some View {
PageView("Icons") {
LazyVGrid(columns: grid) {
ForEach(IconsNames.allCases, id: \.self) { icon in
Icon(icon)
.padding(.vertical)
struct IconsDemo: View {
let grid = [GridItem(),
GridItem(),
GridItem(),
GridItem(),
GridItem(),
GridItem()]
var body: some View {
PageView("Icons") {
LazyVGrid(columns: grid) {
ForEach(IconsNames.allCases, id: \.self) { icon in
Icon(icon)
.padding(.vertical)
}
}
.padding()
}
.padding()
.modalable()
}
.modalable()
}
}

struct IconsDemo_Previews: PreviewProvider {
static var previews: some View {
IconsDemo()
struct IconsDemo_Previews: PreviewProvider {
static var previews: some View {
IconsDemo()
}
}
}
#endif
76 changes: 38 additions & 38 deletions Example/Shared/DemoPages/PageDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,52 @@ import OversizeUI
import SwiftUI

#if os(iOS)
struct PageDemo: View {
@Environment(\.screenSize) var screenSize
@State var isShowModal = false
struct PageDemo: View {
@Environment(\.screenSize) var screenSize
@State var isShowModal = false

var body: some View {
PageView("Page view") {
VStack {
Button {
isShowModal = true
} label: {
Text("Show modal")
}
var body: some View {
PageView("Page view") {
VStack {
Button {
isShowModal = true
} label: {
Text("Show modal")
}

ForEach((1 ... 100).reversed(), id: \.self) { item in
ForEach((1 ... 100).reversed(), id: \.self) { item in

HStack {
Spacer()
Text("Item number \(item)")
Spacer()
HStack {
Spacer()
Text("Item number \(item)")
Spacer()
}
.padding()
}
.padding()
}
}
}
.leadingBar {
BarButton(type: .back)
}
.trailingBar {
BarButton(type: .primary("Save", action: {}))
}
.topToolbar {
Text("Top")
}
.bottomToolbar {
Text("Bottom")
.padding(.bottom, screenSize.safeAreaBottom)
}
.sheet(isPresented: $isShowModal) {
IconsDemo()
.leadingBar {
BarButton(type: .back)
}
.trailingBar {
BarButton(type: .primary("Save", action: {}))
}
.topToolbar {
Text("Top")
}
.bottomToolbar {
Text("Bottom")
.padding(.bottom, screenSize.safeAreaBottom)
}
.sheet(isPresented: $isShowModal) {
IconsDemo()
}
}
}
}

struct PageDemo_Previews: PreviewProvider {
static var previews: some View {
PageDemo()
struct PageDemo_Previews: PreviewProvider {
static var previews: some View {
PageDemo()
}
}
}
#endif
2 changes: 1 addition & 1 deletion Example/Shared/DemoPages/TextFieldDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct TextFieldDemo: View {

}.padding()
}

.navigationTitle("Text fields")
}
}
Expand Down
62 changes: 55 additions & 7 deletions Sources/OversizeUI/Controls/LocationPicker/LocationPicker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@
// LocationPicker.swift
//

import CoreLocation
import MapKit
import SwiftUI

#if os(iOS)
public struct LocationPicker: View {
@Environment(\.theme) private var theme: ThemeSettings

@Binding private var coordinates: CLLocationCoordinate2D
@Binding private var positionName: String?
private let label: String

private let saveButtonText: String?
@Binding var coordinates: CLLocationCoordinate2D
@State var offset = CGPoint(x: 0, y: 0)
@State private var offset = CGPoint(x: 0, y: 0)
@State private var showModal = false
@State private var isSelected = false

public init(label: String, coordinates: Binding<CLLocationCoordinate2D>, saveButtonText: String? = nil) {
public init(label: String, coordinates: Binding<CLLocationCoordinate2D>, positionName: Binding<String?>, saveButtonText: String? = nil) {
self.label = label
self.saveButtonText = saveButtonText
_coordinates = coordinates
_positionName = positionName
}

public var body: some View {
Expand All @@ -32,8 +36,11 @@ import SwiftUI
.fontStyle(.subtitle1, color: .onSurfaceHighEmphasis)
}
Spacer()
Text(String(coordinates.latitude))
Text(String(coordinates.longitude))

if let positionName = positionName {
Text(positionName)
.fontStyle(.subtitle2, color: .onSurfaceMediumEmphasis)
}

Icon(.chevronDown, color: .onSurfaceHighEmphasis)
}
Expand All @@ -51,10 +58,12 @@ import SwiftUI
: Color.surfaceSecondary, lineWidth: CGFloat(theme.borderSize))
)
)

.sheet(isPresented: $showModal) {
modal
}
.onChange(of: coordinates) {
updateCityName(coordinate: $0)
}
}

public var modal: some View {
Expand All @@ -66,8 +75,18 @@ import SwiftUI
Spacer()

MaterialSurface {
Text("\(coordinates.latitude), \(coordinates.longitude)")
VStack(alignment: .center, spacing: .xxSmall) {
if let positionName = positionName {
Text(positionName)
.fontStyle(.title3, color: .onSurfaceHighEmphasis)
}
Text("\(coordinates.latitude), \(coordinates.longitude)")
.fontStyle(.subtitle2, color: .onSurfaceMediumEmphasis)
}
}
.controlPadding(.small)
.controlRadius(.large)
.multilineTextAlignment(.center)

}.padding()
}
Expand All @@ -80,5 +99,34 @@ import SwiftUI
}))
}
}

private func updateCityName(coordinate: CLLocationCoordinate2D) {
let loc = CLLocation(
latitude: coordinate.latitude,
longitude: coordinate.longitude
)

let geocoder = CLGeocoder()

geocoder.reverseGeocodeLocation(loc) { placemarks, error in
if error != nil { return }
if let firstLocation = placemarks?.first as? CLPlacemark {
if let locality = firstLocation.locality {
self.positionName = locality

} else if let subLocality = firstLocation.subLocality {
self.positionName = subLocality
} else {
self.positionName = firstLocation.name
}
}
}
}
}

extension CLLocationCoordinate2D: Equatable {}

public func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool {
lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}
#endif
3 changes: 3 additions & 0 deletions Sources/OversizeUI/Controls/Row/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public enum RowLeadingType {
case image(_ image: Image)
case systemImage(_ imageName: String)
case avatar(_ avatar: AvatarView)
case view(_ view: AnyView)
}

public struct Row: View {
Expand Down Expand Up @@ -169,6 +170,8 @@ public struct Row: View {
.font(.system(size: 24))
.frame(width: 24, height: 24, alignment: .center)
.padding(.trailing, Constants.spacingIconAndText)
case let .view(view):
view
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,30 @@
// ControlSizeEnvironment.swift
//

//import SwiftUI
// import SwiftUI
//
//public enum ControlSize {
// public enum ControlSize {
// case mini
// case small
// case regular
// @available(macOS 11.0, *)
// case large
//}
// }
//
//private struct ControlSizeKey: EnvironmentKey {
// private struct ControlSizeKey: EnvironmentKey {
// public static var defaultValue: ControlSize = .regular
//}
// }
//
//public extension EnvironmentValues {
// public extension EnvironmentValues {
// var controlSize: ControlSize {
// get { self[ControlSizeKey.self] }
// set { self[ControlSizeKey.self] = newValue }
// }
//}
// }
//
//public extension View {
// public extension View {
// @_disfavoredOverload
// @inlinable func controlSize(_ controlSize: ControlSize) -> some View {
// environment(\.controlSize, controlSize)
// }
//}
// }

0 comments on commit 5cee23b

Please sign in to comment.