This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | |
| } | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 -> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| private var kAssociationKeyNextField: UInt8 = 0 | |
| extension UITextField { | |
| @IBOutlet var nextField: UITextField? { | |
| get { | |
| return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField | |
| } | |
| set(newField) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension UIView { | |
| func scrollToY(y: CGFloat) { | |
| scrollToY(y, velocity: 0.25) | |
| } | |
| func scrollToView(view: UIView) { | |
| scrollToView(view, velocity: 0.25) |