From f632aa471259b6cc8aad3e5eb981d1adb6eccf90 Mon Sep 17 00:00:00 2001 From: Thomas Date: Mon, 7 Sep 2020 15:03:22 +0200 Subject: [PATCH] VM-v3 --- .../fedilab/fedilabtube/AccountActivity.java | 6 +- .../asynctasks/RetrieveAccountsAsyncTask.java | 66 ---------------- ...etrievePeertubeNotificationsAsyncTask.java | 69 ----------------- .../drawer/AccountsListAdapter.java | 12 +-- .../fragment/DisplayAccountsFragment.java | 47 ++++++------ .../DisplayNotificationsFragment.java | 24 +++--- .../fragment/DisplayStatusFragment.java | 4 +- .../fedilabtube/viewmodel/AccountsVM.java | 14 +++- .../viewmodel/NotificationsVM.java | 75 +++++++++++++++++++ 9 files changed, 131 insertions(+), 186 deletions(-) delete mode 100644 app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveAccountsAsyncTask.java delete mode 100644 app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeNotificationsAsyncTask.java create mode 100644 app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java diff --git a/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java index b3c351d..2204d22 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/AccountActivity.java @@ -40,13 +40,13 @@ import com.google.android.material.tabs.TabLayout; import org.jetbrains.annotations.NotNull; -import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask; import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment; import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.sqlite.AccountDAO; import app.fedilab.fedilabtube.sqlite.Sqlite; +import app.fedilab.fedilabtube.viewmodel.AccountsVM; public class AccountActivity extends AppCompatActivity { @@ -228,7 +228,7 @@ public class AccountActivity extends AppCompatActivity { case 2: DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment(); if (position == 1) { - bundle.putSerializable("accountFetch", RetrieveAccountsAsyncTask.accountFetch.MUTED); + bundle.putSerializable("accountFetch", AccountsVM.accountFetch.MUTED); } else { SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); @@ -236,7 +236,7 @@ public class AccountActivity extends AppCompatActivity { String instance = Helper.getLiveInstance(AccountActivity.this); Account account = new AccountDAO(AccountActivity.this, db).getUniqAccount(userId, instance); bundle.putString("name", account.getUsername() + "@" + account.getInstance()); - bundle.putSerializable("accountFetch", RetrieveAccountsAsyncTask.accountFetch.CHANNEL); + bundle.putSerializable("accountFetch", AccountsVM.accountFetch.CHANNEL); } displayAccountsFragment.setArguments(bundle); return displayAccountsFragment; diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveAccountsAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveAccountsAsyncTask.java deleted file mode 100644 index 2cb5303..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveAccountsAsyncTask.java +++ /dev/null @@ -1,66 +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.os.AsyncTask; - -import java.lang.ref.WeakReference; - -import app.fedilab.fedilabtube.client.APIResponse; -import app.fedilab.fedilabtube.client.PeertubeAPI; -import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; - - -public class RetrieveAccountsAsyncTask extends AsyncTask { - - private OnRetrieveAccountsInterface listener; - private WeakReference contextReference; - private String max_id; - private accountFetch type; - - public RetrieveAccountsAsyncTask(Context context, String max_id, accountFetch type, OnRetrieveAccountsInterface onRetrieveAccountsInterface) { - this.contextReference = new WeakReference<>(context); - this.max_id = max_id; - this.listener = onRetrieveAccountsInterface; - this.type = type; - } - - @Override - protected APIResponse doInBackground(Void... params) { - PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get()); - if (type == accountFetch.SUBSCRIPTION) { - return peertubeAPI.getSubscriptionUsers(max_id); - } else if (type == accountFetch.MUTED) { - return peertubeAPI.getMuted(max_id); - } else if (type == accountFetch.CHANNEL) { - return peertubeAPI.getPeertubeChannel(max_id); - } else { - return null; - } - } - - @Override - protected void onPostExecute(APIResponse apiResponse) { - listener.onRetrieveAccounts(apiResponse); - } - - public enum accountFetch { - SUBSCRIPTION, - CHANNEL, - MUTED - } - -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeNotificationsAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeNotificationsAsyncTask.java deleted file mode 100644 index 55a249c..0000000 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrievePeertubeNotificationsAsyncTask.java +++ /dev/null @@ -1,69 +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.os.AsyncTask; - -import java.lang.ref.WeakReference; - -import app.fedilab.fedilabtube.client.APIResponse; -import app.fedilab.fedilabtube.client.PeertubeAPI; -import app.fedilab.fedilabtube.client.entities.Account; -import app.fedilab.fedilabtube.client.entities.Error; -import app.fedilab.fedilabtube.interfaces.OnRetrievePeertubeNotificationsInterface; - - -public class RetrievePeertubeNotificationsAsyncTask extends AsyncTask { - - - private String max_id; - private Account account; - private OnRetrievePeertubeNotificationsInterface listener; - private WeakReference contextReference; - - public RetrievePeertubeNotificationsAsyncTask(Context context, Account account, String max_id, OnRetrievePeertubeNotificationsInterface onRetrievePeertubeNotificationsInterface) { - this.contextReference = new WeakReference<>(context); - this.max_id = max_id; - this.listener = onRetrievePeertubeNotificationsInterface; - this.account = account; - } - - - @Override - protected APIResponse doInBackground(Void... params) { - PeertubeAPI api; - APIResponse apiResponse; - if (account == null) { - api = new PeertubeAPI(this.contextReference.get()); - apiResponse = api.getNotifications(max_id); - } else { - if (this.contextReference.get() == null) { - apiResponse = new APIResponse(); - apiResponse.setError(new Error()); - return apiResponse; - } - api = new PeertubeAPI(this.contextReference.get(), account.getInstance(), account.getToken()); - apiResponse = api.getNotificationsSince(max_id); - } - return apiResponse; - } - - @Override - protected void onPostExecute(APIResponse apiResponse) { - listener.onRetrievePeertubeNotifications(apiResponse, account); - } - -} diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsListAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsListAdapter.java index 5bf92c7..79178f7 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsListAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsListAdapter.java @@ -43,7 +43,6 @@ import java.util.List; import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.ShowAccountActivity; import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask; -import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask; import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Account; @@ -51,6 +50,7 @@ import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.fragment.DisplayPlaylistsFragment; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.interfaces.OnPostActionInterface; +import app.fedilab.fedilabtube.viewmodel.AccountsVM; import es.dmoral.toasty.Toasty; @@ -60,9 +60,9 @@ public class AccountsListAdapter extends RecyclerView.Adapter accounts; private Context context; private AccountsListAdapter accountsListAdapter; - private RetrieveAccountsAsyncTask.accountFetch type; + private AccountsVM.accountFetch type; - public AccountsListAdapter(RetrieveAccountsAsyncTask.accountFetch type, List accounts) { + public AccountsListAdapter(AccountsVM.accountFetch type, List accounts) { this.accounts = accounts; this.accountsListAdapter = this; this.type = type; @@ -80,7 +80,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter dialog.dismiss()) .show(); }); - } else if (type == RetrieveAccountsAsyncTask.accountFetch.MUTED) { + } else if (type == AccountsVM.accountFetch.MUTED) { holder.account_action.setOnClickListener(v -> new PostActionAsyncTask(context, PeertubeAPI.StatusAction.UNMUTE, account.getAcct(), AccountsListAdapter.this).execute()); } else { holder.account_action.hide(); @@ -159,7 +159,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter asyncTask; private AccountsListAdapter accountsListAdapter; private String max_id; private List accounts; @@ -73,7 +70,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou private boolean swiped; private RecyclerView lv_accounts; private View rootView; - private RetrieveAccountsAsyncTask.accountFetch accountFetch; + private AccountsVM.accountFetch accountFetch; private FloatingActionButton action_button; @Override @@ -86,7 +83,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou accounts = new ArrayList<>(); if (bundle != null) { if (bundle.containsKey("accountFetch")) { - accountFetch = (RetrieveAccountsAsyncTask.accountFetch) bundle.getSerializable("accountFetch"); + accountFetch = (AccountsVM.accountFetch) bundle.getSerializable("accountFetch"); } name = bundle.getString("name", null); } @@ -184,7 +181,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou accountsListAdapter.allAccountsRemoved = this; lv_accounts.setAdapter(accountsListAdapter); TextView no_action_text = rootView.findViewById(R.id.no_action_text); - if (accountFetch == RetrieveAccountsAsyncTask.accountFetch.MUTED) { + if (accountFetch == AccountsVM.accountFetch.MUTED) { no_action_text.setText(context.getString(R.string.no_muted)); } final LinearLayoutManager mLayoutManager; @@ -200,10 +197,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou if (!flag_loading) { flag_loading = true; if (accountFetch == null) { - asyncTask = new RetrieveSingleAccountAsyncTask(context, name, RetrieveSingleAccountAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); + viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); } else { - String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : max_id; - asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : max_id; + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); + viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); } nextElementLoader.setVisibility(View.VISIBLE); } @@ -216,10 +215,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); if (accountFetch == null) { - asyncTask = new RetrieveSingleAccountAsyncTask(context, name, RetrieveSingleAccountAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); + viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } else { - String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : null; - asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null; + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); + viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } return rootView; } @@ -227,7 +228,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou @Override public void onResume() { super.onResume(); - if (accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL) { + if (accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL) { action_button.setVisibility(View.VISIBLE); } else { action_button.setVisibility(View.GONE); @@ -252,10 +253,9 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou this.context = context; } + @Override public void onDestroy() { super.onDestroy(); - if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING) - asyncTask.cancel(true); } public void scrollToTop() { @@ -263,8 +263,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou lv_accounts.setAdapter(accountsListAdapter); } - @Override - public void onRetrieveAccounts(APIResponse apiResponse) { + private void manageViewAccounts(APIResponse apiResponse) { mainLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE); if (apiResponse.getError() != null) { @@ -306,10 +305,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou swiped = true; swipeRefreshLayout.setRefreshing(true); if (accountFetch == null) { - asyncTask = new RetrieveSingleAccountAsyncTask(context, name, RetrieveSingleAccountAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); + viewModel.getAccounts(null, name, AccountsVM.accountFetch.SINGLE_ACCOUNT).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } else { - String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : null; - asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null; + AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class); + viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java index 14dab02..0eca363 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayNotificationsFragment.java @@ -15,7 +15,6 @@ package app.fedilab.fedilabtube.fragment; * see . */ import android.content.Context; -import android.os.AsyncTask; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; @@ -25,6 +24,7 @@ import android.widget.Toast; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; +import androidx.lifecycle.ViewModelProvider; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -34,20 +34,17 @@ import java.util.ArrayList; import java.util.List; import app.fedilab.fedilabtube.R; -import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeNotificationsAsyncTask; import app.fedilab.fedilabtube.client.APIResponse; -import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.PeertubeNotification; import app.fedilab.fedilabtube.drawer.PeertubeNotificationsListAdapter; -import app.fedilab.fedilabtube.interfaces.OnRetrievePeertubeNotificationsInterface; +import app.fedilab.fedilabtube.viewmodel.NotificationsVM; import es.dmoral.toasty.Toasty; -public class DisplayNotificationsFragment extends Fragment implements OnRetrievePeertubeNotificationsInterface { +public class DisplayNotificationsFragment extends Fragment { private boolean flag_loading; private Context context; - private AsyncTask asyncTask; private PeertubeNotificationsListAdapter peertubeNotificationsListAdapter; private String max_id; private List notifications; @@ -95,7 +92,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve if (firstVisibleItem + visibleItemCount == totalItemCount) { if (!flag_loading) { flag_loading = true; - asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this.requireActivity()).get(NotificationsVM.class); + viewModel.getNotifications(null, max_id).observe(DisplayNotificationsFragment.this.requireActivity(), apiResponse -> manageVIewNotifications(apiResponse)); nextElementLoader.setVisibility(View.VISIBLE); } } else { @@ -106,7 +104,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve }); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); - asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this.requireActivity()).get(NotificationsVM.class); + viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications); return rootView; } @@ -130,8 +129,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve public void onDestroy() { super.onDestroy(); - if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING) - asyncTask.cancel(true); } public void scrollToTop() { @@ -147,11 +144,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve flag_loading = true; swiped = true; swipeRefreshLayout.setRefreshing(true); - asyncTask = new RetrievePeertubeNotificationsAsyncTask(context, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + NotificationsVM viewModel = new ViewModelProvider(DisplayNotificationsFragment.this.requireActivity()).get(NotificationsVM.class); + viewModel.getNotifications(null, null).observe(DisplayNotificationsFragment.this.requireActivity(), this::manageVIewNotifications); + } - @Override - public void onRetrievePeertubeNotifications(APIResponse apiResponse, Account account) { + private void manageVIewNotifications(APIResponse apiResponse) { mainLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE); if (apiResponse.getError() != null) { 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 1292d29..90e4e65 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java +++ b/app/src/main/java/app/fedilab/fedilabtube/fragment/DisplayStatusFragment.java @@ -179,7 +179,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter int totalItemCount = mLayoutManager.getItemCount(); if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) { AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class); - viewModel.getAccounts(max_id_accounts, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); + viewModel.getAccounts(max_id_accounts, null, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), apiResponse -> manageViewAccounts(apiResponse)); } } } @@ -253,7 +253,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter } if (type == PSUBSCRIPTIONS) { AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class); - viewModel.getAccounts(max_id, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), this::manageViewAccounts); + viewModel.getAccounts(max_id, null, AccountsVM.accountFetch.SUBSCRIPTION).observe(DisplayStatusFragment.this.requireActivity(), this::manageViewAccounts); } display_all.setOnClickListener(v -> { 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 36ed908..e7a86f2 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/AccountsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/AccountsVM.java @@ -37,15 +37,15 @@ public class AccountsVM extends AndroidViewModel { this.application = application; } - public LiveData getAccounts(String max_id, accountFetch type) { + public LiveData getAccounts(String max_id, String name, accountFetch type) { if (apiResponseMutableLiveData == null) { apiResponseMutableLiveData = new MutableLiveData<>(); - loadAccounts(max_id, type); + loadAccounts(max_id, name, type); } return apiResponseMutableLiveData; } - private void loadAccounts(String max_id, accountFetch type) { + private void loadAccounts(String max_id, String name, accountFetch type) { Context _mContext = this.application.getApplicationContext(); new Thread(() -> { try { @@ -57,6 +57,10 @@ public class AccountsVM extends AndroidViewModel { apiResponse = peertubeAPI.getMuted(max_id); } else if (type == accountFetch.CHANNEL) { apiResponse = peertubeAPI.getPeertubeChannel(max_id); + } else if (type == accountFetch.SINGLE_ACCOUNT) { + apiResponse = peertubeAPI.getPeertubeChannel(name); + } else if (type == accountFetch.SINGLE_CHANNEL) { + apiResponse = peertubeAPI.getPeertubeChannelInfo(name); } Handler mainHandler = new Handler(Looper.getMainLooper()); APIResponse finalApiResponse = apiResponse; @@ -71,6 +75,8 @@ public class AccountsVM extends AndroidViewModel { public enum accountFetch { SUBSCRIPTION, CHANNEL, - MUTED + MUTED, + SINGLE_ACCOUNT, + SINGLE_CHANNEL } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java new file mode 100644 index 0000000..1279f05 --- /dev/null +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/NotificationsVM.java @@ -0,0 +1,75 @@ +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.os.Handler; +import android.os.Looper; + +import androidx.annotation.NonNull; +import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; + +import app.fedilab.fedilabtube.client.APIResponse; +import app.fedilab.fedilabtube.client.PeertubeAPI; +import app.fedilab.fedilabtube.client.entities.Account; +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) { + apiResponseMutableLiveData = new MutableLiveData<>(); + loadNotifications(account, max_id); + return apiResponseMutableLiveData; + } + + private void loadNotifications(Account account, String max_id) { + Context _mContext = this.application.getApplicationContext(); + new Thread(() -> { + try { + PeertubeAPI api; + APIResponse apiResponse; + if (account == null) { + api = new PeertubeAPI(_mContext); + apiResponse = api.getNotifications(max_id); + } else { + if (_mContext == null) { + apiResponse = new APIResponse(); + apiResponse.setError(new Error()); + } + api = new PeertubeAPI(_mContext, account.getInstance(), account.getToken()); + apiResponse = api.getNotificationsSince(max_id); + } + Handler mainHandler = new Handler(Looper.getMainLooper()); + APIResponse finalApiResponse = apiResponse; + Runnable myRunnable = () -> apiResponseMutableLiveData.setValue(finalApiResponse); + mainHandler.post(myRunnable); + } catch (Exception e) { + e.printStackTrace(); + } + }).start(); + } + +}