1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-04-12 17:51:32 +02:00
This commit is contained in:
Thomas 2020-09-07 15:03:22 +02:00
parent 81de1c1aa0
commit f632aa4712
9 changed files with 131 additions and 186 deletions

View File

@ -40,13 +40,13 @@ import com.google.android.material.tabs.TabLayout;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment; import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment;
import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment; import app.fedilab.fedilabtube.fragment.DisplayNotificationsFragment;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.sqlite.AccountDAO; import app.fedilab.fedilabtube.sqlite.AccountDAO;
import app.fedilab.fedilabtube.sqlite.Sqlite; import app.fedilab.fedilabtube.sqlite.Sqlite;
import app.fedilab.fedilabtube.viewmodel.AccountsVM;
public class AccountActivity extends AppCompatActivity { public class AccountActivity extends AppCompatActivity {
@ -228,7 +228,7 @@ public class AccountActivity extends AppCompatActivity {
case 2: case 2:
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment(); DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
if (position == 1) { if (position == 1) {
bundle.putSerializable("accountFetch", RetrieveAccountsAsyncTask.accountFetch.MUTED); bundle.putSerializable("accountFetch", AccountsVM.accountFetch.MUTED);
} else { } else {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); 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); String instance = Helper.getLiveInstance(AccountActivity.this);
Account account = new AccountDAO(AccountActivity.this, db).getUniqAccount(userId, instance); Account account = new AccountDAO(AccountActivity.this, db).getUniqAccount(userId, instance);
bundle.putString("name", account.getUsername() + "@" + account.getInstance()); bundle.putString("name", account.getUsername() + "@" + account.getInstance());
bundle.putSerializable("accountFetch", RetrieveAccountsAsyncTask.accountFetch.CHANNEL); bundle.putSerializable("accountFetch", AccountsVM.accountFetch.CHANNEL);
} }
displayAccountsFragment.setArguments(bundle); displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment; return displayAccountsFragment;

View File

@ -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 <http://www.gnu.org/licenses>. */
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<Void, Void, APIResponse> {
private OnRetrieveAccountsInterface listener;
private WeakReference<Context> 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
}
}

View File

@ -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 <http://www.gnu.org/licenses>. */
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<Void, Void, APIResponse> {
private String max_id;
private Account account;
private OnRetrievePeertubeNotificationsInterface listener;
private WeakReference<Context> 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);
}
}

View File

@ -43,7 +43,6 @@ import java.util.List;
import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.ShowAccountActivity; import app.fedilab.fedilabtube.ShowAccountActivity;
import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask; import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Account; 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.fragment.DisplayPlaylistsFragment;
import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.interfaces.OnPostActionInterface; import app.fedilab.fedilabtube.interfaces.OnPostActionInterface;
import app.fedilab.fedilabtube.viewmodel.AccountsVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
@ -60,9 +60,9 @@ public class AccountsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
private List<Account> accounts; private List<Account> accounts;
private Context context; private Context context;
private AccountsListAdapter accountsListAdapter; private AccountsListAdapter accountsListAdapter;
private RetrieveAccountsAsyncTask.accountFetch type; private AccountsVM.accountFetch type;
public AccountsListAdapter(RetrieveAccountsAsyncTask.accountFetch type, List<Account> accounts) { public AccountsListAdapter(AccountsVM.accountFetch type, List<Account> accounts) {
this.accounts = accounts; this.accounts = accounts;
this.accountsListAdapter = this; this.accountsListAdapter = this;
this.type = type; this.type = type;
@ -80,7 +80,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
final AccountsListAdapter.ViewHolder holder = (AccountsListAdapter.ViewHolder) viewHolder; final AccountsListAdapter.ViewHolder holder = (AccountsListAdapter.ViewHolder) viewHolder;
final Account account = accounts.get(position); final Account account = accounts.get(position);
if (type == RetrieveAccountsAsyncTask.accountFetch.CHANNEL) { if (type == AccountsVM.accountFetch.CHANNEL) {
holder.account_action.show(); holder.account_action.show();
holder.account_action.setImageResource(R.drawable.ic_baseline_delete_24); holder.account_action.setImageResource(R.drawable.ic_baseline_delete_24);
holder.account_action.setContentDescription(context.getString(R.string.delete_channel)); holder.account_action.setContentDescription(context.getString(R.string.delete_channel));
@ -127,7 +127,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
.setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss()) .setNegativeButton(R.string.no, (dialog, which) -> dialog.dismiss())
.show(); .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()); holder.account_action.setOnClickListener(v -> new PostActionAsyncTask(context, PeertubeAPI.StatusAction.UNMUTE, account.getAcct(), AccountsListAdapter.this).execute());
} else { } else {
holder.account_action.hide(); holder.account_action.hide();
@ -159,7 +159,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewH
holder.account_action.setEnabled(true); holder.account_action.setEnabled(true);
} }
//Follow button //Follow button
if (type == RetrieveAccountsAsyncTask.accountFetch.MUTED) { if (type == AccountsVM.accountFetch.MUTED) {
holder.account_action.show(); holder.account_action.show();
holder.account_action.setImageResource(R.drawable.ic_baseline_volume_mute_24); holder.account_action.setImageResource(R.drawable.ic_baseline_volume_mute_24);
} }

