Skip to content

Instantly share code, notes, and snippets.

View mdb1's full-sized avatar
🎯
Focusing

Manu mdb1

🎯
Focusing
View GitHub Profile
@mdb1
mdb1 / String+Localized.swift
Last active April 29, 2025 15:24
String Localizable extension
import Foundation
extension String {
/// Returns the localized value for the given key (`self`).
/// This property will look in the `Localizable.strings` file based on the current system language.
///
/// Usage
/// ===================================
/// ```swift
/// "key".localized
@mdb1
mdb1 / LocalizedExample.swift
Created December 27, 2022 12:59
Using localized strings in a SwiftUI View
import SwiftUI
struct ContentView: View {
let name: String
var body: some View {
VStack {
Text(ViewConstants.title.localized(with: [name]))
Text(ViewConstants.description)
}
@mdb1
mdb1 / .swiftformat
Created January 1, 2023 18:11
SwiftFormat options and Rules
# format options
--allman false
--indent 4
--maxwidth 120
--commas inline
--decimalgrouping 3
--wraparguments before-first
# rules
--enable isEmpty
@mdb1
mdb1 / contributing.md
Last active February 16, 2023 20:51
Contributing template

Contributing

This is a living document representing some guidelines that will help our team do great work, and keep consistency in the codebase and the systems over time.

Code Quality Principles

  • Naming → Explicitness, brevity, and consistency.
  • Commenting → Exist to help future readers (including yourself).
  • Testing:
  • Code isn’t high quality without tests.
@mdb1
mdb1 / View+RoundedOverlayBorder.swift
Last active January 3, 2023 22:45
RoundedOverlayBorder modifier
/// Extension to apply a RoundedOverlayBorder modifier.
extension View {
/// Adds a rounded overlay border to the view.
func roundedOverlayBorder(
cornerRadius: CGFloat,
color: Color = .gray,
lineWidth: CGFloat = 1
) -> some View {
modifier(
RoundedOverlayBorderModifier(
@mdb1
mdb1 / View+Scrollable.swift
Created January 3, 2023 22:23
Scrollable View Modifier
extension View {
/// View modifier that embeds the content inside a scroll view.
func scrollable(axis: Axis.Set = .vertical) -> some View {
modifier(
ScrollViewModifier(axis: axis)
)
}
}
/// A modifier that embeds the content inside a scroll view.
@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?
@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 / 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 / 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)