diff --git a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java index 2f29b24..1468a4a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/LoginActivity.java @@ -239,7 +239,7 @@ public class LoginActivity extends AppCompatActivity { try { String responseLogin = new HttpsConnection(LoginActivity.this).post("https://" + finalInstance + oauthUrl, 30, parameters, null); proceedLogin(responseLogin, host); - }catch (final Exception e2) { + } catch (final Exception e2) { e2.printStackTrace(); runOnUiThread(() -> { connectionButton.setEnabled(true); @@ -279,7 +279,7 @@ public class LoginActivity extends AppCompatActivity { }); } - private void proceedLogin(String responseLogin, String host){ + private void proceedLogin(String responseLogin, String host) { runOnUiThread(() -> { JSONObject resobjLogin; try { diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 17ae74b..f58ede8 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -41,7 +41,6 @@ import org.jetbrains.annotations.NotNull; import java.util.LinkedHashMap; -import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeInformationAsyncTask; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Account; @@ -49,6 +48,7 @@ import app.fedilab.fedilabtube.client.entities.PeertubeInformation; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.sqlite.AccountDAO; import app.fedilab.fedilabtube.sqlite.Sqlite; +import app.fedilab.fedilabtube.viewmodel.FeedsVM; import static app.fedilab.fedilabtube.asynctasks.RetrievePeertubeInformationAsyncTask.peertubeInformation; import static app.fedilab.fedilabtube.helper.Helper.academies; @@ -188,14 +188,14 @@ public class MainActivity extends AppCompatActivity { } else if (item.getItemId() == R.id.action_myvideos) { Intent intent = new Intent(MainActivity.this, MyVideosActivity.class); Bundle bundle = new Bundle(); - bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.MYVIDEOS); + bundle.putSerializable("type", FeedsVM.Type.MYVIDEOS); intent.putExtras(bundle); startActivity(intent); return true; } else if (item.getItemId() == R.id.action_history) { Intent intent = new Intent(MainActivity.this, MyVideosActivity.class); Bundle bundle = new Bundle(); - bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.PEERTUBE_HISTORY); + bundle.putSerializable("type", FeedsVM.Type.PEERTUBE_HISTORY); intent.putExtras(bundle); startActivity(intent); return true; diff --git a/app/src/main/java/app/fedilab/fedilabtube/MyVideosActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MyVideosActivity.java index a0f12d9..f2196d8 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MyVideosActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MyVideosActivity.java @@ -20,15 +20,15 @@ import android.view.MenuItem; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentTransaction; -import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.fragment.DisplayStatusFragment; import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface; +import app.fedilab.fedilabtube.viewmodel.FeedsVM; public class MyVideosActivity extends AppCompatActivity implements OnRetrieveFeedsInterface { - private RetrieveFeedsAsyncTask.Type type; + private FeedsVM.Type type; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,13 +41,13 @@ public class MyVideosActivity extends AppCompatActivity implements OnRetrieveFee Bundle b = getIntent().getExtras(); if (b != null) - type = (RetrieveFeedsAsyncTask.Type) b.get("type"); + type = (FeedsVM.Type) b.get("type"); - if (type == RetrieveFeedsAsyncTask.Type.MYVIDEOS) { + if (type == FeedsVM.Type.MYVIDEOS) { setTitle(R.string.my_videos); - } else if (type == RetrieveFeedsAsyncTask.Type.PSUBSCRIPTIONS) { + } else if (type == FeedsVM.Type.PSUBSCRIPTIONS) { setTitle(R.string.subscriptions); - } else if (type == RetrieveFeedsAsyncTask.Type.PEERTUBE_HISTORY) { + } else if (type == FeedsVM.Type.PEERTUBE_HISTORY) { setTitle(R.string.my_history); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java index 41300e5..b5b8780 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java @@ -52,7 +52,6 @@ import java.util.ArrayList; import java.util.List; import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask; -import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrieveRelationshipAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrieveSingleAccountAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; @@ -71,6 +70,7 @@ import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsAccountInterface; import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface; import app.fedilab.fedilabtube.interfaces.OnRetrieveRelationshipInterface; +import app.fedilab.fedilabtube.viewmodel.FeedsVM; import es.dmoral.toasty.Toasty; import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY; @@ -121,7 +121,7 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi } statuses = new ArrayList<>(); StatusDrawerParams statusDrawerParams = new StatusDrawerParams(); - statusDrawerParams.setType(RetrieveFeedsAsyncTask.Type.USER); + statusDrawerParams.setType(FeedsVM.Type.USER); statusDrawerParams.setTargetedId(accountId); statusDrawerParams.setStatuses(statuses); statusListAdapter = new StatusListAdapter(statusDrawerParams); @@ -465,7 +465,7 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi if (position == 0) { DisplayStatusFragment displayStatusFragment = new DisplayStatusFragment(); bundle = new Bundle(); - bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER); + bundle.putSerializable("type", FeedsVM.Type.USER); bundle.putString("targetedid", account.getAcct()); bundle.putBoolean("ischannel", ischannel); displayStatusFragment.setArguments(bundle); diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java deleted file mode 100644 index 5a2b670..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java +++ /dev/null @@ -1,158 +0,0 @@ -package app.fedilab.fedilabtube.asynctasks; -/* Copyright 2020 Thomas Schneider - * - * This file is a part of TubeLab - * - * This program is free software; you can redistribute it and/or modify it under the terms of the - * GNU General Public License as published by the Free Software Foundation; either version 3 of the - * License, or (at your option) any later version. - * - * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even - * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General - * Public License for more details. - * - * You should have received a copy of the GNU General Public License along with TubeLab; if not, - * see . */ - -import android.content.Context; -import android.database.sqlite.SQLiteDatabase; -import android.os.AsyncTask; - -import java.lang.ref.WeakReference; -import java.util.List; - -import app.fedilab.fedilabtube.client.APIResponse; -import app.fedilab.fedilabtube.client.PeertubeAPI; -import app.fedilab.fedilabtube.client.entities.Peertube; -import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface; -import app.fedilab.fedilabtube.sqlite.PeertubeFavoritesDAO; -import app.fedilab.fedilabtube.sqlite.Sqlite; - - -public class RetrieveFeedsAsyncTask extends AsyncTask { - - - private Type action; - private APIResponse apiResponse; - private String max_id; - private OnRetrieveFeedsInterface listener; - private WeakReference contextReference; - private String target; - private String forAccount; - - public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { - this.contextReference = new WeakReference<>(context); - this.action = action; - this.max_id = max_id; - this.listener = onRetrieveFeedsInterface; - this.target = null; - } - - public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, String target, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { - this.contextReference = new WeakReference<>(context); - this.action = action; - this.max_id = max_id; - this.listener = onRetrieveFeedsInterface; - this.target = target; - } - - public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, String target, String forAccount, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { - this.contextReference = new WeakReference<>(context); - this.action = action; - this.max_id = max_id; - this.listener = onRetrieveFeedsInterface; - this.target = target; - this.forAccount = forAccount; - } - - - @Override - protected Void doInBackground(Void... params) { - PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get()); - - if (action == null) - return null; - switch (action) { - case USER: - apiResponse = peertubeAPI.getVideos(target, max_id); - break; - case MYVIDEOS: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getMyVideos(max_id); - break; - case PEERTUBE_HISTORY: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getMyHistory(max_id); - break; - case CHANNEL: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getVideosChannel(target, max_id); - break; - - case CACHE_BOOKMARKS_PEERTUBE: - apiResponse = new APIResponse(); - SQLiteDatabase db = Sqlite.getInstance(contextReference.get().getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); - List peertubes = new PeertubeFavoritesDAO(contextReference.get(), db).getAllPeertube(); - apiResponse.setPeertubes(peertubes); - break; - case PSUBSCRIPTIONS: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - if (forAccount == null) { - apiResponse = peertubeAPI.getSubscriptionsTL(max_id); - } else { - apiResponse = peertubeAPI.getVideosChannel(forAccount, max_id); - } - break; - case POVERVIEW: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getOverviewTL(max_id); - break; - case PTRENDING: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getTrendingTL(max_id); - break; - case PRECENTLYADDED: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getRecentlyAddedTL(max_id); - break; - case PLOCAL: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getLocalTL(max_id); - break; - case PPUBLIC: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getPublicTL(max_id); - break; - case PMOSTLIKED: - peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getLikedTL(max_id); - break; - - } - return null; - } - - @Override - protected void onPostExecute(Void result) { - listener.onRetrieveFeeds(apiResponse); - } - - public enum Type { - - USER, - - PPUBLIC, - PSUBSCRIPTIONS, - POVERVIEW, - PTRENDING, - PRECENTLYADDED, - PMOSTLIKED, - PLOCAL, - CHANNEL, - MYVIDEOS, - PEERTUBE_HISTORY, - - CACHE_BOOKMARKS_PEERTUBE, - - } -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/Status.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/Status.java index 9fdbe49..61dd7c7 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/Status.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/Status.java @@ -49,9 +49,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import app.fedilab.fedilabtube.R; -import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.fedilabtube.helper.CustomQuoteSpan; import app.fedilab.fedilabtube.helper.Helper; +import app.fedilab.fedilabtube.viewmodel.FeedsVM; @SuppressWarnings("unused") @@ -88,7 +88,7 @@ public class Status implements Parcelable { private String language; private String content; private SpannableString contentSpan; - private transient RetrieveFeedsAsyncTask.Type type; + private transient FeedsVM.Type type; private String conversationId; private String contentType; @@ -118,7 +118,7 @@ public class Status implements Parcelable { this.content = in.readString(); this.contentSpan = (SpannableString) TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); int tmpType = in.readInt(); - this.type = tmpType == -1 ? null : RetrieveFeedsAsyncTask.Type.values()[tmpType]; + this.type = tmpType == -1 ? null : FeedsVM.Type.values()[tmpType]; this.conversationId = in.readString(); this.contentType = in.readString(); } @@ -620,11 +620,11 @@ public class Status implements Parcelable { this.replies_count = replies_count; } - public RetrieveFeedsAsyncTask.Type getType() { + public FeedsVM.Type getType() { return type; } - public void setType(RetrieveFeedsAsyncTask.Type type) { + public void setType(FeedsVM.Type type) { this.type = type; } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/StatusDrawerParams.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/StatusDrawerParams.java index e94978c..d894ac0 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/StatusDrawerParams.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/StatusDrawerParams.java @@ -16,12 +16,12 @@ package app.fedilab.fedilabtube.client.entities; import java.util.List; -import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask; +import app.fedilab.fedilabtube.viewmodel.FeedsVM; @SuppressWarnings("unused") public class StatusDrawerParams { private List statuses; - private RetrieveFeedsAsyncTask.Type type; + private FeedsVM.Type type; private String targetedId; private int position; @@ -34,11 +34,11 @@ public class StatusDrawerParams { this.statuses = statuses; } - public RetrieveFeedsAsyncTask.Type getType() { + public FeedsVM.Type getType() { return type; } - public void setType(RetrieveFeedsAsyncTask.Type type) { + public void setType(FeedsVM.Type type) { this.type = type; } 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 c194832..3966a6e 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java @@ -33,6 +33,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.GridLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -45,7 +46,6 @@ import java.util.List; import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask; -import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask; import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeSearchAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.PeertubeAPI; @@ -58,10 +58,11 @@ import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.interfaces.OnPostActionInterface; import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface; +import app.fedilab.fedilabtube.viewmodel.FeedsVM; import es.dmoral.toasty.Toasty; -import static app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask.Type.POVERVIEW; -import static app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask.Type.PSUBSCRIPTIONS; +import static app.fedilab.fedilabtube.viewmodel.FeedsVM.Type.POVERVIEW; +import static app.fedilab.fedilabtube.viewmodel.FeedsVM.Type.PSUBSCRIPTIONS; public class DisplayStatusFragment extends Fragment implements OnPostActionInterface, OnRetrieveFeedsInterface, OnRetrieveAccountsInterface, AccountsHorizontalListAdapter.EventListener { @@ -77,7 +78,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter private String max_id, max_id_accounts; private List peertubes; private List accounts; - private RetrieveFeedsAsyncTask.Type type; + private FeedsVM.Type type; private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private boolean firstLoad; private SwipeRefreshLayout swipeRefreshLayout; @@ -110,7 +111,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter search_peertube = bundle.getString("search_peertube", null); targetedId = bundle.getString("targetedid", null); ischannel = bundle.getBoolean("ischannel", false); - type = (RetrieveFeedsAsyncTask.Type) bundle.get("type"); + type = (FeedsVM.Type) bundle.get("type"); } if (getArguments() != null && type == null) { @@ -119,7 +120,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter if (ischannel) { - type = RetrieveFeedsAsyncTask.Type.CHANNEL; + type = FeedsVM.Type.CHANNEL; } forAccount = null; @@ -169,7 +170,8 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter if (context != null) { //Load data depending of the value if (search_peertube == null) { //Not a Peertube search - asyncTask = new RetrieveFeedsAsyncTask(context, type, "0", targetedId, DisplayStatusFragment.this).execute(); + FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIew); } else { asyncTask = new RetrievePeertubeSearchAsyncTask(context, "0", search_peertube, DisplayStatusFragment.this).execute(); } @@ -177,7 +179,10 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter new Handler(Looper.getMainLooper()).postDelayed(() -> { if (context != null) { if (search_peertube == null) { //Not a Peertube search - asyncTask = new RetrieveFeedsAsyncTask(context, type, "0", targetedId, DisplayStatusFragment.this).execute(); + if (getActivity() != null) { + FeedsVM viewModel = new ViewModelProvider(getActivity()).get(FeedsVM.class); + viewModel.getVideos(type, "0", targetedId, null).observe(getActivity(), this::manageVIew); + } } else { asyncTask = new RetrievePeertubeSearchAsyncTask(context, "0", search_peertube, DisplayStatusFragment.this).execute(); } @@ -227,7 +232,8 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter if (!flag_loading) { flag_loading = true; if (search_peertube == null) { //Not a Peertube search - asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).execute(); + FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIew(apiResponse)); } else { asyncTask = new RetrievePeertubeSearchAsyncTask(context, max_id, search_peertube, DisplayStatusFragment.this).execute(); } @@ -246,7 +252,8 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter if (!flag_loading) { flag_loading = true; if (search_peertube == null) { //Not a Peertube search - asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).execute(); + FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + viewModel.getVideos(type, max_id, null, null).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageVIew(apiResponse)); } else { asyncTask = new RetrievePeertubeSearchAsyncTask(context, max_id, search_peertube, DisplayStatusFragment.this).execute(); } @@ -332,6 +339,50 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter } } + + private void manageVIew(APIResponse apiResponse) { + //hide loaders + mainLoader.setVisibility(View.GONE); + nextElementLoader.setVisibility(View.GONE); + //handle other API error but discards 404 - error which can often happen due to toots which have been deleted + if (this.peertubes == null || apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError().getStatusCode() != 501)) { + if (apiResponse == null) + Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show(); + else { + Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); + } + swipeRefreshLayout.setRefreshing(false); + flag_loading = false; + return; + } + int previousPosition = this.peertubes.size(); + if (max_id == null) + max_id = "0"; + //max_id needs to work like an offset + int tootPerPage = sharedpreferences.getInt(Helper.SET_VIDEOS_PER_PAGE, Helper.VIDEOS_PER_PAGE); + max_id = String.valueOf(Integer.parseInt(max_id) + tootPerPage); + if (apiResponse.getPeertubes() == null) { + return; + } + this.peertubes.addAll(apiResponse.getPeertubes()); + //If no item were inserted previously the adapter is created + if (previousPosition == 0) { + peertubeAdapater = new PeertubeAdapter(this.peertubes); + lv_status.setAdapter(peertubeAdapater); + } else + peertubeAdapater.notifyItemRangeInserted(previousPosition, apiResponse.getPeertubes().size()); + //remove handlers + swipeRefreshLayout.setRefreshing(false); + textviewNoAction.setVisibility(View.GONE); + if (firstLoad && (apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0)) { + textviewNoActionText.setText(R.string.no_video_to_display); + textviewNoAction.setVisibility(View.VISIBLE); + } + flag_loading = false; + firstLoad = false; + } + + @Override public void onRetrieveFeeds(APIResponse apiResponse) { //hide loaders @@ -417,7 +468,8 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter accountsHorizontalListAdapter.notifyItemRangeRemoved(0, accounts.size()); } if (search_peertube == null) { //Not a Peertube search - asyncTask = new RetrieveFeedsAsyncTask(context, type, "0", targetedId, forAccount, DisplayStatusFragment.this).execute(); + FeedsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(FeedsVM.class); + viewModel.getVideos(type, "0", targetedId, null).observe(DisplayStatusFragment.this.requireActivity(), this::manageVIew); } else { asyncTask = new RetrievePeertubeSearchAsyncTask(context, "0", search_peertube, DisplayStatusFragment.this).execute(); } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java new file mode 100644 index 0000000..d93d2a5 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/FeedsVM.java @@ -0,0 +1,140 @@ +package app.fedilab.fedilabtube.viewmodel; +/* Copyright 2020 Thomas Schneider + * + * This file is a part of TubeLab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with TubeLab; if not, + * see . */ + +import android.app.Application; +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; + +import java.util.List; + +import app.fedilab.fedilabtube.client.APIResponse; +import app.fedilab.fedilabtube.client.PeertubeAPI; +import app.fedilab.fedilabtube.client.entities.Peertube; +import app.fedilab.fedilabtube.sqlite.PeertubeFavoritesDAO; +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); + } + return apiResponseMutableLiveData; + } + + private void loadVideos(Type action, String max_id, String target, String forAccount) { + Context _mContext = this.application.getApplicationContext(); + new Thread(() -> { + try { + PeertubeAPI peertubeAPI = new PeertubeAPI(_mContext); + if (action == null) + return; + APIResponse apiResponse = null; + switch (action) { + case USER: + apiResponse = peertubeAPI.getVideos(target, max_id); + break; + case MYVIDEOS: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getMyVideos(max_id); + break; + case PEERTUBE_HISTORY: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getMyHistory(max_id); + break; + case CHANNEL: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getVideosChannel(target, max_id); + break; + + case CACHE_BOOKMARKS_PEERTUBE: + apiResponse = new APIResponse(); + SQLiteDatabase db = Sqlite.getInstance(_mContext, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + List peertubes = new PeertubeFavoritesDAO(_mContext, db).getAllPeertube(); + apiResponse.setPeertubes(peertubes); + break; + case PSUBSCRIPTIONS: + peertubeAPI = new PeertubeAPI(_mContext); + if (forAccount == null) { + apiResponse = peertubeAPI.getSubscriptionsTL(max_id); + } else { + apiResponse = peertubeAPI.getVideosChannel(forAccount, max_id); + } + break; + case POVERVIEW: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getOverviewTL(max_id); + break; + case PTRENDING: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getTrendingTL(max_id); + break; + case PRECENTLYADDED: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getRecentlyAddedTL(max_id); + break; + case PLOCAL: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getLocalTL(max_id); + break; + case PPUBLIC: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getPublicTL(max_id); + break; + case PMOSTLIKED: + peertubeAPI = new PeertubeAPI(_mContext); + apiResponse = peertubeAPI.getLikedTL(max_id); + break; + + } + apiResponseMutableLiveData.setValue(apiResponse); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + } + + public enum Type { + + USER, + PPUBLIC, + PSUBSCRIPTIONS, + POVERVIEW, + PTRENDING, + PRECENTLYADDED, + PMOSTLIKED, + PLOCAL, + CHANNEL, + MYVIDEOS, + PEERTUBE_HISTORY, + CACHE_BOOKMARKS_PEERTUBE, + + } +}