Skip to content

Instantly share code, notes, and snippets.

@foxicode
foxicode / String+loadSave.kt
Created March 15, 2020 17:05
Kotlin String load/save extension
import android.annotation.SuppressLint
import android.content.Context
@SuppressLint("ApplySharedPref")
fun String.save(applicationContext: Context, value: Map<String, Any>, clear: Boolean = false, now: Boolean = false) {
val sp = applicationContext.getSharedPreferences(this, Context.MODE_PRIVATE).edit()
if (clear)
sp.clear()
value.keys.forEach { key ->
val v = value[key]
@foxicode
foxicode / String+json.kt
Created March 15, 2020 17:27
Kotlin String extension to help JSON parsing
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
val String.jsonObject: JSONObject?
get() = try {
JSONObject(this)
} catch (e: JSONException) {
null
}
@foxicode
foxicode / String+creditCardFormatted.kt
Created March 15, 2020 17:40
Kotlin String extension to format String as credit card number
val String.creditCardFormatted: String
get() {
val preparedString = replace(" ", "").trim()
val result = StringBuilder()
for (i in preparedString.indices) {
if (i % 4 == 0 && i != 0) {
result.append(" ")
}
result.append(preparedString[i])
}
@foxicode
foxicode / String+awtColor.kt
Created March 15, 2020 18:11
Kotlin String extension to convert hex color to AWT Color
import java.awt.Color
val String.awtColor: Color?
get() {
val r = substring(1, 3).toIntOrNull(16) ?: return null
val g = substring(3, 5).toIntOrNull(16) ?: return null
val b = substring(5, 7).toIntOrNull(16) ?: return null
return Color(r, g, b)
}
@foxicode
foxicode / String+asColor.kt
Last active March 15, 2020 18:28
Kotlin String extension to parse hex colors (Android)
import android.graphics.Color
val String.asColor: Int?
get() = try {
Color.parseColor(this)
} catch (e: java.lang.IllegalArgumentException) {
null
}
@foxicode
foxicode / Podfile
Created March 19, 2020 22:56
Supernova Podfile
platform :ios, '11.0'
inhibit_all_warnings!
target 'Waste-management-app-musafarouk' do
# Add all your pods here
# Supernova Pods
@foxicode
foxicode / HomeActiveNavViewController.swift
Created March 19, 2020 23:02
Example of Supernova export
//
// HomeActiveNavViewController.swift
// Waste-management-app-musafarouk
//
// Created by Supernova.
// Copyright © 2018 Supernova. All rights reserved.
//
// --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
// MARK: - Import
@foxicode
foxicode / SwiftUIView.swift
Created March 19, 2020 23:19
Example of SwiftUI View (by Apple)
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.edgesIgnoringSafeArea(.top)
.frame(height: 300)
CircleImage()
@foxicode
foxicode / CreateButton.swift
Created March 19, 2020 23:26
Example of UIButton creation in Swift
override func viewDidLoad() {
super.viewDidLoad()
let continueButton = UIButton()
continueButton.setTitle("Continue", for: .normal)
continueButton.setTitleColor(UIColor.blue, for: .normal)
continueButton.frame = CGRect(x: 16, y: 32, width: UIScreen.main.bounds.width - 32, height: 44)
continueButton.addTarget(self, action: #selector(pressedButton(_:)), for: .touchUpInside)
self.view.addSubview(continueButton)
}
@foxicode
foxicode / LayoutKit.swift
Created March 19, 2020 23:31
Example of LayoutKit
let image = SizeLayout<UIImageView>(width: 50, height: 50, config: { imageView in
imageView.image = UIImage(named: "earth.jpg")
})
let label = LabelLayout(text: "Hello World!", alignment: .center)
let stack = StackLayout(
axis: .horizontal,
spacing: 4,
sublayouts: [image, label])