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

110 lines
3.8 KiB
Java
Raw Normal View History

2020-03-08 19:25:18 +01:00
package app.fedilab.android.fragments;
2020-03-08 19:32:59 +01:00
/* Copyright 2020 Thomas Schneider
2020-03-08 19:25:18 +01:00
*
* This file is a part of Fedilab
*
* 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
2020-03-08 19:32:59 +01:00
import android.widget.Toast;
2020-03-08 19:25:18 +01:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
2020-03-08 19:32:59 +01:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2020-04-08 12:42:15 +02:00
2020-03-08 19:32:59 +01:00
import org.jetbrains.annotations.NotNull;
2020-04-08 12:42:15 +02:00
2020-03-08 19:25:18 +01:00
import java.util.List;
2020-04-08 12:42:15 +02:00
2020-03-08 19:25:18 +01:00
import app.fedilab.android.R;
2020-03-08 19:32:59 +01:00
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Announcement;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
import es.dmoral.toasty.Toasty;
2020-03-08 19:25:18 +01:00
/**
2020-03-08 19:32:59 +01:00
* Created by Thomas on 08/03/2020.
* Fragment to display announcements
2020-03-08 19:25:18 +01:00
*/
2020-03-08 19:32:59 +01:00
public class DisplayAnnouncementsFragment extends Fragment implements OnRetrieveFeedsInterface {
2020-03-08 19:25:18 +01:00
private Context context;
private RelativeLayout textviewNoAction;
2020-03-08 19:32:59 +01:00
private RelativeLayout mainLoader;
2020-03-08 19:25:18 +01:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2020-03-08 19:32:59 +01:00
View rootView = inflater.inflate(R.layout.fragment_announcements, container, false);
2020-03-08 19:25:18 +01:00
context = getContext();
2020-03-08 19:32:59 +01:00
final RecyclerView lv_annoucements = rootView.findViewById(R.id.lv_annoucements);
mainLoader = rootView.findViewById(R.id.loader);
2020-03-08 19:25:18 +01:00
textviewNoAction = rootView.findViewById(R.id.no_action);
mainLoader.setVisibility(View.VISIBLE);
2020-03-08 19:32:59 +01:00
LinearLayoutManager mLayoutManager = new LinearLayoutManager(context);
lv_annoucements.setLayoutManager(mLayoutManager);
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.ANNOUNCEMENTS, null, DisplayAnnouncementsFragment.this);
2020-03-08 19:25:18 +01:00
return rootView;
}
2020-03-08 19:32:59 +01:00
@Override
public void onDestroy() {
super.onDestroy();
}
2020-04-08 12:42:15 +02:00
2020-03-08 19:25:18 +01:00
@Override
public void onCreate(Bundle saveInstance) {
super.onCreate(saveInstance);
}
@Override
2020-03-08 19:32:59 +01:00
public void onAttach(@NotNull Context context) {
2020-03-08 19:25:18 +01:00
super.onAttach(context);
this.context = context;
}
2020-03-08 19:32:59 +01:00
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
mainLoader.setVisibility(View.GONE);
if (apiResponse == null) {
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return;
2020-04-08 12:42:15 +02:00
} else {
2020-03-08 19:32:59 +01:00
if (apiResponse.getError() != null) {
if (apiResponse.getError().getError().length() < 100) {
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
}
return;
}
}
2021-01-19 17:43:51 +01:00
List<Announcement> announcements = apiResponse.getAnnouncements();
2020-04-08 12:42:15 +02:00
if (announcements == null || announcements.size() == 0) {
2020-03-08 19:32:59 +01:00
textviewNoAction.setVisibility(View.VISIBLE);
}
}
2020-03-08 19:25:18 +01:00
}