Created
February 27, 2020 02:15
-
-
Save amirulzin/a7f825fe8daad85aebcb853a5d70560e to your computer and use it in GitHub Desktop.
AAC ViewModel constructor injection with SavedState support (MIT license)
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
import android.os.Bundle; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.lifecycle.AbstractSavedStateViewModelFactory; | |
import androidx.lifecycle.SavedStateHandle; | |
import androidx.lifecycle.ViewModel; | |
import androidx.lifecycle.ViewModelStoreOwner; | |
import androidx.savedstate.SavedStateRegistryOwner; | |
import javax.inject.Inject; | |
public class DaggerStatelessViewModelFactory<VM extends ViewModel> { | |
private final DaggerViewModelFactory<VM> factory; | |
@Inject | |
public DaggerStatelessViewModelFactory(DaggerViewModelFactory<VM> factory) { | |
this.factory = factory; | |
} | |
@NonNull | |
public <S extends ViewModelStoreOwner & SavedStateRegistryOwner> AbstractSavedStateViewModelFactory createFrom(@NonNull S owner, @Nullable Bundle defaultArgs) { | |
return new AbstractSavedStateViewModelFactory(owner, defaultArgs) { | |
@NonNull | |
@Override | |
protected <T extends ViewModel> T create(@NonNull String key, @NonNull Class<T> modelClass, @NonNull SavedStateHandle handle) { | |
T vm = factory.create(modelClass); | |
if (vm instanceof SavedStateViewModel) { | |
((SavedStateViewModel) vm).init(handle); | |
} | |
return vm; | |
} | |
}; | |
} | |
} |
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 class SampleActivity extends AppCompatActivity { | |
@Inject | |
DaggerStatelessViewModelFactory<SomeViewModel> vmFactory; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
dependencyInjection(); | |
SomeViewModel vm = new ViewModelProvider(this, vmFactory).get(SomeViewModel.class); | |
} | |
private void dependencyInjection() { | |
((SomeApplication) getApplication()) | |
.getSomeAppComponent() | |
.inject(this); | |
} | |
} |
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 interface SavedStateViewModel { | |
void init(@NonNull SavedStateHandle savedStateHandle); | |
} |
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 class SomeViewModel extends ViewModel implements SavedStateViewModel { | |
@Inject | |
public SomeViewModel(){} | |
@Override | |
public void init(@NonNull SavedStateHandle savedStateHandle){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment