Skip to content

Instantly share code, notes, and snippets.

View mdb1's full-sized avatar
🎯
Focusing

Manu mdb1

🎯
Focusing
View GitHub Profile
@mdb1
mdb1 / ___FILEBASENAME___.swift
Last active September 12, 2023 20:37
Xcode Template - View
//___FILEHEADER___
import SwiftUI
struct ___VARIABLE_ViewName___: View {
@StateObject private var viewModel: ViewModel
init(dependencies: ViewModel.Dependencies) {
_viewModel = StateObject(
wrappedValue: ViewModel(
@mdb1
mdb1 / Fonts+Typography.swift
Created January 20, 2023 13:31
Convenience properties to reuse your typographies.
import SwiftUI
// swiftlint:disable identifier_name
public extension Font {
/// The different options for Public Sans font.
enum PublicSans {
/// H1 Font.
/// Font: Public Sans.
/// Weight: Bold.
/// Size: 24.
@mdb1
mdb1 / Fonts+Register.swift
Created January 20, 2023 13:29
Register Custom Fonts
import CoreGraphics
import CoreText
import UIKit
enum FontError: Swift.Error {
case failedToRegisterFont
}
func registerFont(named name: String) throws {
guard let asset = NSDataAsset(name: "Fonts/\(name)", bundle: Bundle.module),
@mdb1
mdb1 / DefaultJsonEncoderDecoder.swift
Created January 10, 2023 18:47
Default JsonEncoder and JsonDecoder to reuse across the app.
import Foundation
extension JSONEncoder {
/// Returns a JSONEncoder object using:
/// * `.convertToSnakeCase` as `keyEncodingStrategy`.
/// * `.iso8601` as `.dateEncodingStrategy`.
static var `default`: JSONEncoder {
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
encoder.dateEncodingStrategy = .iso8601
@mdb1
mdb1 / DateFormatters.swift
Created January 10, 2023 18:37
Shared date formatters to encapsulate the date display logic
import Foundation
extension DateFormatter {
/// A DateFormatter to display dates with all of your components day of the week ,day, month and year.
/// The `dateFormat` property will always be set to `EEEE, MMMM dd, yyyy`.
/// Example: `Friday, November 11, 2022`.
/// - Parameters:
/// - timeZone: The timezone for the formatter. Default: `.current`.
/// - locale: The locale for the formatter. Default: `.current`.
/// - Returns: The DateFormatter with the applied properties.
@mdb1
mdb1 / pull_request_template.md
Last active January 9, 2023 12:07
Pull Request Template for Swift projects

What

Replace with a description of your changes.

Why

Replace with the reason for the changes you are making.

Screenshots

  • Help reviewers by providing before/after screenshots or a quick video.
  • Remove this section if there are no screenshots or videos.
@mdb1
mdb1 / ViewState.swift
Created January 8, 2023 14:03
A reusable way to handle the state of the views in SwiftUI
import Foundation
/// Enum that represents the state of a view that loads information from the backend.
enum ViewState<Info: Any> {
/// Represents non state. Useful when used in combination with other ViewState.
case initial
/// Represents the loading state.
case loading
/// Represents an error.
case error(_: Error)
@mdb1
mdb1 / ViewStateExampleView.swift
Created January 8, 2023 14:01
A simple view to demonstrate how to use the ViewStateWrapper
import SwiftUI
/// In this example:
/// * The view model holds and publishes the view state wrapper value.
/// * The view reacts to changes by holding an @StateObject reference to the view model.
/// * The initial loading is an empty screen with a ProgressView in the middle.
/// * Once we have some data, the subsequent loadings will not override the loaded data,
/// and instead, will just display a ProgressView in the toolbar.
struct ViewStateExampleView: View {
@StateObject private var viewModel: ViewModel = .init()
@mdb1
mdb1 / LongPressButton.swift
Created January 6, 2023 17:36
A button with a long press gesture recognizer
import SwiftUI
/// A button that needs to be pressed for a given amount of seconds before executing it's action.
/// It contain 4 states:
/// * `initial`: The only state where the button can be tapped.
/// * `loading`: Displays a progress view to the right of the text.
/// * `success`.
/// * `error`.
struct LongPressButton: View {
@GestureState private var isHighlighted = false
@mdb1
mdb1 / LoadingButton.swift
Last active January 4, 2023 21:38
LoadingButton
import SwiftUI
/// A button with 4 states:
/// * `initial`: The only state where the button can be tapped.
/// * `loading`: Displays a progress view to the right of the text.
/// * `success`: Changes the background color to a success color.
/// * `error`: Changes the background color to an error color.
struct LoadingButton: View {
private var title: String
private var loadingTitle: String?