Skip to content

Instantly share code, notes, and snippets.

View nitin-agam's full-sized avatar
😎
Execution is the key 🔑

Nitin Aggarwal nitin-agam

😎
Execution is the key 🔑
View GitHub Profile
@nitin-agam
nitin-agam / Time.Swift
Created April 6, 2023 14:46
Here is an example to convert seconds in display time format.
import Foundation
func formatDuration(_ durationInSeconds: Int) -> String {
let (hours, secondsAfterHours) = divmod(durationInSeconds, 3600)
let (minutes, seconds) = divmod(secondsAfterHours, 60)
return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}
func divmod(_ numerator: Int,
_ denominator: Int) -> (quotient: Int, remainder: Int) {
func isLeapYear(_ year: Int) -> Bool {
if year % 4 != 0 {
print("The year \(year) is not leap year because it is not evenly divisible by 4.")
return false
} else if year % 100 != 0 {
print("The year \(year) is a leap year because it is divisible by 4 and not by 100.")
return true
} else if year % 400 == 0 {
print("The year \(year) is a leap year because it is divisible by 400")
return true
@nitin-agam
nitin-agam / CreditCardView.swift
Last active January 13, 2022 11:03
How to create credit card view using SwiftUI ?
struct CreditCardView: View {
var body: some View {
VStack(alignment: .leading, spacing: 20) {
HStack {
Text("Royale Gold Card")
.font(.system(size: 20, weight: .semibold, design: .monospaced))
Spacer()
Image("american_ex")
.resizable()
extension UIView {
class func gradientViewWithText(text: String, size: CGSize, firstColor: UIColor, secondColor: UIColor, font: UIFont) -> UIView {
let view = UIView()
let layer = CAGradientLayer()
layer.colors = [firstColor.cgColor, secondColor.cgColor]
layer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
view.layer.addSublayer(layer)
let textLabel = UILabel()
import UIKit
// MARK: - IndexPaths
extension UICollectionView {
var lastIndexPath: IndexPath? {
return lastIndexPath(inSection: lastSection)
}
var lastSection: Int {
import Foundation
extension Date {
func timeAgo() -> String {
let secondsAgo = Int(Date().timeIntervalSince(self))
let minute = 60
let hour = 60 * minute
struct Stack<T>: CustomStringConvertible {
// array of items
var items: [T] = []
// to print the formatted description
var description: String {
return "---- Stack begin ----\n" +
items.map({ "\($0)" }).joined(separator: "\n") +
"\n---- Stack End ----"
class BindingTextField: UITextField {
var textChangedHandler: (String) -> () = { _ in }
override init(frame: CGRect) {
super.init(frame: frame)
initialSetup()
}
required init?(coder: NSCoder) {
import Foundation
enum NetworkError: Error {
case decodingError
case domainError
case urlError
}
enum HttpMethod: String {
import Network
class NetworkReachability {
static let shared = NetworkReachability()
private var monitor: NWPathMonitor?
private var isMonitoring = false
// use it to notified that monitoring did start.
var didStartMonitoringHandler: (() -> Void)?