aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/customtheme/CustomThemeViewModel.java
blob: 9da70d3cf4414a22ee9843bf33284ff18eefb75c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ml.docilealligator.infinityforreddit.customtheme;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelKt;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.PagingData;

import java.util.List;
import java.util.concurrent.Executor;

import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import retrofit2.Retrofit;

public class CustomThemeViewModel extends ViewModel {
    @Nullable
    private LocalCustomThemeRepository localCustomThemeRepository;
    @Nullable
    private OnlineCustomThemeRepository onlineCustomThemeRepository;

    public CustomThemeViewModel(RedditDataRoomDatabase redditDataRoomDatabase) {
        localCustomThemeRepository = new LocalCustomThemeRepository(redditDataRoomDatabase);
    }

    public CustomThemeViewModel(Executor executor, Retrofit retrofit,
                                RedditDataRoomDatabase redditDataRoomDatabase) {
        onlineCustomThemeRepository = new OnlineCustomThemeRepository(executor, retrofit, redditDataRoomDatabase,
                ViewModelKt.getViewModelScope(this));
    }

    @Nullable
    public LiveData<List<CustomTheme>> getAllCustomThemes() {
        return localCustomThemeRepository.getAllCustomThemes();
    }

    public LiveData<CustomTheme> getCurrentLightThemeLiveData() {
        return localCustomThemeRepository.getCurrentLightCustomTheme();
    }

    public LiveData<CustomTheme> getCurrentDarkThemeLiveData() {
        return localCustomThemeRepository.getCurrentDarkCustomTheme();
    }

    @Nullable
    public LiveData<CustomTheme> getCurrentAmoledThemeLiveData() {
        return localCustomThemeRepository.getCurrentAmoledCustomTheme();
    }

    public LiveData<PagingData<OnlineCustomThemeMetadata>> getOnlineCustomThemeMetadata() {
        return onlineCustomThemeRepository.getOnlineCustomThemeMetadata();
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory {
        private Executor executor;
        private Retrofit retrofit;
        private final RedditDataRoomDatabase mRedditDataRoomDatabase;
        private final boolean isOnline;

        public Factory(RedditDataRoomDatabase redditDataRoomDatabase) {
            mRedditDataRoomDatabase = redditDataRoomDatabase;
            isOnline = false;
        }

        public Factory(Executor executor, Retrofit retrofit, RedditDataRoomDatabase redditDataRoomDatabase) {
            this.executor = executor;
            this.retrofit = retrofit;
            mRedditDataRoomDatabase = redditDataRoomDatabase;
            isOnline = true;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            if (isOnline) {
                return (T) new CustomThemeViewModel(executor, retrofit, mRedditDataRoomDatabase);
            } else {
                return (T) new CustomThemeViewModel(mRedditDataRoomDatabase);
            }
        }
    }
}