Skip to content

Instantly share code, notes, and snippets.

View cloudbank's full-sized avatar
🎯
Focusing

S.G. Vogel cloudbank

🎯
Focusing
View GitHub Profile
private fun updateImageWithImageDecoder(assetFileName: String, imageView: ImageView) {
val context = requireContext()
viewLifecycleOwner.lifecycleScope.launch {
val d = withContext(Dispatchers.Default) {
val source = ImageDecoder.createSource(context.assets, assetFileName)
val drawable = ImageDecoder.decodeDrawable(source)
return@withContext drawable
}
withContext(Dispatchers.Main) {
//1) inject a datasource and repository, which is passed to the constructor of the ViewModel
@Singleton
class LoginDataSource @Inject constructor(){
private var mAuth: FirebaseAuth. ...//
class LoginRepository @Inject constructor(val dataSource: LoginDataSource) { ...
//2) use the Executor overload from the DataSource:
var executor: ThreadPoolExecutor = ThreadPoolExecutor(
numCores * 2, numCores * 2,
@Module
abstract class ViewModelModule {
@Binds
internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
@Binds
@IntoMap
@ViewModelKey(LoginViewModel::class)
@Module
abstract class ViewModelModule {
@Binds
internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory
@Binds
@IntoMap
@ViewModelKey(LoginViewModel::class)
@Singleton
class ViewModelFactory @Inject constructor(private val viewModels: MutableMap<Class<out ViewModel>, Provider<ViewModel>>) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T }
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
@cloudbank
cloudbank / Rover.java
Last active September 8, 2019 23:34
Mars rover given a list of String cmds in an nxn matrix, return the square you end on.
public class Rover {
public static int roverMove(int matrixSize, List<String> cmds) {
int result = 0;
for (int i = 0; i < cmds.size(); i++) {
result = process(cmds.get(i), matrixSize, result);
}
return result;
}
static int process(String cmd, int n, int current) {
@cloudbank
cloudbank / SalaryCap
Last active October 6, 2018 02:40
determine the salary cap given a target and list of salaries (unsorted)
public int salaryCap(int[] salaries, int target) {
int a = target / salaries.length;
int sum = 0, count=0;
for (int s: salaries) {
if (s <= a) {
sum += s;
} else {
count++;
}
}
public class TestApplication extends DaggerApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(this).build();
}
}
@Module
public abstract class AndroidBindingModule {
@ContributesAndroidInjector
abstract MainActivity bindMainActivity();
}
@Module
public class RepoModule {
@Provides
TestRepository provideTestRepository() {
return new TestRepository("blah");
}
}