Skip to content

Instantly share code, notes, and snippets.

@hlung
hlung / MapValueExample.swift
Last active May 29, 2017 08:36
A Swift 3 example of mapValue(), a map() variant that only converts value of dictionary receiver and returns as a new dictionary instead of array.
//
// MapValueExample.swift
// ViKi
//
// Created by Thongchai Kolyutsakul on 29/5/17.
//
//
import Foundation
import SwiftyJSON

haha

  • 123
  • 456
@hlung
hlung / keyboardHeightObservable.swift
Last active June 11, 2023 11:41 — forked from laurilehmijoki/keyboardHeightObservable.swift
RxSwift Observable on iOS keyboard height. Updated from original. Changelog below.
import RxSwift // Version 3.2.0
import RxCocoa // Version 3.2.0
typealias KeyboardHeightInfo = (CGFloat, TimeInterval)
func keyboardHeight() -> Driver<KeyboardHeightInfo> {
return Observable
.from([
NotificationCenter.default.rx.notification(NSNotification.Name.UIKeyboardWillShow)
.map { notification -> KeyboardHeightInfo in
@hlung
hlung / PlaceholderTextView.swift
Created November 27, 2018 10:41
A UITextView subclass with placeholder text support. Swift 4.2
import UIKit
/// A UITextView subclass with placeholder text support.
/// It uses another UILabel to show the placeholder, shown when text is empty.
class PlaceholderTextView: UITextView {
lazy var placeholderLabel: UILabel = {
let label = UILabel()
label.textColor = UIColor(white: 0.5, alpha: 0.85)
label.backgroundColor = .clear
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // UIApplicationDelegate expects an optional UIWindow
private var appCoordinator: AppCoordinator!
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
@hlung
hlung / Pokemon.kt
Created April 29, 2019 09:24
Pokemon model classes in Kotlin
package com.raywenderlich.pokelist
import kotlinx.serialization.Serializable
@Serializable
data class Pokemon(
val name: String,
val url: String
)
@Serializable
@hlung
hlung / PokeApi.kt
Last active April 29, 2019 09:26
Pokemon API client in Kotlin
package com.raywenderlich.pokelist
import com.raywenderlich.pokelist.shared.ApplicationDispatcher
import com.raywenderlich.pokelist.shared.Image
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
@hlung
hlung / PokeListViewController.swift
Last active April 29, 2019 10:34
A UIViewController invoking PokeApi
import UIKit
import app
class PokeListViewController: UIViewController {
@IBOutlet var pokemonSprite: UIImageView!
@IBOutlet var pokemonInfo: UILabel!
@IBOutlet var pokeListTableView: UITableView!
internal var pokeList: [PokemonEntry] = []
internal var api = PokeApi()
__attribute__((objc_subclassing_restricted))
__attribute__((swift_name("PokeApi")))
@interface AppPokeApi : KotlinBase
- (instancetype)init __attribute__((swift_name("init()"))) __attribute__((objc_designated_initializer));
+ (instancetype)new __attribute__((availability(swift, unavailable, message="use object initializers instead")));
- (void)getPokemonListSuccess:(AppKotlinUnit *(^)(NSArray<AppPokemonEntry *> *))success failure:(AppKotlinUnit *(^)(AppKotlinThrowable * _Nullable))failure __attribute__((swift_name("getPokemonList(success:failure:)")));
@end;
class PokeApi {
fun getPokemonList(success: (List<PokemonEntry>) -> Unit, failure: (Throwable?) -> Unit) {
...
}
}