This file contains 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
enum MyError: ErrorType { | |
case Test | |
} | |
enum Future<WrappedObject> { | |
case Present(value:WrappedObject) | |
case Error(error:ErrorType) | |
case Finally | |
case None | |
} |
This file contains 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
/* | |
The MIT License (MIT) | |
Copyright (c) 2016 Tigerspike | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in the | |
Software without restriction, including without limitation the rights to use, copy, | |
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
and to permit persons to whom the Software is furnished to do so, subject to the |
This file contains 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 void onCreate(Bundle savedInstanceState) { | |
MyViewModel model = ViewModelProviders.of(this).get(MyViewModel.class); | |
model.getUsers().observe(this, users -> { | |
// update UI | |
}); | |
} |
This file contains 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 static ViewModelProvider of(@NonNull Fragment fragment) { | |
AndroidViewModelFactory factory = AndroidViewModelFactory.getInstance(checkApplication(checkActivity(fragment))); | |
return new ViewModelProvider(ViewModelStores.of(fragment), factory); | |
} |
This file contains 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
final void put(String key, ViewModel viewModel) { | |
ViewModel oldViewModel = mMap.get(key); | |
if (oldViewModel != null) { | |
oldViewModel.onCleared(); | |
} | |
mMap.put(key, viewModel); | |
} | |
final ViewModel get(String key) { | |
return mMap.get(key); |
This file contains 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
@NonNull | |
@Override | |
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | |
if (AndroidViewModel.class.isAssignableFrom(modelClass)) { | |
try { | |
return modelClass.getConstructor(Application.class).newInstance(mApplication); | |
} catch ... { | |
... | |
} | |
} |
This file contains 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 <T extends ViewModel> T create(@NonNull Class<T> modelClass) { | |
try { | |
return modelClass.newInstance(); | |
} catch (InstantiationException e) { | |
... | |
} | |
} |
This file contains 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 <T extends ViewModel> T get(@NonNull Class<T> modelClass) { | |
String canonicalName = modelClass.getCanonicalName(); | |
if (canonicalName == null) { | |
throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels"); | |
} | |
return get(DEFAULT_KEY + ":" + canonicalName, modelClass); | |
} | |
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) { | |
ViewModel viewModel = mViewModelStore.get(key); |
This file contains 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 static ViewModelProvider of(@NonNull Fragment fragment) { | |
AndroidViewModelFactory factory = AndroidViewModelFactory.getInstance(checkApplication(checkActivity(fragment))); | |
return new ViewModelProvider(ViewModelStores.of(fragment), factory); | |
} |
This file contains 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 static ViewModelStore of(@NonNull Fragment fragment) { | |
if (fragment instanceof ViewModelStoreOwner) { | |
return ((ViewModelStoreOwner) fragment).getViewModelStore(); | |
} | |
return holderFragmentFor(fragment).getViewModelStore(); | |
} | |
... | |
public static HolderFragment holderFragmentFor(Fragment fragment) { |
OlderNewer