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.AsyncTask;
|
|
|
|
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.RelativeLayout;
|
2017-05-30 19:31:44 +02:00
|
|
|
import android.widget.Toast;
|
2017-05-27 08:32:14 +02:00
|
|
|
|
2019-11-15 16:32:25 +01:00
|
|
|
import androidx.appcompat.widget.Toolbar;
|
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;
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
|
2019-04-27 11:51:19 +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;
|
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;
|
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;
|
|
|
|
private String max_id;
|
|
|
|
private List<Status> statuses;
|
|
|
|
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
|
|
|
|
private boolean firstLoad;
|
|
|
|
private SwipeRefreshLayout swipeRefreshLayout;
|
|
|
|
private String tag;
|
|
|
|
private int tootsPerPage;
|
|
|
|
private boolean flag_loading = false;
|
|
|
|
|
|
|
|
@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);
|
2017-07-04 21:15:11 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-05-27 08:32:14 +02:00
|
|
|
setContentView(R.layout.activity_hashtag);
|
2017-10-28 10:38:37 +02:00
|
|
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
|
setSupportActionBar(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);
|
2017-10-27 15:34:53 +02:00
|
|
|
swipeRefreshLayout = findViewById(R.id.swipeContainer);
|
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);
|
2019-11-12 19:08:05 +01:00
|
|
|
swipeRefreshLayout.setProgressBackgroundColorSchemeColor(c3);
|
|
|
|
swipeRefreshLayout.setColorSchemeColors(
|
|
|
|
c1, c2, c1
|
|
|
|
);
|
2019-11-20 12:11:37 +01:00
|
|
|
toolbar.setBackgroundColor(ContextCompat.getColor(HashTagActivity.this, R.color.cyanea_primary));
|
2017-10-27 15:34:53 +02:00
|
|
|
final RecyclerView lv_status = findViewById(R.id.lv_status);
|
2019-08-31 16:41:29 +02:00
|
|
|
tootsPerPage = sharedpreferences.getInt(Helper.SET_TOOT_PER_PAGE, Helper.TOOTS_PER_PAGE);
|
2017-10-27 15:34:53 +02:00
|
|
|
mainLoader = findViewById(R.id.loader);
|
|
|
|
nextElementLoader = findViewById(R.id.loading_next_status);
|
|
|
|
textviewNoAction = findViewById(R.id.no_action);
|
2017-05-27 08:32:14 +02:00
|
|
|
mainLoader.setVisibility(View.VISIBLE);
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
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);
|
|
|
|
statusListAdapter = new StatusListAdapter(statusDrawerParams);
|
2017-05-27 08:32:14 +02:00
|
|
|
lv_status.setAdapter(statusListAdapter);
|
2017-06-11 11:58:46 +02:00
|
|
|
setTitle(String.format("#%s", tag));
|
2020-03-08 09:15:12 +01:00
|
|
|
swipeRefreshLayout.setOnRefreshListener(() -> {
|
|
|
|
max_id = null;
|
|
|
|
statuses = new ArrayList<>();
|
|
|
|
firstLoad = true;
|
|
|
|
flag_loading = true;
|
2020-04-08 12:42:15 +02:00
|
|
|
new RetrieveFeedsAsyncTask(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, tag, null, max_id, HashTagActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-05-27 08:32:14 +02:00
|
|
|
});
|
2017-10-14 14:36:48 +02:00
|
|
|
final LinearLayoutManager mLayoutManager;
|
|
|
|
mLayoutManager = new LinearLayoutManager(this);
|
|
|
|
lv_status.setLayoutManager(mLayoutManager);
|
|
|
|
lv_status.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;
|
2020-04-08 12:42:15 +02:00
|
|
|
new RetrieveFeedsAsyncTask(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, tag, null, max_id, HashTagActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2017-10-14 14:36:48 +02:00
|
|
|
|
|
|
|
nextElementLoader.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nextElementLoader.setVisibility(View.GONE);
|
2017-05-27 08:32:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-04-08 12:42:15 +02:00
|
|
|
new RetrieveFeedsAsyncTask(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, tag, null, max_id, HashTagActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
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());
|
2019-11-15 16:32:25 +01:00
|
|
|
if (searchInDb != null && searchInDb.size() > 0) {
|
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) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
finish();
|
|
|
|
return true;
|
2019-11-09 14:35:45 +01:00
|
|
|
case R.id.action_pin:
|
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
|
|
|
new SearchDAO(HashTagActivity.this, db).insertSearch(tag);
|
|
|
|
Intent intent = new Intent(HashTagActivity.this, MainActivity.class);
|
2019-12-17 18:45:29 +01:00
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
|
2019-05-18 11:10:30 +02:00
|
|
|
intent.putExtra(Helper.INTENT_ACTION, Helper.SEARCH_TAG);
|
|
|
|
intent.putExtra(Helper.SEARCH_KEYWORD, tag);
|
2018-08-18 15:16:21 +02:00
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
2017-05-27 08:32:14 +02:00
|
|
|
default:
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-29 16:10:11 +02:00
|
|
|
public void onRetrieveFeeds(APIResponse apiResponse) {
|
2017-05-27 08:32:14 +02:00
|
|
|
|
2017-05-30 19:31:44 +02:00
|
|
|
mainLoader.setVisibility(View.GONE);
|
|
|
|
nextElementLoader.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();
|
2019-04-27 11:51:19 +02:00
|
|
|
else
|
2020-04-08 12:42:15 +02:00
|
|
|
Toasty.error(HashTagActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
|
2017-05-30 19:31:44 +02:00
|
|
|
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))
|
2017-05-27 08:32:14 +02:00
|
|
|
textviewNoAction.setVisibility(View.VISIBLE);
|
|
|
|
else
|
|
|
|
textviewNoAction.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);
|
2017-05-27 08:32:14 +02:00
|
|
|
statusListAdapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
swipeRefreshLayout.setRefreshing(false);
|
|
|
|
firstLoad = false;
|
|
|
|
flag_loading = statuses != null && statuses.size() < tootsPerPage;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|