From 4027a8b725fe4ac75f46ff9a939d5f984ae7c2a4 Mon Sep 17 00:00:00 2001 From: Thomas Date: Wed, 9 Sep 2020 15:00:15 +0200 Subject: [PATCH] Some fixes --- .../fragment/DisplayStatusFragment.java | 8 ++++---- .../fedilab/fedilabtube/viewmodel/AccountsVM.java | 10 +++------- .../fedilab/fedilabtube/viewmodel/ChannelsVM.java | 10 +++------- .../fedilab/fedilabtube/viewmodel/CommentVM.java | 10 +++------- .../app/fedilab/fedilabtube/viewmodel/FeedsVM.java | 14 +++++--------- .../fedilabtube/viewmodel/NotificationsVM.java | 10 +++------- .../fedilab/fedilabtube/viewmodel/PlaylistsVM.java | 10 +++------- .../fedilabtube/viewmodel/PostActionsVM.java | 10 +++------- .../fedilabtube/viewmodel/RelationshipVM.java | 10 +++------- .../fedilab/fedilabtube/viewmodel/SearchVM.java | 10 +++------- 10 files changed, 33 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java index e1d210f..8b926fd 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java @@ -202,6 +202,8 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta } } } + FeedsVM viewModelFeeds = new ViewModelProvider(DisplayStatusFragment.this).get(FeedsVM.class); + SearchVM viewModelSearch = new ViewModelProvider(DisplayStatusFragment.this).get(SearchVM.class); if (mLayoutManager != null) { int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); if (dy > 0) { @@ -211,11 +213,9 @@ public class DisplayStatusFragment extends Fragment implements AccountsHorizonta if (!flag_loading) { flag_loading = true; if (search_peertube == null) { //Not a Peertube search - FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(FeedsVM.class); - viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); + viewModelFeeds.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); } else { - SearchVM viewModel = new ViewModelProvider(DisplayStatusFragment.this).get(SearchVM.class); - viewModel.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); + viewModelSearch.getVideos(max_id, search_peertube).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIewVideos(apiResponse)); } nextElementLoader.setVisibility(View.VISIBLE); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/AccountsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/AccountsVM.java index e7a86f2..2f12563 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/AccountsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/AccountsVM.java @@ -30,23 +30,19 @@ import app.fedilab.fedilabtube.client.PeertubeAPI; public class AccountsVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public AccountsVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData getAccounts(String max_id, String name, accountFetch type) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - loadAccounts(max_id, name, type); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + loadAccounts(max_id, name, type); return apiResponseMutableLiveData; } private void loadAccounts(String max_id, String name, accountFetch type) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java index 844238e..eefe9f8 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/ChannelsVM.java @@ -36,23 +36,19 @@ import app.fedilab.fedilabtube.sqlite.Sqlite; public class ChannelsVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public ChannelsVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData get() { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - getChannels(); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + getChannels(); return apiResponseMutableLiveData; } private void getChannels() { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CommentVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CommentVM.java index e41de60..122da23 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CommentVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/CommentVM.java @@ -30,24 +30,20 @@ import app.fedilab.fedilabtube.client.PeertubeAPI; public class CommentVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public CommentVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData getComment(String instanceName, String videoId) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - getSingle(instanceName, videoId); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + getSingle(instanceName, videoId); return apiResponseMutableLiveData; } private void getSingle(String instanceName, String videoId) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI api = new PeertubeAPI(_mContext); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java index 7d168c2..62b36d3 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java @@ -38,18 +38,14 @@ import app.fedilab.fedilabtube.sqlite.Sqlite; public class FeedsVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public FeedsVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData getVideos(Type action, String max_id, String target, String forAccount) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - loadVideos(action, max_id, target, forAccount); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + loadVideos(action, max_id, target, forAccount); return apiResponseMutableLiveData; } @@ -67,7 +63,7 @@ public class FeedsVM extends AndroidViewModel { } private void update(Peertube peertube) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); @@ -85,7 +81,7 @@ public class FeedsVM extends AndroidViewModel { private void getSingle(String instanceName, String videoId) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); @@ -107,7 +103,7 @@ public class FeedsVM extends AndroidViewModel { } private void loadVideos(Type action, String max_id, String target, String forAccount) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java index 6ea23c0..ac4733f 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java @@ -32,23 +32,19 @@ import app.fedilab.fedilabtube.client.entities.Error; public class NotificationsVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public NotificationsVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData getNotifications(Account account, String max_id) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - loadNotifications(account, max_id); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + loadNotifications(account, max_id); return apiResponseMutableLiveData; } private void loadNotifications(Account account, String max_id) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI api; diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java index d078b1f..5bbc499 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java @@ -39,23 +39,19 @@ import app.fedilab.fedilabtube.sqlite.Sqlite; public class PlaylistsVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public PlaylistsVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData manage(action apiAction, Playlist playlist, String videoId, String max_id) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - managePlaylists(apiAction, playlist, videoId, max_id); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + managePlaylists(apiAction, playlist, videoId, max_id); return apiResponseMutableLiveData; } private void managePlaylists(action apiAction, Playlist playlist, String videoId, String max_id) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PostActionsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PostActionsVM.java index 5c05ce8..c82f012 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PostActionsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PostActionsVM.java @@ -30,23 +30,19 @@ import app.fedilab.fedilabtube.client.PeertubeAPI; public class PostActionsVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public PostActionsVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData post(PeertubeAPI.StatusAction apiAction, String targetedId, String comment, String targetedComment) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - makeAction(apiAction, targetedId, comment, targetedComment); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + makeAction(apiAction, targetedId, comment, targetedComment); return apiResponseMutableLiveData; } private void makeAction(PeertubeAPI.StatusAction apiAction, String targetedId, String comment, String targetedComment) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java index b253f69..a8dc01c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/RelationshipVM.java @@ -34,23 +34,19 @@ import app.fedilab.fedilabtube.client.entities.Relationship; public class RelationshipVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public RelationshipVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData get(String targetedId) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - getRelationShip(targetedId); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + getRelationShip(targetedId); return apiResponseMutableLiveData; } private void getRelationShip(String targetedId) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI api = new PeertubeAPI(_mContext); diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/SearchVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/SearchVM.java index 38df4af..af0c0bc 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/SearchVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/SearchVM.java @@ -30,23 +30,19 @@ import app.fedilab.fedilabtube.client.PeertubeAPI; public class SearchVM extends AndroidViewModel { private MutableLiveData apiResponseMutableLiveData; - private Application application; public SearchVM(@NonNull Application application) { super(application); - this.application = application; } public LiveData getVideos(String max_id, String query) { - if (apiResponseMutableLiveData == null) { - apiResponseMutableLiveData = new MutableLiveData<>(); - loadVideos(max_id, query); - } + apiResponseMutableLiveData = new MutableLiveData<>(); + loadVideos(max_id, query); return apiResponseMutableLiveData; } private void loadVideos(String max_id, String query) { - Context _mContext = this.application.getApplicationContext(); + Context _mContext = getApplication().getApplicationContext(); new Thread(() -> { try { PeertubeAPI api = new PeertubeAPI(_mContext);