Skip to content

Instantly share code, notes, and snippets.

View meyusufdemirci's full-sized avatar

Yusuf Demirci meyusufdemirci

View GitHub Profile
@meyusufdemirci
meyusufdemirci / CompressImage.swift
Last active April 16, 2022 09:04
Compress Image By Max Kb
import UIKit
extension UIImage {
func compress(maxKb: Double) -> Data? {
let quality: CGFloat = maxKb / self.sizeAsKb()
let compressedData: Data? = self.jpegData(compressionQuality: quality)
return compressedData
}
@meyusufdemirci
meyusufdemirci / CollectionView+Extensions.swift
Created September 18, 2021 13:16
Register and use collection view cells with generic type
// USAGE
collectionView.register([CELL_CLASS_NAME.self])
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: CELL_CLASS_NAME = collectionView.dequeueReusableCell(indexPath: indexPath)
}
// USAGE
import UIKit
import RxCocoa
import RxSwift
class ListViewModel {
// MARK: Properties
let coins: BehaviorRelay<[Coin]> = .init(value: [])
func refreshCoins() {
import UIKit
import RxSwift
import RxCocoa
class ListController: UITableViewController {
// MARK: Properties
private let searchController: UISearchController = {
let controller: UISearchController = UISearchController()
import Foundation
class ListViewModel {
// MARK: Properties
var coins: [Coin] = []
var coinsDidRefresh: (() -> Void)?
var coinsCouldNotRefresh: (() -> Void)?
import UIKit
class ListController: UITableViewController {
// MARK: Properties
private let searchController: UISearchController = {
let controller: UISearchController = UISearchController()
controller.searchBar.placeholder = "Search"
controller.obscuresBackgroundDuringPresentation = false
import Foundation
protocol ListDelegate {
func coinsDidRefresh()
func coinsCouldNotRefresh()
}
class ListViewModel {
// MARK: Properties
import UIKit
class ListController: UITableViewController {
// MARK: Properties
private let searchController: UISearchController = {
let controller: UISearchController = UISearchController()
controller.searchBar.placeholder = "Search"
controller.obscuresBackgroundDuringPresentation = false
if let userId: String = KeychainManager.shared["userId"], !userId.isEmpty {
// found an userId, which means the user already has opened the app before
// next step: open the home screen or whatever you want
} else {
// the user launches the app for the first time
let newUserId = uniqueId(length: 10)
KeychainManager.shared["userId"] = newUserId
// next step: open the home screen or whatever you want
}
KeychainManager.shared["email"] = "[email protected]"