Skip to content

Instantly share code, notes, and snippets.

View aunnnn's full-sized avatar
🏠
Working from home

Wirawit Rueopas aunnnn

🏠
Working from home
View GitHub Profile
@aunnnn
aunnnn / collectionview.swift
Created May 15, 2020 05:43 — forked from chriseidhof/collectionview.swift
SwiftUI Flow Layout
//
// ContentView.swift
// CollectionView
//
// Created by Chris Eidhof on 20.08.19.
// Copyright © 2019 Chris Eidhof. All rights reserved.
//
import SwiftUI
final class MyCell: UICollectionViewCell {
override var isHighlighted: Bool {
didSet {
shrink(down: isHighlighted)
}
}
}
// You can make it nicer by using enum (.down, .up) instead of Bool
func shrink(down: Bool) {
UIView.animate(withDuration: 0.6) {
if down {
cell.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
else {
cell.transform = .identity
}
}
}
import EarlGrey
import Salsa
import XCTest
@testable import YourApp
class Test: XCTestCase {
override func setup() {
super.setup()
}
@aunnnn
aunnnn / test.json
Created May 17, 2018 16:14
Decodable from nested keys
{
"applmusic": {
"code": "AAPL",
"quality": "good",
"line": "She told me don't worry"
},
"spotify": {
"differentcode": "SPOT",
"music_quality": "good",
"spotify_specific_code": "absent in apple"
@aunnnn
aunnnn / test.json
Last active May 17, 2018 16:16
Decodable from specific key of json
{
"applmusic":{
"code":"AAPL",
"quality":"good",
"line":"She told me don't worry",
},
"spotify":{
"differentcode":"SPOT",
"music_quality":"good",
"spotify_specific_code":"absent in apple"
// Linear cell's size animation
UIView.animate(withDuration: self.transitionDuration(using: ctx), animations: {
// other animations ...
// card width & height expansion (widthAnc and heightAnc are stored previously)
self.widthAnc.constant = detailVc.cardContentView.bounds.width
self.heightAnc.constant = detailVc.cardContentView.bounds.height
// call layoutIfNeeded() on the animatingContainerOView
self.animatingContainerView.layoutIfNeeded()
class SomeTransitionManager: UIPercentDrivenInteractiveTransition, UIViewControllerAnimatedTransitioning {
// ..
}
// Bind everything together
extension SomeTransitionManager: UIViewControllerTransitioningDelegate {
func animationController(fortPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return self
}
func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
class SomeViewController: UIViewController {
private let serviceBuilder: () -> Service
private lazy var service: Service = serviceBuilder() // build when it’s really needed.
// Lazy dependency injection!
init(service: @escaping @autoclosure () -> Service) {
serviceBuilder = service
}
func fetch() {
func fetchObject(id: Int, @escaping completion: (Object) -> Void) {
let request = ...
APIService.get(request) { result in
switch result {
case .success(let object):
completion(object)
case .error:
// make default object, not so flexible here...
let defaultObject = makeDefaultObject()
completion(defaultObject)