2021-12-10 10:21:06 +00:00
|
|
|
package cc.pogoda.mobile.meteosystem.activity;
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-05-14 13:11:20 +00:00
|
|
|
import android.app.SearchManager;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2022-05-08 14:42:45 +00:00
|
|
|
import android.os.Bundle;
|
2022-05-14 13:11:20 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.widget.SearchView;
|
2022-05-08 14:42:45 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2020-12-16 19:26:53 +00:00
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2022-05-08 14:42:45 +00:00
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
import org.greenrobot.eventbus.Subscribe;
|
|
|
|
import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2022-05-14 13:11:20 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.stream.Collectors;
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-01-03 23:13:56 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.Main;
|
2021-12-10 10:21:06 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.R;
|
|
|
|
import cc.pogoda.mobile.meteosystem.adapter.WeatherStationRecyclerViewAdapter;
|
2022-05-08 14:42:45 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.type.AllStationsReceivedEvent;
|
2021-12-10 10:21:06 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.type.ParceableFavsCallReason;
|
2022-05-08 14:42:45 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.type.StartStationsRefreshEvent;
|
2022-01-03 23:13:56 +00:00
|
|
|
import cc.pogoda.mobile.meteosystem.type.WeatherStation;
|
|
|
|
|
2020-12-16 19:26:53 +00:00
|
|
|
public class AllStationsActivity extends AppCompatActivity {
|
|
|
|
|
2022-05-14 13:11:20 +00:00
|
|
|
private List<WeatherStation> allStationsList;
|
2022-05-08 14:42:45 +00:00
|
|
|
private SwipeRefreshLayout refreshLayout;
|
|
|
|
private WeatherStationRecyclerViewAdapter adapter;
|
2021-08-11 17:20:03 +00:00
|
|
|
|
2020-12-16 19:26:53 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2022-05-08 14:42:45 +00:00
|
|
|
|
2020-12-16 19:26:53 +00:00
|
|
|
setContentView(R.layout.activity_all_stations);
|
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
refreshLayout = findViewById(R.id.refreshAllStationsView);
|
|
|
|
refreshLayout.setOnRefreshListener(
|
|
|
|
() -> ((Main) getApplication()).startGetAllStationsService()
|
|
|
|
);
|
2022-01-03 23:13:56 +00:00
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
RecyclerView recyclerViewAllStations = findViewById(R.id.recyclerViewAllStations);
|
|
|
|
adapter = new WeatherStationRecyclerViewAdapter(
|
2022-05-14 13:11:20 +00:00
|
|
|
new LinkedList<>(), this, ParceableFavsCallReason.Reason.ALL_STATIONS);
|
2022-05-08 14:42:45 +00:00
|
|
|
recyclerViewAllStations.setAdapter(adapter);
|
|
|
|
recyclerViewAllStations.setLayoutManager(new LinearLayoutManager(this));
|
2022-05-14 13:11:20 +00:00
|
|
|
|
|
|
|
handleIntent(getIntent());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void handleIntent(Intent intent) {
|
|
|
|
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
|
|
|
|
String query = intent.getStringExtra(SearchManager.QUERY);
|
|
|
|
filterStationList(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void filterStationList(String searchQuery) {
|
|
|
|
if (allStationsList == null || allStationsList.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(searchQuery.isEmpty())
|
|
|
|
adapter.update(allStationsList);
|
|
|
|
|
|
|
|
List<WeatherStation> newList = allStationsList.stream()
|
|
|
|
.filter(station -> station.getDisplayedName()
|
|
|
|
.toLowerCase(Locale.ROOT).contains(searchQuery.toLowerCase(Locale.ROOT)))
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
adapter.update(newList);
|
2022-05-08 14:42:45 +00:00
|
|
|
}
|
2021-08-11 17:20:03 +00:00
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
EventBus.getDefault().register(this);
|
|
|
|
updateStationList(((Main) getApplication()).getListOfAllStations());
|
|
|
|
}
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
EventBus.getDefault().unregister(this);
|
|
|
|
}
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-05-14 13:11:20 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.main_activity_all_stations, menu);
|
|
|
|
|
|
|
|
// Associate searchable configuration with the SearchView
|
|
|
|
SearchManager searchManager =
|
|
|
|
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
|
|
|
|
SearchView searchView =
|
|
|
|
(SearchView) menu.findItem(R.id.search).getActionView();
|
|
|
|
searchView.setSearchableInfo(
|
|
|
|
searchManager.getSearchableInfo(getComponentName()));
|
2022-06-14 18:13:47 +00:00
|
|
|
searchView.setMaxWidth(Integer.MAX_VALUE);
|
2022-05-14 13:11:20 +00:00
|
|
|
|
|
|
|
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String s) {
|
|
|
|
filterStationList(s);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
searchView.setOnCloseListener(() -> {
|
|
|
|
adapter.update(allStationsList);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
private void updateStationList(List<WeatherStation> stations) {
|
|
|
|
if (stations != null) {
|
|
|
|
refreshLayout.setRefreshing(false);
|
2022-05-14 13:11:20 +00:00
|
|
|
allStationsList = stations;
|
|
|
|
adapter.update(stations);
|
2022-05-08 14:42:45 +00:00
|
|
|
} else {
|
|
|
|
EventBus.getDefault().post(new StartStationsRefreshEvent());
|
|
|
|
}
|
|
|
|
}
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
|
public void allStationsEventHandler(@NonNull AllStationsReceivedEvent event) {
|
|
|
|
updateStationList(event.getStations());
|
|
|
|
}
|
2020-12-16 19:26:53 +00:00
|
|
|
|
2022-05-08 14:42:45 +00:00
|
|
|
@Subscribe(threadMode = ThreadMode.MAIN)
|
|
|
|
public void startStationsRefreshEventHandler(@NonNull StartStationsRefreshEvent event) {
|
|
|
|
refreshLayout.setRefreshing(true);
|
|
|
|
Toast.makeText(this, R.string.refreshing_station_list, Toast.LENGTH_SHORT).show();
|
2020-12-16 19:26:53 +00:00
|
|
|
}
|
|
|
|
}
|