/* Copyright 2017 Thomas Schneider * * This file is a part of Mastalab * * 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. * * Mastalab 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 Mastalab; if not, * see . */ package fr.gouv.etalab.mastodon.activities; import android.content.Intent; import android.content.SharedPreferences; import android.database.sqlite.SQLiteDatabase; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.DividerItemDecoration; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.RelativeLayout; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import es.dmoral.toasty.Toasty; import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.drawers.StatusListAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface; import fr.gouv.etalab.mastodon.sqlite.SearchDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_KEYWORD; import static fr.gouv.etalab.mastodon.helper.Helper.SEARCH_TAG; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; /** * Created by Thomas on 27/05/2017. * Show hashtag stream */ public class HashTagActivity extends BaseActivity implements OnRetrieveFeedsInterface { public static int position; private StatusListAdapter statusListAdapter; private String max_id; private List 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); SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); switch (theme){ case Helper.THEME_LIGHT: setTheme(R.style.AppTheme_NoActionBar); break; case Helper.THEME_DARK: setTheme(R.style.AppThemeDark_NoActionBar); break; case Helper.THEME_BLACK: setTheme(R.style.AppThemeBlack_NoActionBar); break; default: setTheme(R.style.AppThemeDark_NoActionBar); } setContentView(R.layout.activity_hashtag); Toolbar toolbar = findViewById(R.id.toolbar); if( theme == THEME_BLACK) toolbar.setBackgroundColor(ContextCompat.getColor(HashTagActivity.this, R.color.black)); setSupportActionBar(toolbar); if( getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true); Bundle b = getIntent().getExtras(); if(b != null) tag = b.getString("tag", null); if( tag == null) finish(); statuses = new ArrayList<>(); max_id = null; flag_loading = true; firstLoad = true; boolean isOnWifi = Helper.isOnWIFI(getApplicationContext()); swipeRefreshLayout = findViewById(R.id.swipeContainer); int behaviorWithAttachments = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS); final RecyclerView lv_status = findViewById(R.id.lv_status); lv_status.addItemDecoration(new DividerItemDecoration(HashTagActivity.this, DividerItemDecoration.VERTICAL)); tootsPerPage = sharedpreferences.getInt(Helper.SET_TOOTS_PER_PAGE, 40); mainLoader = findViewById(R.id.loader); nextElementLoader = findViewById(R.id.loading_next_status); textviewNoAction = findViewById(R.id.no_action); mainLoader.setVisibility(View.VISIBLE); nextElementLoader.setVisibility(View.GONE); int positionSpinnerTrans = (sharedpreferences.getInt(Helper.SET_TRANSLATOR, Helper.TRANS_YANDEX)); statusListAdapter = new StatusListAdapter(HashTagActivity.this, RetrieveFeedsAsyncTask.Type.TAG, null, isOnWifi, this.statuses); lv_status.setAdapter(statusListAdapter); setTitle(String.format("#%s", tag)); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { max_id = null; statuses = new ArrayList<>(); firstLoad = true; flag_loading = true; new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.TAG, tag,null, max_id, HashTagActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); final LinearLayoutManager mLayoutManager; mLayoutManager = new LinearLayoutManager(this); lv_status.setLayoutManager(mLayoutManager); lv_status.addOnScrollListener(new RecyclerView.OnScrollListener() { public void onScrolled(RecyclerView recyclerView, int dx, int dy) { if(dy > 0){ int visibleItemCount = mLayoutManager.getChildCount(); int totalItemCount = mLayoutManager.getItemCount(); int firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition(); if(firstVisibleItem + visibleItemCount == totalItemCount ) { if(!flag_loading ) { flag_loading = true; new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.TAG, tag,null, max_id, HashTagActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); nextElementLoader.setVisibility(View.VISIBLE); } } else { nextElementLoader.setVisibility(View.GONE); } } } }); new RetrieveFeedsAsyncTask(getApplicationContext(), RetrieveFeedsAsyncTask.Type.TAG, tag,null, max_id, HashTagActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @Override public boolean onPrepareOptionsMenu(Menu menu) { SQLiteDatabase db = Sqlite.getInstance(HashTagActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); List searchInDb = new SearchDAO(HashTagActivity.this, db).getSearchByKeyword(tag.trim()); if( searchInDb == null || searchInDb.size() == 0){ menu.clear(); menu.add(0, 777, Menu.NONE, R.string.pin_add).setIcon(R.drawable.ic_add).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); } SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); if( theme == THEME_LIGHT) Helper.colorizeIconMenu(menu, R.color.black); return super.onPrepareOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: finish(); return true; case 777: SQLiteDatabase db = Sqlite.getInstance(HashTagActivity.this, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); new SearchDAO(HashTagActivity.this, db).insertSearch(tag); Intent intent = new Intent(HashTagActivity.this, MainActivity.class); intent.putExtra(INTENT_ACTION, SEARCH_TAG); intent.putExtra(SEARCH_KEYWORD, tag); startActivity(intent); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onRetrieveFeeds(APIResponse apiResponse) { mainLoader.setVisibility(View.GONE); nextElementLoader.setVisibility(View.GONE); if( apiResponse.getError() != null){ Toasty.error(getApplicationContext(), apiResponse.getError().getError(),Toast.LENGTH_LONG).show(); return; } List statuses = apiResponse.getStatuses(); if( firstLoad && (statuses == null || statuses.size() == 0)) textviewNoAction.setVisibility(View.VISIBLE); else textviewNoAction.setVisibility(View.GONE); if( statuses != null && statuses.size() > 1) max_id =statuses.get(statuses.size()-1).getId(); else max_id = null; if( statuses != null) { this.statuses.addAll(statuses); statusListAdapter.notifyDataSetChanged(); } swipeRefreshLayout.setRefreshing(false); firstLoad = false; flag_loading = statuses != null && statuses.size() < tootsPerPage; } }