Skip to content

Instantly share code, notes, and snippets.

View tonnylitao's full-sized avatar
🎯
Focusing

Tonny tonnylitao

🎯
Focusing
View GitHub Profile
(function() {
var feelGood = {
talktoomuch: "I'm good at programming."
};
var feelBetter = {
arrogant: Infinity
};
var feelBest = function() {
//some time, frc section may be need section offset
@objc public protocol MFetchedResultsControllerOffsetSectionDelegate{
func offsetSection() -> Int
}
class MFetchedResultsController: NSFetchedResultsController, NSFetchedResultsControllerDelegate {
weak var viewController: UIViewController? //UITableViewController UICollectionViewController
weak var scrollView: UIScrollView? //TableView CollectionView
weak var offsetSectionDelegate: MFetchedResultsControllerOffsetSectionDelegate?
@tonnylitao
tonnylitao / Applicative0.hs
Last active October 25, 2015 14:15
Why haskell is better than other programming language?
numCommonFriends x y = do
fx <- friendsOf x --database query, automatically work concurrently
fy <- friendsOf y --database query, automatically work concurrently
return (length (intersect fx fy))
-- NodeJS, Go, GCD, Java...other languages need extra code for concurrency,
@tonnylitao
tonnylitao / Dao.swift
Created November 17, 2016 07:23
Using Protocol Extension Constraint
import Foundation
import UIKit
protocol Dao {}
extension NSObject: Dao {}
extension Dao where Self : NSObject {
typealias ConfigClosure = (Self) -> Void
@tonnylitao
tonnylitao / Android MVP Architecture.md
Last active April 17, 2018 23:16
Android MVP using interfaces to decouple.

#Android MVP

##1. Using MVP Android Activity in old time was God Entity which contained tons of business logic. But in MVP architecture, activity is just activity, it’s a native part managed by system. We strive not to disturb activity in business logic development by putting logic/io/computing… code into Presenter.

class HomeActivity extends AppCompatActivity {
    private HomeActivityPresenter mPresenter;
    //...
@tonnylitao
tonnylitao / 0KeyboardShowAndHideProtocol.swift
Last active June 19, 2018 08:23
KeyboardShowAndHide Protocol
import UIKit
// KeyboardAvoidable
// original by Roy McKenzie
protocol KeyboardShowAndHideProtocol: class {
typealias KeyboardHeightDuration = (height: CGFloat, duration: Double)
typealias KeyboardHeightDurationBlock = (KeyboardHeightDuration) -> Void
@tonnylitao
tonnylitao / SmartSpeed.swift
Last active July 5, 2022 05:15
Smart Speed like Overcast. Smart Speed means shorten the silence of audio for saving time.
do {
try audioPlayer = AVAudioPlayer(contentsOf: url)
audioPlayer.isMeteringEnabled = true
audioPlayer.enableRate = true
audioPlayer.prepareToPlay()
} catch let error as NSError {
print("audioPlayer error \(error.localizedDescription)")
}
audioPlayer.play()
@tonnylitao
tonnylitao / AndroidViewModelFactory.kt
Last active December 12, 2020 16:48
Android ViewModel with Variable Arguments
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlin.reflect.full.primaryConstructor
class AndroidViewModelFactory(private vararg val args: Any) :
ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>) =
modelClass.kotlin.primaryConstructor?.call(*args)
?: throw IllegalArgumentException("$modelClass primaryConstructor is null")
@tonnylitao
tonnylitao / Factory.kt
Created April 30, 2020 22:16
Copy paste demo
class IntViewModelFactory(val application: Application, val int: Int) :
ViewModelProvider.NewInstanceFactory() {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>) =
IntViewModel(application, int) as T
}
class StringViewModelFactory(val application: Application, val string: String) :
ViewModelProvider.NewInstanceFactory() {
import androidx.recyclerview.widget.DiffUtil
interface RecyclerItem {
val layoutId: Int
val variableId: Int
val dataToBind: Any
val id: Int