View File

@ -16,7 +16,6 @@ package app.fedilab.fedilabtube.fragment;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
@ -35,6 +34,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction; import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -46,23 +46,20 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrieveSingleAccountAsyncTask;
import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.HttpsConnection; import app.fedilab.fedilabtube.client.HttpsConnection;
import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.PeertubeAPI;
import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.ChannelCreation; import app.fedilab.fedilabtube.client.entities.ChannelCreation;
import app.fedilab.fedilabtube.drawer.AccountsListAdapter; import app.fedilab.fedilabtube.drawer.AccountsListAdapter;
import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; import app.fedilab.fedilabtube.viewmodel.AccountsVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccountsInterface, AccountsListAdapter.AllAccountsRemoved { public class DisplayAccountsFragment extends Fragment implements AccountsListAdapter.AllAccountsRemoved {
private boolean flag_loading; private boolean flag_loading;
private Context context; private Context context;
private AsyncTask<Void, Void, APIResponse> asyncTask;
private AccountsListAdapter accountsListAdapter; private AccountsListAdapter accountsListAdapter;
private String max_id; private String max_id;
private List<Account> accounts; private List<Account> accounts;
@ -73,7 +70,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
private boolean swiped; private boolean swiped;
private RecyclerView lv_accounts; private RecyclerView lv_accounts;
private View rootView; private View rootView;
private RetrieveAccountsAsyncTask.accountFetch accountFetch; private AccountsVM.accountFetch accountFetch;
private FloatingActionButton action_button; private FloatingActionButton action_button;
@Override @Override
@ -86,7 +83,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
accounts = new ArrayList<>(); accounts = new ArrayList<>();
if (bundle != null) { if (bundle != null) {
if (bundle.containsKey("accountFetch")) { if (bundle.containsKey("accountFetch")) {
accountFetch = (RetrieveAccountsAsyncTask.accountFetch) bundle.getSerializable("accountFetch"); accountFetch = (AccountsVM.accountFetch) bundle.getSerializable("accountFetch");
} }
name = bundle.getString("name", null); name = bundle.getString("name", null);
} }
@ -184,7 +181,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
accountsListAdapter.allAccountsRemoved = this; accountsListAdapter.allAccountsRemoved = this;
lv_accounts.setAdapter(accountsListAdapter); lv_accounts.setAdapter(accountsListAdapter);
TextView no_action_text = rootView.findViewById(R.id.no_action_text); 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)); no_action_text.setText(context.getString(R.string.no_muted));
} }
final LinearLayoutManager mLayoutManager; final LinearLayoutManager mLayoutManager;
@ -200,10 +197,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
if (!flag_loading) { if (!flag_loading) {
flag_loading = true; flag_loading = true;
if (accountFetch == null) { 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 { } else {
String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : max_id; String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : max_id;
asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 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); nextElementLoader.setVisibility(View.VISIBLE);
} }
@ -216,10 +215,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);
if (accountFetch == null) { 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 { } else {
String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : null; String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null;
asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class);
viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
} }
return rootView; return rootView;
} }
@ -227,7 +228,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
if (accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL) { if (accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL) {
action_button.setVisibility(View.VISIBLE); action_button.setVisibility(View.VISIBLE);
} else { } else {
action_button.setVisibility(View.GONE); action_button.setVisibility(View.GONE);
@ -252,10 +253,9 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
this.context = context; this.context = context;
} }
@Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
asyncTask.cancel(true);
} }
public void scrollToTop() { public void scrollToTop() {
@ -263,8 +263,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
lv_accounts.setAdapter(accountsListAdapter); lv_accounts.setAdapter(accountsListAdapter);
} }
@Override private void manageViewAccounts(APIResponse apiResponse) {
public void onRetrieveAccounts(APIResponse apiResponse) {
mainLoader.setVisibility(View.GONE); mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE);
if (apiResponse.getError() != null) { if (apiResponse.getError() != null) {
@ -306,10 +305,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
swiped = true; swiped = true;
swipeRefreshLayout.setRefreshing(true); swipeRefreshLayout.setRefreshing(true);
if (accountFetch == null) { 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 { } else {
String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : null; String param = accountFetch == AccountsVM.accountFetch.SINGLE_CHANNEL ? name : null;
asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); AccountsVM viewModel = new ViewModelProvider(DisplayAccountsFragment.this.requireActivity()).get(AccountsVM.class);
viewModel.getAccounts(null, param, accountFetch).observe(DisplayAccountsFragment.this.requireActivity(), this::manageViewAccounts);
} }
} }

