package app.fedilab.fedilabtube.fragment; /* 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.Activity; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.recyclerview.widget.DividerItemDecoration; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; import java.util.ArrayList; import java.util.List; 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.HttpsConnection; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.ChannelCreation; import app.fedilab.fedilabtube.drawer.AccountsListAdapter; import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface; import es.dmoral.toasty.Toasty; public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccountsInterface, AccountsListAdapter.AllAccountsRemoved { private boolean flag_loading; private Context context; private AsyncTask asyncTask; private AccountsListAdapter accountsListAdapter; private String max_id; private List accounts; private RelativeLayout mainLoader, nextElementLoader, textviewNoAction; private boolean firstLoad; private SwipeRefreshLayout swipeRefreshLayout; private String name; private boolean swiped; private RecyclerView lv_accounts; private View rootView; private RetrieveAccountsAsyncTask.accountFetch accountFetch; private FloatingActionButton action_button; @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_recyclerview, container, false); context = getContext(); Bundle bundle = this.getArguments(); accounts = new ArrayList<>(); if (bundle != null) { if (bundle.containsKey("accountFetch")) { accountFetch = (RetrieveAccountsAsyncTask.accountFetch) bundle.getSerializable("accountFetch"); } name = bundle.getString("name", null); } max_id = null; firstLoad = true; flag_loading = true; swiped = false; swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer); if (getActivity() != null) { action_button = getActivity().findViewById(R.id.action_button); action_button.setOnClickListener(view -> { AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context); LayoutInflater inflater1 = ((Activity) context).getLayoutInflater(); View dialogView = inflater1.inflate(R.layout.add_channel, new LinearLayout(context), false); dialogBuilder.setView(dialogView); EditText display_name = dialogView.findViewById(R.id.display_name); EditText name = dialogView.findViewById(R.id.name); EditText description = dialogView.findViewById(R.id.description); dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> { if (display_name.getText() != null && display_name.getText().toString().trim().length() > 0 && name.getText() != null && name.getText().toString().trim().length() > 0) { ChannelCreation channelCreation = new ChannelCreation(); channelCreation.setDisplayName(display_name.getText().toString().trim()); channelCreation.setName(name.getText().toString().trim()); if (description.getText() != null && description.getText().toString().trim().length() > 0) { channelCreation.setDescription(description.getText().toString().trim()); } new Thread(() -> { try { new PeertubeAPI(context).createChannel(channelCreation); Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { DisplayPlaylistsFragment displayPlaylistsFragment; if (getActivity() == null) return; displayPlaylistsFragment = (DisplayPlaylistsFragment) getActivity().getSupportFragmentManager().findFragmentByTag("CHANNELS"); final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction(); if (displayPlaylistsFragment != null) { ft.detach(displayPlaylistsFragment); ft.attach(displayPlaylistsFragment); ft.commit(); } action_button.setEnabled(true); }; mainHandler.post(myRunnable); } catch (HttpsConnection.HttpsConnectionException e) { action_button.setEnabled(true); e.printStackTrace(); Handler mainHandler = new Handler(Looper.getMainLooper()); Runnable myRunnable = () -> { if (e.getMessage() != null) { Toasty.error(context, e.getMessage(), Toast.LENGTH_LONG).show(); } else { Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show(); } }; mainHandler.post(myRunnable); } }).start(); dialog.dismiss(); action_button.setEnabled(false); } else { Toasty.error(context, context.getString(R.string.error_display_name_channel), Toast.LENGTH_LONG).show(); } }); dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss()); AlertDialog alertDialog = dialogBuilder.create(); alertDialog.setTitle(getString(R.string.action_channel_create)); alertDialog.setOnDismissListener(dialogInterface -> { //Hide keyboard InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); assert imm != null; imm.hideSoftInputFromWindow(display_name.getWindowToken(), 0); }); if (alertDialog.getWindow() != null) alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); alertDialog.show(); }); } lv_accounts = rootView.findViewById(R.id.lv_elements); lv_accounts.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL)); mainLoader = rootView.findViewById(R.id.loader); nextElementLoader = rootView.findViewById(R.id.loading_next); textviewNoAction = rootView.findViewById(R.id.no_action); mainLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.GONE); accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts); accountsListAdapter.allAccountsRemoved = this; lv_accounts.setAdapter(accountsListAdapter); TextView no_action_text = rootView.findViewById(R.id.no_action_text); if (accountFetch == RetrieveAccountsAsyncTask.accountFetch.MUTED) { no_action_text.setText(context.getString(R.string.no_muted)); } final LinearLayoutManager mLayoutManager; mLayoutManager = new LinearLayoutManager(context); lv_accounts.setLayoutManager(mLayoutManager); lv_accounts.addOnScrollListener(new RecyclerView.OnScrollListener() { public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { if (dy > 0) { int visibleItemCount = mLayoutManager.getChildCount(); int totalItemCount = mLayoutManager.getItemCount(); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); if (firstVisibleItem + visibleItemCount == totalItemCount) { if (!flag_loading) { flag_loading = true; if (accountFetch == null) { asyncTask = new RetrieveSingleAccountAsyncTask(context, name, RetrieveSingleAccountAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : max_id; asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } nextElementLoader.setVisibility(View.VISIBLE); } } else { nextElementLoader.setVisibility(View.GONE); } } } }); swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh); if (accountFetch == null) { asyncTask = new RetrieveSingleAccountAsyncTask(context, name, RetrieveSingleAccountAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : null; asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } return rootView; } @Override public void onResume() { super.onResume(); if (accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL) { action_button.setVisibility(View.VISIBLE); } else { action_button.setVisibility(View.GONE); } } @Override public void onDestroyView() { super.onDestroyView(); rootView = null; } @Override public void onCreate(Bundle saveInstance) { super.onCreate(saveInstance); } @Override public void onAttach(@NonNull Context context) { super.onAttach(context); this.context = context; } public void onDestroy() { super.onDestroy(); if (asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING) asyncTask.cancel(true); } public void scrollToTop() { if (lv_accounts != null) lv_accounts.setAdapter(accountsListAdapter); } @Override public void onRetrieveAccounts(APIResponse apiResponse) { mainLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE); if (apiResponse.getError() != null) { Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); swipeRefreshLayout.setRefreshing(false); swiped = false; flag_loading = false; return; } flag_loading = (apiResponse.getMax_id() == null); List accounts = apiResponse.getAccounts(); if (!swiped && firstLoad && (accounts == null || accounts.size() == 0)) textviewNoAction.setVisibility(View.VISIBLE); else textviewNoAction.setVisibility(View.GONE); max_id = apiResponse.getMax_id(); if (swiped) { accountsListAdapter = new AccountsListAdapter(accountFetch, this.accounts); lv_accounts.setAdapter(accountsListAdapter); swiped = false; } if (accounts != null && accounts.size() > 0) { int currentPosition = this.accounts.size(); this.accounts.addAll(accounts); accountsListAdapter.notifyItemRangeChanged(currentPosition, accounts.size()); } swipeRefreshLayout.setRefreshing(false); firstLoad = false; } public void pullToRefresh() { max_id = null; accounts = new ArrayList<>(); firstLoad = true; flag_loading = true; swiped = true; swipeRefreshLayout.setRefreshing(true); if (accountFetch == null) { asyncTask = new RetrieveSingleAccountAsyncTask(context, name, RetrieveSingleAccountAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } else { String param = accountFetch == RetrieveAccountsAsyncTask.accountFetch.CHANNEL ? name : null; asyncTask = new RetrieveAccountsAsyncTask(context, param, accountFetch, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } } @Override public void onAllAccountsRemoved() { textviewNoAction.setVisibility(View.VISIBLE); } }