Skip to content

Instantly share code, notes, and snippets.

View MojtabaHs's full-sized avatar
🏆
Worldwide 2nd in SwiftUI - According to StackOverflow

Seyed Mojtaba Hosseini Zeidabadi MojtabaHs

🏆
Worldwide 2nd in SwiftUI - According to StackOverflow
View GitHub Profile
@MojtabaHs
MojtabaHs / SO.txt
Last active August 13, 2019 05:45
Socket new question data example
{
"action": "1-questions-newest-tag-ios",
"data": "{"id":"57471635","body":"<div class=\"question-summary\" id=\"question-summary-57471635\">\r\n <div class=\"statscontainer\">\r\n <div class=\"stats\">\r\n <div class=\"vote\">\r\n <div class=\"votes\">\r\n <span class=\"vote-count-post \"><strong>0</strong></span>\r\n <div class=\"viewcount\">votes</div>\r\n </div>\r\n </div>\r\n <div class=\"status unanswered\">\r\n <strong>0</strong>answers\r\n </div>\r\n </div>\r\n <div class=\"views \" title=\"1 view\">\r\n 1 view\r\n</div>\r\n </div>\r\n <div class=\"summary\">\r\n <h3><a href=\"/questions/57471635/firebase-crashlytics-not-uploading-dysm-automatically\" class=\"question-hyperlink\">Firebase crashlytics not uploading dysm automatically</a></h3>\r\n <div class=\"excerpt\">\r\n I'm having problem when migrating to firebase crashlytics from fabric with the automatic upload of dysm. Ever since i migrated to firebase the automatic upload isn't working.\n\nI have already tried ...\r\n </div>\r\n <div class=\"t
@MojtabaHs
MojtabaHs / UIViewForSwiftUI.swift
Last active April 18, 2023 10:33
This is a simple file you can use to bring any UIKit UIView in to the SwiftUI. Fully customizable.
import SwiftUI
protocol UIViewRepresentableHelper: UIViewRepresentable {
var configuration: (UIViewType) -> () { get set }
}
@available(iOS 13.0, *)
extension UIViewRepresentableHelper {
func makeUIView(context: UIViewRepresentableContext<Self>) -> UIViewType {
let uiView = UIViewType()
@MojtabaHs
MojtabaHs / SomeKindOfBool.swift
Last active June 17, 2022 15:16
Decode logical `Bool` into a real `Bool` with this simple Property Wrapper.
// MARK: - Wrapper
@propertyWrapper
struct SomeKindOfBool: Decodable {
var wrappedValue: Bool
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let stringifiedValue = try? container.decode(String.self) {
switch stringifiedValue.lowercased() {
case "false", "no", "0", "n", "f": wrappedValue = false
struct <#NSViewForSwiftUI#>: NSViewRepresentable {
typealias TheNSView = <#The Original UIView Type#> /* e.g. NSProgressIndicator */
var configuration = { (view: TheNSView) in }
func makeNSView(context: NSViewRepresentableContext<ProgressIndicator>) -> NSProgressIndicator {
TheNSView()
}
func updateNSView(_ nsView: NSProgressIndicator, context: NSViewRepresentableContext<ProgressIndicator>) {
@MojtabaHs
MojtabaHs / NSViewForSwiftUI.swift
Last active January 11, 2020 06:42
This is a simple file you can use to bring any AppKit NSView in to the SwiftUI. Fully customizable.
struct <#NSViewForSwiftUI#>: NSViewRepresentable {
typealias TheNSView = <#The Original UIView Type#> /* e.g. NSProgressIndicator */
var configuration = { (view: TheNSView) in }
func makeNSView(context: NSViewRepresentableContext<ProgressIndicator>) -> NSProgressIndicator {
TheNSView()
}
func updateNSView(_ nsView: NSProgressIndicator, context: NSViewRepresentableContext<ProgressIndicator>) {
@MojtabaHs
MojtabaHs / convertFromUpperCamelCase.swift
Last active February 26, 2024 14:47
Strategy for decoding UpperCamelCase JSONs to preferred lowerCamelCase version.
public extension JSONDecoder.KeyDecodingStrategy {
/// Convert from "UpperCamelCaseKeys" to "lowerCamelCaseKeys" before attempting to match a key with the one specified by each type.
///
/// The conversion to upper case uses `Locale.system`, also known as the ICU "root" locale. This means the result is consistent regardless of the current user's locale and language preferences.
///
/// - Copyright: This function is based on this [source](https://gist.github.com/MojtabaHs/1f9a0f19bafaecd0c94f585648244303)
static var convertFromUpperCamelCase: Self {
.custom { keys in
/// - Precondition: `Keys` array must never be empty.
guard let key = keys.last else { fatalError("Keys should not be empty") }
#if DEBUG
import SwiftUI
protocol UIViewControllerRepresentableHelper: UIViewControllerRepresentable {
var configuration: (UIViewControllerType) -> () { get set }
}
extension UIViewControllerRepresentableHelper {
func makeUIViewController(context: Context) -> UIViewControllerType { UIViewControllerType() }
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) { configuration(uiViewController) }
@MojtabaHs
MojtabaHs / Faillible.swift
Created July 9, 2020 11:19
A property wrapper for arrays with invalid decodable elements.
@propertyWrapper
struct Fallible<Value: Decodable>: Decodable {
var wrappedValue: [Value] = []
private struct _None: Decodable {}
init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
while !container.isAtEnd {
if let decoded = try? container.decode(Value.self) {
@MojtabaHs
MojtabaHs / MaskableView.swift
Created August 1, 2020 22:14
A class for masking any subclass of UIView with an Image.
@IBDesignable
class MaskableView: <#AnyUIViewSubclass#> {
var maskImageView = UIImageView()
@IBInspectable
var maskImage: UIImage? {
didSet {
maskImageView.image = maskImage
updateView()
}
@MojtabaHs
MojtabaHs / PropertyWrapper-BundleFile.swift
Last active August 5, 2020 08:19
A PropertyWrapper for load and decode any Decodable from the given bundle.
@propertyWrapper struct BundleFile<DataType> {
let name: String
let type: String
let fileManager: FileManager = .default
let bundle: Bundle = .main
let decoder: (Data) -> DataType
var wrappedValue: DataType {
guard let path = bundle.path(forResource: name, ofType: type) else { fatalError("Resource not found") }
guard let data = fileManager.contents(atPath: path) else { fatalError("File not loaded") }