View File

@ -15,7 +15,6 @@ package app.fedilab.fedilabtube.fragment;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.content.Context; import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -25,6 +24,7 @@ import android.widget.Toast;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
@ -34,20 +34,17 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import app.fedilab.fedilabtube.R; import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.asynctasks.RetrievePeertubeNotificationsAsyncTask;
import app.fedilab.fedilabtube.client.APIResponse; import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.entities.Account;
import app.fedilab.fedilabtube.client.entities.PeertubeNotification; import app.fedilab.fedilabtube.client.entities.PeertubeNotification;
import app.fedilab.fedilabtube.drawer.PeertubeNotificationsListAdapter; import app.fedilab.fedilabtube.drawer.PeertubeNotificationsListAdapter;
import app.fedilab.fedilabtube.interfaces.OnRetrievePeertubeNotificationsInterface; import app.fedilab.fedilabtube.viewmodel.NotificationsVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
public class DisplayNotificationsFragment extends Fragment implements OnRetrievePeertubeNotificationsInterface { public class DisplayNotificationsFragment extends Fragment {
private boolean flag_loading; private boolean flag_loading;
private Context context; private Context context;
private AsyncTask<Void, Void, APIResponse> asyncTask;
private PeertubeNotificationsListAdapter peertubeNotificationsListAdapter; private PeertubeNotificationsListAdapter peertubeNotificationsListAdapter;
private String max_id; private String max_id;
private List<PeertubeNotification> notifications; private List<PeertubeNotification> notifications;
@ -95,7 +92,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
if (firstVisibleItem + visibleItemCount == totalItemCount) { if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) { if (!flag_loading) {
flag_loading = true; 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); nextElementLoader.setVisibility(View.VISIBLE);
} }
} else { } else {
@ -106,7 +104,8 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
}); });
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); 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; return rootView;
} }
@ -130,8 +129,6 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
asyncTask.cancel(true);
} }
public void scrollToTop() { public void scrollToTop() {
@ -147,11 +144,12 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve
flag_loading = true; flag_loading = true;
swiped = true; swiped = true;
swipeRefreshLayout.setRefreshing(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 private void manageVIewNotifications(APIResponse apiResponse) {
public void onRetrievePeertubeNotifications(APIResponse apiResponse, Account account) {
mainLoader.setVisibility(View.GONE); mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE);
if (apiResponse.getError() != null) { if (apiResponse.getError() != null) {

View File

@ -179,7 +179,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter
int totalItemCount = mLayoutManager.getItemCount(); int totalItemCount = mLayoutManager.getItemCount();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) { if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class); 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) { if (type == PSUBSCRIPTIONS) {
AccountsVM viewModel = new ViewModelProvider(DisplayStatusFragment.this.requireActivity()).get(AccountsVM.class); 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 -> { display_all.setOnClickListener(v -> {

View File

@ -37,15 +37,15 @@ public class AccountsVM extends AndroidViewModel {
this.application = application; this.application = application;
} }
public LiveData<APIResponse> getAccounts(String max_id, accountFetch type) { public LiveData<APIResponse> getAccounts(String max_id, String name, accountFetch type) {
if (apiResponseMutableLiveData == null) { if (apiResponseMutableLiveData == null) {
apiResponseMutableLiveData = new MutableLiveData<>(); apiResponseMutableLiveData = new MutableLiveData<>();
loadAccounts(max_id, type); loadAccounts(max_id, name, type);
} }
return apiResponseMutableLiveData; 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(); Context _mContext = this.application.getApplicationContext();
new Thread(() -> { new Thread(() -> {
try { try {
@ -57,6 +57,10 @@ public class AccountsVM extends AndroidViewModel {
apiResponse = peertubeAPI.getMuted(max_id); apiResponse = peertubeAPI.getMuted(max_id);
} else if (type == accountFetch.CHANNEL) { } else if (type == accountFetch.CHANNEL) {
apiResponse = peertubeAPI.getPeertubeChannel(max_id); 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()); Handler mainHandler = new Handler(Looper.getMainLooper());
APIResponse finalApiResponse = apiResponse; APIResponse finalApiResponse = apiResponse;
@ -71,6 +75,8 @@ public class AccountsVM extends AndroidViewModel {
public enum accountFetch { public enum accountFetch {
SUBSCRIPTION, SUBSCRIPTION,
CHANNEL, CHANNEL,
MUTED MUTED,
SINGLE_ACCOUNT,
SINGLE_CHANNEL
} }
} }

View File

@ -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 <http://www.gnu.org/licenses>. */
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<APIResponse> apiResponseMutableLiveData;
private Application application;
public NotificationsVM(@NonNull Application application) {
super(application);
this.application = application;
}
public LiveData<APIResponse> 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();
}
}