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
package javax.inject; | |
public interface Provider<T> { | |
T get(); | |
} |
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 MyViewModelFactory @Inject constructor( | |
private val mainFragModel: Provider<MainFragmentViewModel> | |
) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
return when (modelClass) { | |
MainFragmentViewModel::class.java -> mainFragModel.get() | |
else -> TODO("Missing viewModel $modelClass") | |
} as 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
class MyFragmentFactory @Inject constructor( | |
private val mainFragProvider: Provider<MainFragment> | |
) : FragmentFactory() { | |
override fun instantiate(classLoader: ClassLoader, className: String): Fragment { | |
return when (className) { | |
MainFragment::class.java.canonicalName -> mainFragProvider.get() | |
else -> TODO("Missing fragment $className") | |
} | |
} |
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 MainFragmentViewModel @Inject constructor(myService: MyService) : ViewModel() { | |
private val data: MutableLiveData<String> = MutableLiveData() | |
init { | |
// call async service with callback | |
myService.getData(data::setValue) | |
} | |
fun getData(): LiveData<String> = data |
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 MainFragment | |
@Inject constructor(private val factory: ViewModelProvider.Factory) : Fragment() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val viewModel by viewModels<MainFragmentViewModel> { factory } | |
// get some data sync, show a loader and display the data | |
viewModel.getData().observe(this, Observer { |
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
apply: plugin “kotlin-kapt” | |
// just gradle things | |
implementation "com.google.dagger:dagger:2.23.2" | |
kapt "com.google.dagger:dagger-compiler:2.23.2" | |
compileOnly “javax.annotation:jsr250-api:1.0” |
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
public class UI: Kotlinx_coroutines_core_nativeCoroutineDispatcher { | |
override public func dispatch(context: KotlinCoroutineContext, block: Kotlinx_coroutines_core_nativeRunnable) { | |
DispatchQueue.main.async { | |
block.run() | |
} | |
} | |
} |
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 | |
import app | |
class ViewController: UIViewController, PhotoView { | |
lazy var actions: PhotoPresenter = { | |
PhotoPresenter( | |
uiContext: UI() as KotlinCoroutineContext, | |
view: self | |
) | |
}() |
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 MainActivity : AppCompatActivity(), PhotoView { | |
override var isUpdating: Boolean by Delegates.observable(false) { _, _, isLoading -> | |
if (isLoading) { | |
progressBar.visibility = View.VISIBLE | |
button.visibility = View.GONE | |
imageView.visibility = View.GONE | |
text.visibility = View.GONE | |
} else { | |
progressBar.visibility = View.GONE |
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 android.util.Log | |
actual fun log(level: LogLevel, tag: String, message: String, error: Throwable) { | |
when (level) { | |
is LogLevel.DEBUG -> Log.d(tag, message, error) | |
is LogLevel.INFO -> Log.i(tag, message, error) | |
is LogLevel.WARN -> Log.w(tag, message, error) | |
is LogLevel.ERROR -> Log.e(tag, message, error) | |
} | |
} |