Skip to content

Instantly share code, notes, and snippets.

View Rsych's full-sized avatar
🙈
I may be slow to respond.

J. W. Kim Rsych

🙈
I may be slow to respond.
View GitHub Profile
@Rsych
Rsych / ObservableEnvironment.swift
Created November 4, 2021 06:58
Reading custom values from the environment with @EnvironmentObject
class User: ObservableObject {
@Published var name = "Ryan"
}
struct EditView: View {
@EnvironmentObject var user: User
var body: some View {
TextField("Name", text: $user.name)
}
@Rsych
Rsych / AlertControl.swift
Last active November 2, 2021 09:56
SwiftUI custom Textfield Alert
import SwiftUI
struct AlertControl: UIViewControllerRepresentable {
@Binding var textString: String
@Binding var textString2: String
@Binding var show: Bool
@Rsych
Rsych / FirstScreen.swift
Created November 1, 2021 14:35
First screen before auth completed
import SwiftUI
struct FirstScreen: View {
// MARK: - Properties
@State private var isUnlocked = false
// MARK: - Body
var body: some View {
Group {
if isUnlocked {
@Rsych
Rsych / AuthView.swift
Created November 1, 2021 14:34
Authentication View
import SwiftUI
import LocalAuthentication
struct AuthView: View {
@Binding var isUnlocked: Bool
@State private var showingAuthenticationError = false
@State private var authenticationErrorTitle = ""
@State private var authenticationErrorMessage = ""
@Rsych
Rsych / CodableMKPointAnnotation.swift
Created November 1, 2021 13:17
CodableMKPointAnnotation class
class CodableMKPointAnnotation: MKPointAnnotation, Codable {
enum CodingKeys: CodingKey {
case title, subtitle, latitude, longitude
}
override init() {
super.init()
}
@Rsych
Rsych / fetchNearbyPlacesFunc.swift
Last active November 1, 2021 12:29
Function that fetch data from wikipedia api
func fetchNearbyPlaces() {
let urlString = "https://en.wikipedia.org/w/api.php?ggscoord=\(placemark.coordinate.latitude)%7C\(placemark.coordinate.longitude)&action=query&prop=coordinates%7Cpageimages%7Cpageterms&colimit=50&piprop=thumbnail&pithumbsize=500&pilimit=50&wbptterms=description&generator=geosearch&ggsradius=10000&ggslimit=50&format=json"
guard let url = URL(string: urlString) else {
print("Bad URL: \(urlString)")
return
}
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
let decoder = JSONDecoder()
@Rsych
Rsych / QueryWiki.swift
Last active November 1, 2021 12:27
Query nearby locations from Wikipedia
import Foundation
struct QueryWiki: Codable {
let query: Query
}
struct Query: Codable {
let pages: [Int: Page]
}
@Rsych
Rsych / MapViewContent.swift
Created November 1, 2021 08:34
MapView with EditButton, EditView and annotations
import SwiftUI
import MapKit
struct ContentView: View {
// MARK: - Properties
@State private var centerCoordinate = CLLocationCoordinate2D()
@State private var locations = [MKPointAnnotation]()
@State private var selectedPlace: MKPointAnnotation?
@State private var showingPlaceDetails = false
@Rsych
Rsych / EditView.swift
Created November 1, 2021 08:30
Map Edit View in Swift
import MapKit
import SwiftUI
struct EditView: View {
// MARK: - Properties
@Environment(\.presentationMode) var presentationMode
@ObservedObject var placemark: MKPointAnnotation
// MARK: - Body
@Rsych
Rsych / MKPointAnnotation-ObservableObject.swift
Created November 1, 2021 08:09
MKPointAnnotation to ObservableObject
import MapKit
extension MKPointAnnotation: ObservableObject {
public var wrappedTitle: String {
get {
self.title ?? "Unknown value"
}
set {
title = newValue