Skip to content

Instantly share code, notes, and snippets.

View navsing's full-sized avatar
🏠
Working from home

Navdeep Singh navsing

🏠
Working from home
View GitHub Profile
const path = require('path');
const fs = require('fs');
const basePathToData = path.join(__dirname, 'path-to-your-json-data-file');
const getJsonData = function (basePathToData, filename) {
var filename = path.join(basePathToData, filename);
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
};
[
{
"id": 1,
"name": "sample-name-1"
},
{
"id": 2,
"name": "sample-name-2"
},
]
func loadData() {
guard let url = URL(string: "https://jsonplaceholder.typicode.com/todos") else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data {
if let response = try? JSONDecoder().decode([TaskEntry].self, from: data) {
import SwiftUI
struct ContentView: View {
@State var results = [TaskEntry]()
var body: some View {
List(results, id: \.id) { item in
VStack(alignment: .leading) {
Text(item.title)
import SwiftUI
class Favorites: ObservableObject {
private var tasks: Set<String>
let defaults = UserDefaults.standard
init() {
let decoder = JSONDecoder()
if let data = defaults.value(forKey: "Favorites") as? Data {
let taskData = try? decoder.decode(Set<String>.self, from: data)
ForEach(self.tasks, id: \.id) { task in
VStack {
Text(task.title)
Button(action: {
if self.favorites.contains(task) {
self.favorites.remove(task)
} else {
self.favorites.add(task)
}
}) {
func scheduleNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { success, error in
if success {
print("Success")
//To add badgeNumber
//UIApplication.shared.applicationIconBadgeNumber = badgeNumber (Integer Value)
} else if let error = error {
print(error.localizedDescription)
}
@navsing
navsing / AppStoreHome.swift
Last active August 6, 2023 09:10
Let’s recreate the iOS app store home screen in less than 5 minutes using SwiftUI and Swift Playgrounds on iPad
import SwiftUI
import PlaygroundSupport
struct Screen: View {
var body: some View {
ScrollView {
HStack {
VStack (alignment: .leading) {
Text("TUESDAY, MAY 12").foregroundColor(.secondary).bold().font(.footnote)
@navsing
navsing / breathe.swift
Created May 14, 2020 19:18
let’s recreate the breathe app in less than 3 minutes using SwiftUI and Swift Playgrounds on iPad
struct Breathe: View {
@State var scale = false
@State var rotate = false
var body: some View {
ZStack {
Group {
ZStack {
Circle().frame(width: 80, height: 80).foregroundColor(Color(UIColor.systemBlue)).offset(y: -42)
Circle().frame(width: 80, height: 80).foregroundColor(Color(UIColor.systemBlue)).offset(y: 42)
struct AppleIdButton: UIViewRepresentable {
func makeUIView(context: Context) -> ASAuthorizationAppleIDButton {
ASAuthorizationAppleIDButton()
}
func updateUIView(_ uiView: ASAuthorizationAppleIDButton, context: Context) {
}
}