Created
February 17, 2021 17:56
-
-
Save NinoDLC/477fffa52672bd8f6e49dcfb6e48e7ac to your computer and use it in GitHub Desktop.
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 androidx.annotation.NonNull; | |
import androidx.lifecycle.ViewModel; | |
import androidx.lifecycle.ViewModelProvider; | |
import java.time.Clock; | |
import fr.delcey.mareu.domain.MeetingRepository; | |
import fr.delcey.mareu.ui.create.CreateMeetingViewModel; | |
import fr.delcey.mareu.ui.meetings.MeetingViewModel; | |
public class ViewModelFactory implements ViewModelProvider.Factory { | |
private static ViewModelFactory factory; | |
private ViewModelFactory() { | |
} | |
public static ViewModelFactory getInstance() { | |
if (factory == null) { | |
synchronized (ViewModelFactory.class) { | |
if (factory == null) { | |
factory = new ViewModelFactory(); | |
} | |
} | |
} | |
return factory; | |
} | |
@SuppressWarnings("unchecked") | |
@NonNull | |
@Override | |
public <T extends ViewModel> T create(Class<T> modelClass) { | |
if (modelClass.isAssignableFrom(MeetingViewModel.class)) { | |
return (T) new MeetingViewModel( | |
MainApplication.getInstance().getResources(), | |
MeetingRepository.getInstance() | |
); | |
} else if (modelClass.isAssignableFrom(CreateMeetingViewModel.class)) { | |
return (T) new CreateMeetingViewModel( | |
MainApplication.getInstance().getResources(), | |
MeetingRepository.getInstance(), | |
Clock.systemDefaultZone() | |
); | |
} | |
throw new IllegalArgumentException("Unknown ViewModel class"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment