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

120 lines
4.3 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2018-09-10 19:21:42 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-10 19:21:42 +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-10 19:21:42 +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-10 19:21:42 +02:00
* see <http://www.gnu.org/licenses>. */
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
2019-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
2019-11-12 18:51:39 +01:00
import org.jetbrains.annotations.NotNull;
2018-09-10 19:21:42 +02:00
import java.util.Calendar;
import java.util.Date;
import java.util.List;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.WhoToFollowAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.TrunkAccount;
import app.fedilab.android.drawers.WhoToFollowAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrieveWhoToFollowInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2018-09-10 19:21:42 +02:00
/**
* Created by Thomas on 10/09/2018.
* Fragment to display who to follow list
*/
public class WhoToFollowFragment extends Fragment implements OnRetrieveWhoToFollowInterface {
private Context context;
private View rootView;
2018-09-12 09:49:56 +02:00
private RelativeLayout mainLoader;
2018-09-10 19:21:42 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_who_to_follow, container, false);
context = getContext();
2018-09-12 09:49:56 +02:00
mainLoader = rootView.findViewById(R.id.loader);
2018-09-10 19:21:42 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String lastDateListRefresh = sharedpreferences.getString(Helper.LAST_DATE_LIST_REFRESH, null);
Calendar cal = Calendar.getInstance();
2018-09-10 19:35:17 +02:00
cal.setTime(new Date());
2018-09-10 19:21:42 +02:00
cal.add(Calendar.MINUTE, -15);
Date dateAllowed = cal.getTime();
2019-09-06 17:55:14 +02:00
if (lastDateListRefresh == null || Helper.stringToDate(context, lastDateListRefresh).before(dateAllowed)) {
2018-09-10 19:35:17 +02:00
mainLoader.setVisibility(View.VISIBLE);
2021-01-19 17:43:51 +01:00
new WhoToFollowAsyncTask(context, null, WhoToFollowFragment.this);
2019-09-06 17:55:14 +02:00
} else {
2018-09-10 19:21:42 +02:00
String lastList = sharedpreferences.getString(Helper.LAST_LIST, null);
List<String> list = Helper.restoreArrayFromString(lastList);
displayResults(list);
}
2018-09-10 19:35:17 +02:00
2018-09-10 19:21:42 +02:00
return rootView;
}
@Override
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2018-09-10 19:21:42 +02:00
super.onCreate(saveInstance);
}
@Override
2019-11-12 18:51:39 +01:00
public void onAttach(@NotNull Context context) {
2018-09-10 19:21:42 +02:00
super.onAttach(context);
this.context = context;
}
2019-09-06 17:55:14 +02:00
private void displayResults(List<String> list) {
2018-09-10 19:21:42 +02:00
mainLoader.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (list != null) {
2018-09-10 19:21:42 +02:00
ListView lv_list = rootView.findViewById(R.id.lv_list);
2019-08-19 09:18:30 +02:00
WhoToFollowAdapter whoToFollowAdapter = new WhoToFollowAdapter(list);
2018-09-10 19:21:42 +02:00
lv_list.setAdapter(whoToFollowAdapter);
2019-09-06 17:55:14 +02:00
} else {
Toasty.error(context, context.getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2018-09-10 19:21:42 +02:00
}
}
2018-09-12 09:49:56 +02:00
@Override
public void onRetrieveWhoToFollowList(List<String> list) {
2019-09-06 17:55:14 +02:00
if (list != null) {
2018-09-12 09:49:56 +02:00
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.LAST_DATE_LIST_REFRESH, Helper.dateToString(new Date()));
editor.putString(Helper.LAST_LIST, Helper.arrayToStringStorage(list));
editor.apply();
}
displayResults(list);
}
@Override
public void onRetrieveWhoToFollowAccount(List<TrunkAccount> trunkAccounts) {
}
2018-09-10 19:21:42 +02:00
}