Skip to content

Instantly share code, notes, and snippets.

View charlesmuchene's full-sized avatar
😎
Kotlin, checking out Rust-lang

Charles Muchene charlesmuchene

😎
Kotlin, checking out Rust-lang
View GitHub Profile
// ================LearningIterators.class =================
// class version 52.0 (52)
// access flags 0x31
public final class LearningIterators {
// access flags 0x19
public final static main()V
L0
LINENUMBER 2 L0
@charlesmuchene
charlesmuchene / statuscode.swift
Created July 20, 2020 05:18
Sample status code entity
struct StatusCode: CustomStringConvertible, Equatable {
let code: Int
var description: String
static let ok = StatusCode(code: 200, description: "Success")
static let unauthorized = StatusCode(code: 403, description: "Unauthorized")
static let invalidCredentials = StatusCode(code: 401, description: "Invalid credentials")
static func == (lhs: Int, rhs: StatusCode) -> Bool {
@charlesmuchene
charlesmuchene / apierror.swift
Created July 20, 2020 05:17
Api error entity
struct APIError: Error, Decodable, CustomStringConvertible {
let code: Int
let message: String
let errors: [String] = [] // May be dictionary??
static let genericResponseError = APIError(code: 500, message: "Error getting response")
static let noResponseError = APIError(code: 500, message: "No response from server")
@charlesmuchene
charlesmuchene / scrollviewinsets.swift
Last active June 15, 2020 08:53
Scroll View Insets
let scrollView = UIScrollView()
scrollView.backgroundColor = .systemGreen
scrollView.contentInset = UIEdgeInsets(top: 100, left: 100, bottom: 100, right: 100)
scrollView.contentSize = self.view.bounds.size
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
let views = ["scrollview": scrollView]
NSLayoutConstraint.activate([
NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollview]|", metrics: nil, views: views),
@charlesmuchene
charlesmuchene / TwoDecodableResponseSerializer.swift
Created February 12, 2020 19:02
Alamofire custom serializer
//
// TwoDecodableResponseSerializer.swift
//
// Created by Charles Muchene on 2/12/20.
// Copyright © 2020 SenseiDevs. All rights reserved.
//
import Alamofire
import Foundation
@charlesmuchene
charlesmuchene / peripheral-delegate.swift
Last active April 17, 2020 04:12
iOS as Central - peripheral delegate
// Implement peripheral callback methods
extension SocialService: CBPeripheralDelegate {
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
// Discover characteristics
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
// Request for TraceCharacteristic value -- TraceToken
}
@charlesmuchene
charlesmuchene / central.swift
Created April 17, 2020 03:48
iOS as Central
// Initialize central manager, set self as delegate
centralManager = CBCentralManager(delegate: self, queue: nil, options: [CBCentralManagerOptionRestoreIdentifierKey: TraceService.CMRestoreIdentifierKey])
// Provide implementation for delegate callback methods
extension TraceService: CBCentralManagerDelegate {
func centralManager(_ central: CBCentralManager, willRestoreState dict: [String: Any]) { ... }
func centralManagerDidUpdateState(_ central: CBCentralManager) {
// When .poweredOn, scan for Peripherals
@charlesmuchene
charlesmuchene / peripheral.kt
Last active April 17, 2020 03:38
Android Gatt server - Peripheral
// Starts GattServer to serve remote Central requests
private val gattServer: BluetoothGattServer by lazyNone {
bluetoothManager.openGattServer(this.applicationContext, GattServerCallback())
}
private class GattServerCallback : BluetoothGattServerCallback() {
override fun onConnectionStateChange(device: BluetoothDevice, status: Int, newState: Int) { ... }
override fun onCharacteristicReadRequest(device: BluetoothDevice, requestId: Int, offset: Int, characteristic: BluetoothGattCharacteristic) {
@charlesmuchene
charlesmuchene / advertise.kt
Created April 17, 2020 03:31
Android as Peripheral - Advertising
// Advertisement data: only has Trace UUID to enable filtering
val advertiseData: AdvertiseData = AdvertiseData.Builder()
.addServiceUuid(Services.TRACE.serviceUUID)
.build()
// Advertise
bleAdvertiser.startAdvertising(advertiseSettings, advertiseData, adCallback)
// Advertisement callback - delivers advertising operation status
private inner class AdCallback : AdvertiseCallback() {
@charlesmuchene
charlesmuchene / lazynone.kt
Created April 16, 2020 04:00
A function to create an un-synchronized lazy initialization of a property
/**
* Initialize lazily without thread synchronization
*
* @param initializer Initializer block
* @return [Lazy] instance
*/
fun <T> lazyNone(initializer: () -> T): Lazy<T> = lazy(LazyThreadSafetyMode.NONE, initializer)