fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplayMediaFragment.java

218 lines
8.2 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2018-09-05 15:02:48 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-05 15:02:48 +02:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2018-09-05 15:02:48 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2018-09-05 15:02:48 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
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.widget.RelativeLayout;
import android.widget.Toast;
2018-11-25 10:45:16 +01:00
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2020-07-06 15:04:39 +02:00
import org.jetbrains.annotations.NotNull;
2018-09-05 15:02:48 +02:00
import java.util.ArrayList;
import java.util.List;
2018-11-25 10:45:16 +01:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Attachment;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.drawers.ImageAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2018-09-05 15:02:48 +02:00
/**
* Created by Thomas on 05/09/2018.
* Fragment to display media related to status
*/
public class DisplayMediaFragment extends Fragment implements OnRetrieveFeedsInterface {
2019-11-15 16:32:25 +01:00
boolean firstTootsLoaded;
2018-09-05 15:02:48 +02:00
private boolean flag_loading;
private Context context;
private String max_id;
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private String targetedId;
private boolean showMediaOnly, showPinned, showReply;
2019-01-22 18:07:06 +01:00
private ArrayList<Status> statuses;
2018-09-05 15:02:48 +02:00
private ImageAdapter gridAdaper;
2019-08-18 17:17:47 +02:00
private RecyclerView gridview;
2018-09-05 15:02:48 +02:00
2019-09-06 17:55:14 +02:00
public DisplayMediaFragment() {
2018-09-05 15:02:48 +02:00
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_status_media, container, false);
context = getContext();
Bundle bundle = this.getArguments();
showMediaOnly = true;
//Will allow to load first toots if bookmark != null
firstTootsLoaded = true;
showPinned = false;
max_id = null;
flag_loading = true;
showReply = false;
firstLoad = true;
assert context != null;
2019-09-06 17:55:14 +02:00
mainLoader = rootView.findViewById(R.id.loader);
2018-09-05 15:02:48 +02:00
nextElementLoader = rootView.findViewById(R.id.loading_next_status);
2019-09-06 17:55:14 +02:00
textviewNoAction = rootView.findViewById(R.id.no_action);
2018-09-05 15:02:48 +02:00
mainLoader.setVisibility(View.VISIBLE);
nextElementLoader.setVisibility(View.GONE);
if (bundle != null) {
2019-01-03 18:59:44 +01:00
targetedId = bundle.getString("targetedid", null);
2018-09-05 15:02:48 +02:00
}
2019-01-22 18:07:06 +01:00
statuses = new ArrayList<>();
2019-08-19 09:44:15 +02:00
gridAdaper = new ImageAdapter(statuses);
2019-08-18 17:17:47 +02:00
gridview = rootView.findViewById(R.id.gridview_media);
2018-09-05 15:02:48 +02:00
gridview.setAdapter(gridAdaper);
GridLayoutManager gvLayout = new GridLayoutManager(context, 3);
gridview.setLayoutManager(gvLayout);
2018-10-13 13:04:52 +02:00
2018-09-05 15:02:48 +02:00
gridview.addOnScrollListener(new RecyclerView.OnScrollListener() {
2020-07-06 15:04:39 +02:00
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
2018-09-05 15:02:48 +02:00
int firstVisibleItem = gvLayout.findFirstVisibleItemPosition();
if (dy > 0) {
int visibleItemCount = gvLayout.getChildCount();
int totalItemCount = gvLayout.getItemCount();
if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) {
if (!flag_loading) {
flag_loading = true;
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.USER, targetedId, max_id, showMediaOnly, showPinned, showReply, DisplayMediaFragment.this);
2018-09-05 15:02:48 +02:00
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
nextElementLoader.setVisibility(View.GONE);
}
}
}
});
2019-09-06 17:55:14 +02:00
if (context != null) {
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.USER, targetedId, max_id, showMediaOnly, showPinned, showReply, DisplayMediaFragment.this);
2019-09-06 17:55:14 +02:00
} else {
2020-07-06 15:04:39 +02:00
new Handler(Looper.getMainLooper()).postDelayed(() -> {
if (context != null) {
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.USER, targetedId, max_id, showMediaOnly, showPinned, showReply, DisplayMediaFragment.this);
2018-09-05 15:02:48 +02:00
}
}, 500);
}
return rootView;
}
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2018-09-05 15:02:48 +02:00
super.onCreate(saveInstance);
}
2019-08-18 17:17:47 +02:00
@Override
public void onDestroyView() {
2019-09-06 17:55:14 +02:00
if (gridview != null) {
2019-08-18 17:17:47 +02:00
gridview.setAdapter(null);
}
super.onDestroyView();
}
2018-09-05 15:02:48 +02:00
@Override
2020-07-06 15:04:39 +02:00
public void onAttach(@NotNull Context context) {
2018-09-05 15:02:48 +02:00
super.onAttach(context);
this.context = context;
}
@Override
2019-09-06 17:55:14 +02:00
public void onDestroy() {
2018-09-05 15:02:48 +02:00
super.onDestroy();
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
mainLoader.setVisibility(View.GONE);
nextElementLoader.setVisibility(View.GONE);
//Discards 404 - error which can often happen due to toots which have been deleted
2020-01-18 18:23:09 +01:00
if (apiResponse == null || (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError().getStatusCode() != 501)) {
2019-09-06 17:55:14 +02:00
if (apiResponse != null && apiResponse.getError() != null && apiResponse.getError().getError() != null)
2019-11-15 16:32:25 +01:00
if (apiResponse.getError().getError().length() < 100) {
2019-09-30 18:14:46 +02:00
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
}
2019-01-13 11:34:32 +01:00
else
2019-09-06 17:55:14 +02:00
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2018-09-05 15:02:48 +02:00
flag_loading = false;
return;
}
List<Status> statuses = apiResponse.getStatuses();
max_id = apiResponse.getMax_id();
2019-09-06 17:55:14 +02:00
if (this.statuses == null)
2019-01-22 18:07:06 +01:00
this.statuses = new ArrayList<>();
int previousPosition = this.statuses.size();
2019-09-06 17:55:14 +02:00
flag_loading = (max_id == null);
if (firstLoad && (statuses == null || statuses.size() == 0))
2018-09-05 15:02:48 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
else
textviewNoAction.setVisibility(View.GONE);
2019-01-22 18:07:06 +01:00
List<Status> convertedStatuses = new ArrayList<>();
2019-09-06 17:55:14 +02:00
if (apiResponse.getStatuses() != null && apiResponse.getStatuses().size() > 0) {
for (Status status : apiResponse.getStatuses()) {
if (status.getMedia_attachments() != null) {
2019-01-22 18:07:06 +01:00
String statusSerialized = Helper.statusToStringStorage(status);
for (Attachment attachment : status.getMedia_attachments()) {
Status newStatus = Helper.restoreStatusFromString(statusSerialized);
if (newStatus == null)
break;
newStatus.setArt_attachment(attachment);
convertedStatuses.add(newStatus);
}
2018-09-10 07:30:31 +02:00
}
2018-09-05 15:02:48 +02:00
}
}
2019-09-06 17:55:14 +02:00
if (convertedStatuses.size() > 0) {
2019-01-22 18:07:06 +01:00
this.statuses.addAll(convertedStatuses);
gridAdaper.notifyItemRangeInserted(previousPosition, this.statuses.size());
}
2018-09-05 15:02:48 +02:00
firstLoad = false;
}
}