Skip to content

Instantly share code, notes, and snippets.

View freeworld1995's full-sized avatar
🎯
Focusing

Jimmy Bui freeworld1995

🎯
Focusing
  • Vietnam
View GitHub Profile
@freeworld1995
freeworld1995 / AutoRetry.swift
Created February 24, 2022 14:16 — forked from kean/AutoRetry.swift
Smart Auto Retry using RxSwift
// The MIT License (MIT)
//
// Copyright (c) 2017 Alexander Grebenyuk (github.com/kean).
import Foundation
import RxSwift
import RxCocoa
extension ObservableType {
@freeworld1995
freeworld1995 / SheetModalPresentationController.swift
Created December 10, 2021 10:34 — forked from vinczebalazs/SheetModalPresentationController.swift
A presentation controller to use for presenting a view controller modally, which can be dismissed by a pull down gesture. The presented view controller's height is also adjustable.
import UIKit
extension UIView {
var allSubviews: [UIView] {
subviews + subviews.flatMap { $0.allSubviews }
}
func firstSubview<T: UIView>(of type: T.Type) -> T? {
allSubviews.first { $0 is T } as? T

Understanding UIViewController Rotation

Problem

To enable the rotation of a single view controller used to display the preview of Images/Videos. It is intuitive to allow user to rotate there device and screen changes accordingly, so it feels pleasant. But to achieve this, we need to enable the (almost) all Supported Device orientations.

Ex: `Portrait`, `LandscapeLeft`, `LandscapeRight`.
@freeworld1995
freeworld1995 / libdispatch-efficiency-tips.md
Created August 20, 2021 08:12 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@freeworld1995
freeworld1995 / uicollectionview+fade.md
Created August 6, 2021 10:45 — forked from vinhnx/uicollectionview+fade.md
Collection view cell fade as scrolling effect

// reference: https://stackoverflow.com/a/42705208/1477298

You can do a lot of fun stuff to collection views. I like to subclass UICollectionViewFlowLayout. Here is an example that fades the top and the bottom of the collection view based on distance from center. I could modify it to fade only the very edges but you should figure it after you look through the code.

import UIKit

class FadingLayout: UICollectionViewFlowLayout,UICollectionViewDelegateFlowLayout {

    //should be 0<fade<1
@freeworld1995
freeworld1995 / SwiftConcurrency.md
Created July 20, 2021 10:46 — forked from FWEugene/SwiftConcurrency.md
All about concurrency

Threads

Foundation offers a Thread class, internally based on pthread, that can be used to create new threads and execute closures.

// Detaches a new thread and uses the specified selector as the thread entry point.
Thread.detachNewThreadSelector(selector: Selector>, toTarget: Any, with: Any)

// Subclass
class MyThread: Thread {
func setupGoogleButton() {
GIDSignIn.sharedInstance().delegate = self
let button = GIDSignInButton()
button.translatesAutoresizingMaskIntoConstraints = false
let buttonBottom = NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: -96)
let buttonTrailing = NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: -24)
func setupFacebookButton() {
let button = FBSDKLoginButton()
let manager = FBSDKLoginManager()
button.translatesAutoresizingMaskIntoConstraints = false
let buttonBottom = NSLayoutConstraint(item: button, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1.0, constant: -32)
let buttonTrailing = NSLayoutConstraint(item: button, attribute: .trailing, relatedBy: .equal, toItem: self.view, attribute: .trailing, multiplier: 1.0, constant: -24)
let buttonLeading = NSLayoutConstraint(item: button, attribute: .leading, relatedBy: .equal, toItem: self.view, attribute: .leading, multiplier: 1.0, constant: 24)
let buttonHeight = NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 48)