Skip to content

Instantly share code, notes, and snippets.

View demonar's full-sized avatar
😄
(¯`•._.•«<ÐêMøN>»•._.•´¯) -> Building mobile apps with Kotlin and Swift

Alejandro Moya demonar

😄
(¯`•._.•«<ÐêMøN>»•._.•´¯) -> Building mobile apps with Kotlin and Swift
View GitHub Profile
class SeachActivity : AppCompatActivity() {
lateinit var searchView: SearchView
private val vm: SearchViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.search_activity_layout)
recyclerView.adapter = vm.adapter
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/searchView"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.demon.android.demo.activity.SearchActivity">
<include layout="@layout/toolbar" />
sealed class Result<T> {
data class Success<T>(val call: Call<T>, val response: Response<T>): Result<T>()
data class Failure<T>(val call: Call<T>, val error: Throwable): Result<T>()
}
inline fun <reified T> Call<T>.enqueue(crossinline result: (Result<T>) -> Unit) {
enqueue(object: Callback<T> {
override fun onFailure(call: Call<T>, error: Throwable) {
result(Result.Failure(call, error))
}
override fun onResponse(call: Call<T>, response: Response<T>) {
result(Result.Success(call, response))
}
})
//let's say that we have a login method
val call = service.login(username, pass)
//we call the new enqueue
call.enqueue { result ->
when(result) {
is Result.Success -> {
//myData will have the type passed from the service method
val myData = result.response.body()
}
is Result.Failure -> {
@demonar
demonar / SyncRetrofitCallWithResult.kt
Created October 8, 2019 23:59
This method adds an extension to retrofit to return a Result<T> from execute()
inline fun <reified T> Call<T>.executeForResult(): Result<T> {
return try {
val response = execute()
Result.Success(this, response)
} catch (e: Exception) {
Result.Failure(this, e)
}
}
@demonar
demonar / ReactiveQuickHelp.swift
Last active December 12, 2017 22:54
Reactive cocoa in swift 3/4
//Always import
import ReactiveCocoa //Cocoa extensions
import ReactiveSwift //primitives
//Acces UI extensions with .reactive
self.textField.reactive.
//A text listener in a textfield
// self.reactive.lifetime will make the listener to expire on dealloc
import UIKit
private var kAssociationKeyNextField: UInt8 = 0
extension UITextField {
@IBOutlet var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
import UIKit
extension UIView {
func scrollToY(y: CGFloat) {
scrollToY(y, velocity: 0.25)
}
func scrollToView(view: UIView) {
scrollToView(view, velocity: 0.25)