Skip to content

Instantly share code, notes, and snippets.

View cedricbahirwe's full-sized avatar
📈
Small things in a Great way

Cédric Bahirwe cedricbahirwe

📈
Small things in a Great way
View GitHub Profile
@cedricbahirwe
cedricbahirwe / SOLID.swift
Last active August 24, 2022 09:38
A bunch of example illustrating SOLID Principles
import Foundation
class Book {
let name: String
let authorName: String
let year: Int
let price: Double
let isbn: String
init(name: String, authorName: String, year: Int, price: Double, isbn: String) {
@cedricbahirwe
cedricbahirwe / TimeSelectorView.swift
Last active May 3, 2023 16:59
A custom Time Picker for SwiftUI with minutes intervals
struct TimeSelectorView: View {
var editedDate: Date
var completionHandler: (Date) -> Void
private let hours: [Int] = (1..<13).reversed().map { $0 }
private let minutes: [Int] = [0, 15, 30, 45].reversed()
private let meridiems: [Meridiem] = Meridiem.allCases
@State private var selection = TimeSelection()
// Answer to https://stackoverflow.com/questions/69929331/current-time-is-not-displayed-in-the-analog-view
struct Hand: Shape {
let inset: CGFloat
let angle: Angle
func path(in rect: CGRect) -> Path {
let rect = rect.insetBy(dx: inset, dy: inset)
var path = Path()
path.move(to: CGPoint(x: rect.midX, y: rect.midY))
path.addRoundedRect(in: CGRect(x: rect.midX - 4, y: rect.midY - 4, width: 8, height: 8), cornerSize: CGSize(width: 8, height: 8))
@cedricbahirwe
cedricbahirwe / TopSideBarView.swift
Created September 13, 2021 08:02
A Simple Top SideBar View in SwiftUI
import SwiftUI
struct TopSideBarView: View {
private enum Constants {
static let foregrounColor = Color(.systemBackground)
static let backgroundColor = Color(.label).opacity(0.9)
static let avatarMinSize: CGFloat = 60
@cedricbahirwe
cedricbahirwe / DebuggingResource.swift
Created September 9, 2021 08:02
A monolithic class for printing logs when dealing with Networking
class DebuggingResource: NSObject {
private static func printLine(_ line: String) {
print("--- \(line) ---")
}
public static func logJsonDataFormat(_ data: Data) {
printLine("Start Debugging Json Format")
if let json = try? JSONSerialization.jsonObject(with: data) {
@cedricbahirwe
cedricbahirwe / DraggableAnnotationView.swift
Created August 18, 2021 23:26
A DraggableAnnotationView in MapBox
class DraggableAnnotationView: MGLAnnotationView {
var didSetNewCoord: (CLLocationCoordinate2D) -> Void = { _ in }
init(reuseIdentifier: String, size: CGFloat, didSetCoord: @escaping(CLLocationCoordinate2D) -> Void) {
self.didSetNewCoord = didSetCoord
super.init(reuseIdentifier: reuseIdentifier)
// `isDraggable` is a property of MGLAnnotationView, disabled by default.
isDraggable = true
@cedricbahirwe
cedricbahirwe / DebuggingResource.swift
Created August 18, 2021 20:18
A simple helper for debugging my networking requests, response and status code😀.
class DebuggingResource: NSObject {
private static func printLine(_ line: String) {
print("--- \(line) ---")
}
public static func logJsonDataFormat(_ data: Data) {
printLine("Start Debugging Json Format")
if let json = try? JSONSerialization.jsonObject(with: data) {
@cedricbahirwe
cedricbahirwe / PassCodeCodeView.swift
Created August 15, 2021 21:49
OneTime Code SwiftUI Wrapper Customizable View
struct PassCodeCodeView: UIViewRepresentable {
init(_ count: Int = 5,
width: CGFloat,
height: CGFloat,
spacing: CGFloat = 10,
action: @escaping (String) -> ()) {
self.width = width
self.height = height
self.spacing = spacing
@cedricbahirwe
cedricbahirwe / UIDynamicAnimator.swift
Created July 25, 2021 14:11
A simple prototype using `UIDynamicAnimator`
class ViewController: UIViewController {
var animator: UIDynamicAnimator?
var origins: [(String, CGPoint)] = []
let range: ClosedRange<CGFloat> = 0.0...255.0
var message = "This is cedric trying to animate the components with gravity"
override func viewDidLoad() {
@cedricbahirwe
cedricbahirwe / LocalStorageProperyWrapper.swift
Created July 20, 2021 11:11
An implementation of Property Wrapper used to store data in UserDefaults in swift.
@propertyWrapper
struct LocalStorage<T: Codable> {
private let userDefaults: UserDefaults = .standard
private let key: String
private let defaultValue: T
init(wrappedValue defaultValue: T, key: String) {
self.key = key
self.defaultValue = defaultValue
}