fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/HashTagActivity.java

256 lines
10 KiB
Java
Raw Normal View History

2017-05-27 08:32:14 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-05-27 08:32:14 +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
2017-05-27 08:32:14 +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,
2017-05-27 08:32:14 +02:00
* see <http://www.gnu.org/licenses>. */
2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2017-05-27 08:32:14 +02:00
2018-08-18 15:16:21 +02:00
import android.content.Intent;
2017-05-27 08:32:14 +02:00
import android.content.SharedPreferences;
2018-08-18 15:16:21 +02:00
import android.database.sqlite.SQLiteDatabase;
2017-05-27 08:32:14 +02:00
import android.os.Bundle;
2018-08-18 15:16:21 +02:00
import android.view.Menu;
2017-05-27 08:32:14 +02:00
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
2017-05-27 08:32:14 +02:00
2021-01-31 17:06:22 +01:00
2019-11-20 12:11:37 +01:00
import androidx.core.content.ContextCompat;
2019-11-15 16:32:25 +01:00
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
2020-06-08 17:59:12 +02:00
import org.jetbrains.annotations.NotNull;
2017-05-27 08:32:14 +02:00
import java.util.ArrayList;
import java.util.List;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
2021-01-31 17:06:22 +01:00
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Status;
2020-03-07 08:46:48 +01:00
import app.fedilab.android.client.Entities.StatusDrawerParams;
2020-06-08 17:59:12 +02:00
import app.fedilab.android.client.Entities.StoredStatus;
2021-01-31 17:06:22 +01:00
import app.fedilab.android.databinding.ActivityHashtagBinding;
import app.fedilab.android.drawers.PixelfedListAdapter;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.drawers.StatusListAdapter;
import app.fedilab.android.helper.Helper;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.sqlite.SearchDAO;
import app.fedilab.android.sqlite.Sqlite;
2018-11-25 10:45:16 +01:00
import es.dmoral.toasty.Toasty;
2017-05-27 08:32:14 +02:00
/**
* Created by Thomas on 27/05/2017.
* Show hashtag stream
*/
2017-12-12 18:17:01 +01:00
public class HashTagActivity extends BaseActivity implements OnRetrieveFeedsInterface {
2017-05-27 08:32:14 +02:00
public static int position;
private StatusListAdapter statusListAdapter;
2021-01-31 17:06:22 +01:00
private PixelfedListAdapter pixelfedListAdapter;
2017-05-27 08:32:14 +02:00
private String max_id;
private List<Status> statuses;
private boolean firstLoad;
private String tag;
private int tootsPerPage;
private boolean flag_loading = false;
2021-01-31 17:06:22 +01:00
private ActivityHashtagBinding binding;
2017-05-27 08:32:14 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
2017-06-30 17:09:07 +02:00
2019-05-18 11:10:30 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2018-05-11 11:28:05 +02:00
case Helper.THEME_LIGHT:
2019-07-20 15:15:47 +02:00
setTheme(R.style.AppTheme_NoActionBar_Fedilab);
2018-05-11 11:28:05 +02:00
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack_NoActionBar);
break;
default:
setTheme(R.style.AppThemeDark_NoActionBar);
2017-06-30 17:09:07 +02:00
}
2021-01-31 17:06:22 +01:00
binding = ActivityHashtagBinding.inflate(getLayoutInflater());
View viewRoot = binding.getRoot();
setContentView(viewRoot);
setSupportActionBar(binding.toolbar);
2017-06-30 17:09:07 +02:00
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2017-05-27 08:32:14 +02:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Bundle b = getIntent().getExtras();
2019-11-30 15:47:21 +01:00
if (b != null) {
2017-05-27 08:32:14 +02:00
tag = b.getString("tag", null);
2019-11-30 15:47:21 +01:00
}
2019-09-06 17:55:14 +02:00
if (tag == null)
2017-05-27 08:32:14 +02:00
finish();
statuses = new ArrayList<>();
max_id = null;
flag_loading = true;
firstLoad = true;
2020-04-08 12:42:15 +02:00
boolean isOnWifi = Helper.isOnWIFI(HashTagActivity.this);
2019-11-12 19:22:14 +01:00
int c1 = getResources().getColor(R.color.cyanea_accent);
int c2 = getResources().getColor(R.color.cyanea_primary_dark);
int c3 = getResources().getColor(R.color.cyanea_primary);
2021-01-31 17:06:22 +01:00
binding.swipeContainer.setProgressBackgroundColorSchemeColor(c3);
binding.swipeContainer.setColorSchemeColors(
2019-11-12 19:08:05 +01:00
c1, c2, c1
);
2020-06-08 17:59:12 +02:00
2021-01-31 17:06:22 +01:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
binding.toot.hide();
}
binding.toot.setOnClickListener(v -> {
2020-06-08 17:59:12 +02:00
Intent intentToot = new Intent(HashTagActivity.this, TootActivity.class);
Bundle val = new Bundle();
StoredStatus storedStatus = new StoredStatus();
Status tagStatus = new Status();
tagStatus.setVisibility("public");
tagStatus.setContent(HashTagActivity.this, String.format("#%s ", tag));
storedStatus.setStatus(tagStatus);
val.putParcelable("storedStatus", storedStatus);
intentToot.putExtras(val);
startActivity(intentToot);
});
2021-01-31 17:06:22 +01:00
binding.toot.setBackgroundColor(ContextCompat.getColor(HashTagActivity.this, R.color.cyanea_primary));
2019-08-31 16:41:29 +02:00
tootsPerPage = sharedpreferences.getInt(Helper.SET_TOOT_PER_PAGE, Helper.TOOTS_PER_PAGE);
2020-03-07 08:46:48 +01:00
StatusDrawerParams statusDrawerParams = new StatusDrawerParams();
statusDrawerParams.setType(RetrieveFeedsAsyncTask.Type.TAG);
statusDrawerParams.setTargetedId(null);
statusDrawerParams.setOnWifi(isOnWifi);
statusDrawerParams.setStatuses(this.statuses);
2021-01-31 17:06:22 +01:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
statusListAdapter = new StatusListAdapter(statusDrawerParams);
binding.lvStatus.setAdapter(statusListAdapter);
} else {
pixelfedListAdapter = new PixelfedListAdapter(statusDrawerParams);
binding.lvStatus.setAdapter(pixelfedListAdapter);
}
2017-06-11 11:58:46 +02:00
setTitle(String.format("#%s", tag));
2021-01-31 17:06:22 +01:00
binding.swipeContainer.setOnRefreshListener(() -> {
2020-03-08 09:15:12 +01:00
max_id = null;
statuses = new ArrayList<>();
firstLoad = true;
flag_loading = true;
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, tag, null, max_id, HashTagActivity.this);
2017-05-27 08:32:14 +02:00
});
2017-10-14 14:36:48 +02:00
final LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
2021-01-31 17:06:22 +01:00
binding.lvStatus.setLayoutManager(mLayoutManager);
binding.lvStatus.addOnScrollListener(new RecyclerView.OnScrollListener() {
2019-09-06 17:55:14 +02:00
public void onScrolled(@NotNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0) {
2017-10-14 14:36:48 +02:00
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
2019-09-06 17:55:14 +02:00
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
2017-10-14 14:36:48 +02:00
flag_loading = true;
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, tag, null, max_id, HashTagActivity.this);
2017-10-14 14:36:48 +02:00
2021-01-31 17:06:22 +01:00
binding.loadingNextStatus.setVisibility(View.VISIBLE);
2017-10-14 14:36:48 +02:00
}
} else {
2021-01-31 17:06:22 +01:00
binding.loadingNextStatus.setVisibility(View.GONE);
2017-05-27 08:32:14 +02:00
}
}
}
});
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, tag, null, max_id, HashTagActivity.this);
2017-05-27 08:32:14 +02:00
}
2019-09-06 17:55:14 +02:00
2018-08-18 15:16:21 +02:00
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
2019-11-09 14:35:45 +01:00
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.tag_pin, menu);
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2018-08-18 15:16:21 +02:00
List<String> searchInDb = new SearchDAO(HashTagActivity.this, db).getSearchByKeyword(tag.trim());
2021-01-31 17:06:22 +01:00
if (searchInDb != null && searchInDb.size() > 0 || MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) {
2019-11-09 14:35:45 +01:00
menu.findItem(R.id.action_pin).setVisible(false);
2018-08-18 15:16:21 +02:00
}
2019-11-09 14:35:45 +01:00
return true;
2018-08-18 15:16:21 +02:00
}
2017-05-27 08:32:14 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
2021-01-10 18:41:50 +01:00
int itemId = item.getItemId();
if (itemId == android.R.id.home) {
finish();
return true;
} else if (itemId == R.id.action_pin) {
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new SearchDAO(HashTagActivity.this, db).insertSearch(tag);
Intent intent = new Intent(HashTagActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Helper.INTENT_ACTION, Helper.SEARCH_TAG);
intent.putExtra(Helper.SEARCH_KEYWORD, tag);
startActivity(intent);
return true;
2017-05-27 08:32:14 +02:00
}
2021-01-10 18:41:50 +01:00
return super.onOptionsItemSelected(item);
2017-05-27 08:32:14 +02:00
}
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
2017-05-27 08:32:14 +02:00
2021-01-31 17:06:22 +01:00
binding.loader.setVisibility(View.GONE);
binding.loadingNextStatus.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (apiResponse == null || apiResponse.getError() != null) {
if (apiResponse != null)
2020-04-08 12:42:15 +02:00
Toasty.error(HashTagActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
else
2020-04-08 12:42:15 +02:00
Toasty.error(HashTagActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return;
}
2017-06-03 18:18:27 +02:00
List<Status> statuses = apiResponse.getStatuses();
2019-09-06 17:55:14 +02:00
if (firstLoad && (statuses == null || statuses.size() == 0))
2021-01-31 17:06:22 +01:00
binding.noAction.setVisibility(View.VISIBLE);
2017-05-27 08:32:14 +02:00
else
2021-01-31 17:06:22 +01:00
binding.noAction.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (statuses != null && statuses.size() > 1)
max_id = statuses.get(statuses.size() - 1).getId();
2017-05-27 08:32:14 +02:00
else
max_id = null;
2019-09-06 17:55:14 +02:00
if (statuses != null) {
2017-10-27 15:34:53 +02:00
this.statuses.addAll(statuses);
2021-01-31 17:06:22 +01:00
if (statusListAdapter != null) {
statusListAdapter.notifyDataSetChanged();
} else if (pixelfedListAdapter != null) {
pixelfedListAdapter.notifyDataSetChanged();
}
2017-05-27 08:32:14 +02:00
}
2021-01-31 17:06:22 +01:00
binding.swipeContainer.setRefreshing(false);
2017-05-27 08:32:14 +02:00
firstLoad = false;
flag_loading = statuses != null && statuses.size() < tootsPerPage;
}
}