aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/comment/CommentViewModel.java
blob: faa2d6e4c3b847e6d49006c24f05ff8d0a8f6547 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package ml.docilealligator.infinityforreddit.comment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;

import ml.docilealligator.infinityforreddit.NetworkState;
import ml.docilealligator.infinityforreddit.thing.SortType;
import retrofit2.Retrofit;

public class CommentViewModel extends ViewModel {
    private final CommentDataSourceFactory commentDataSourceFactory;
    private final LiveData<NetworkState> paginationNetworkState;
    private final LiveData<NetworkState> initialLoadingState;
    private final LiveData<Boolean> hasCommentLiveData;
    private final LiveData<PagedList<Comment>> comments;
    private final MutableLiveData<SortType> sortTypeLiveData;

    public CommentViewModel(Retrofit retrofit, @Nullable String accessToken, @NonNull String accountName,
                            String username, SortType sortType, boolean areSavedComments) {
        commentDataSourceFactory = new CommentDataSourceFactory(retrofit, accessToken, accountName,
                username, sortType, areSavedComments);

        initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
                CommentDataSource::getInitialLoadStateLiveData);
        paginationNetworkState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
                CommentDataSource::getPaginationNetworkStateLiveData);
        hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
                CommentDataSource::hasPostLiveData);

        sortTypeLiveData = new MutableLiveData<>(sortType);

        PagedList.Config pagedListConfig =
                (new PagedList.Config.Builder())
                        .setEnablePlaceholders(false)
                        .setPageSize(100)
                        .setPrefetchDistance(10)
                        .setInitialLoadSizeHint(10)
                        .build();

        comments = Transformations.switchMap(sortTypeLiveData, sort -> {
            commentDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
            return (new LivePagedListBuilder(commentDataSourceFactory, pagedListConfig)).build();
        });
    }

    public LiveData<PagedList<Comment>> getComments() {
        return comments;
    }

    public LiveData<NetworkState> getPaginationNetworkState() {
        return paginationNetworkState;
    }

    public LiveData<NetworkState> getInitialLoadingState() {
        return initialLoadingState;
    }

    public LiveData<Boolean> hasComment() {
        return hasCommentLiveData;
    }

    public void refresh() {
        commentDataSourceFactory.getCommentDataSource().invalidate();
    }

    public void retryLoadingMore() {
        commentDataSourceFactory.getCommentDataSource().retryLoadingMore();
    }

    public void changeSortType(SortType sortType) {
        sortTypeLiveData.postValue(sortType);
    }

    public static class Factory extends ViewModelProvider.NewInstanceFactory {
        private final Retrofit retrofit;
        private final String accessToken;
        private final String accountName;
        private final String username;
        private final SortType sortType;
        private final boolean areSavedComments;

        public Factory(Retrofit retrofit, @Nullable String accessToken, @NonNull String accountName, String username,
                       SortType sortType, boolean areSavedComments) {
            this.retrofit = retrofit;
            this.accessToken = accessToken;
            this.accountName = accountName;
            this.username = username;
            this.sortType = sortType;
            this.areSavedComments = areSavedComments;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T) new CommentViewModel(retrofit, accessToken, accountName, username,
                    sortType, areSavedComments);
        }
    }
}