229 lines
6.6 KiB
Java
229 lines
6.6 KiB
Java
package at.fhj.airkoality.ui.activity;
|
|
|
|
import android.app.ProgressDialog;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.os.Bundle;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.Nullable;
|
|
import android.support.design.widget.BottomNavigationView;
|
|
import android.support.v4.app.Fragment;
|
|
import android.support.v4.app.FragmentTransaction;
|
|
import android.support.v7.app.AppCompatActivity;
|
|
import android.view.Menu;
|
|
import android.view.MenuInflater;
|
|
import android.view.MenuItem;
|
|
|
|
|
|
import org.json.JSONArray;
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import at.fhj.airkoality.R;
|
|
import at.fhj.airkoality.db.room.AirKoalityDB;
|
|
import at.fhj.airkoality.model.Location;
|
|
import at.fhj.airkoality.network.HttpsGetTask;
|
|
import at.fhj.airkoality.network.RequestCallback;
|
|
import at.fhj.airkoality.service.LocationService;
|
|
import at.fhj.airkoality.ui.fragment.LocationListFragment;
|
|
import at.fhj.airkoality.ui.fragment.MapFragment;
|
|
|
|
public class MainActivity extends AppCompatActivity implements RequestCallback {
|
|
|
|
private LocationListFragment locationListFragment;
|
|
private MapFragment mapFragment;
|
|
|
|
private static final String LOCATION_LIST = "location_list";
|
|
private static final String MAP = "map";
|
|
private static final String FRAGMENT_PREF_KEY = "last_fragment";
|
|
|
|
private ProgressDialog progressDialog;
|
|
|
|
private AirKoalityDB database;
|
|
|
|
|
|
|
|
private BottomNavigationView bottomNavigationView;
|
|
|
|
private SharedPreferences sharedPreferences;
|
|
|
|
|
|
@Override
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_main);
|
|
|
|
locationListFragment = new LocationListFragment();
|
|
mapFragment = new MapFragment();
|
|
|
|
bottomNavigationView = findViewById(R.id.bnvMain);
|
|
|
|
progressDialog = new ProgressDialog(this);
|
|
|
|
database = AirKoalityDB.getDatabase(this);
|
|
|
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
|
transaction.add(R.id.flFragmentContainer, locationListFragment);
|
|
transaction.add(R.id.flFragmentContainer, mapFragment);
|
|
|
|
transaction.commit();
|
|
|
|
bottomNavigationView.setOnNavigationItemSelectedListener(menuItem -> {
|
|
|
|
switch (menuItem.getItemId()){
|
|
case R.id.action_location:
|
|
setSelectedFragment(LOCATION_LIST);
|
|
sharedPreferences.edit()
|
|
.putString(FRAGMENT_PREF_KEY, LOCATION_LIST)
|
|
.commit();
|
|
|
|
break;
|
|
case R.id.action_map:
|
|
setSelectedFragment(MAP);
|
|
sharedPreferences.edit()
|
|
.putString(FRAGMENT_PREF_KEY, MAP)
|
|
.commit();
|
|
break;
|
|
}
|
|
return true;
|
|
|
|
});
|
|
setSelectedFragment(sharedPreferences.getString("last_fragment", LOCATION_LIST));
|
|
fetchLocations();
|
|
|
|
}
|
|
|
|
|
|
private void fetchLocations(){
|
|
HttpsGetTask httpsGetTask = new HttpsGetTask(this);
|
|
httpsGetTask.execute("https://api.openaq.org/v1/locations?country=AT&limit=200");
|
|
}
|
|
|
|
|
|
|
|
private void setSelectedFragment(String fragmentName){
|
|
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
|
switch (fragmentName) {
|
|
case LOCATION_LIST:
|
|
transaction.hide(mapFragment);
|
|
transaction.show(locationListFragment);
|
|
break;
|
|
case MAP:
|
|
transaction.hide(locationListFragment);
|
|
transaction.show(mapFragment);
|
|
break;
|
|
}
|
|
transaction.commit();
|
|
}
|
|
|
|
@Override
|
|
public void onRequestStart() {
|
|
progressDialog.setMessage("Fetching...");
|
|
progressDialog.show();
|
|
}
|
|
|
|
@Override
|
|
public void onResult(String result) {
|
|
new Thread(()->{
|
|
|
|
List<Location> locations;
|
|
//parse result
|
|
if(result == null){
|
|
System.out.println("no locations");
|
|
}else {
|
|
//write to db
|
|
try {
|
|
locations = parseLocations(result);
|
|
database.locationDAO().clear();
|
|
database.locationDAO().addAll(locations);
|
|
}catch (JSONException e){
|
|
e.printStackTrace();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//notify fragments
|
|
dismissProgressDialog();
|
|
updateFragments();
|
|
|
|
}).start();
|
|
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
inflater.inflate(R.menu.main_menue, menu);
|
|
return true;
|
|
|
|
}
|
|
|
|
@Override
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()){
|
|
case R.id.action_service_on:
|
|
startLocationService();
|
|
return true;
|
|
case R.id.action_service_off:
|
|
stopLocationService();
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
private void startLocationService(){
|
|
Intent intent = new Intent(this, LocationService.class);
|
|
startService(intent);
|
|
|
|
}
|
|
|
|
private void stopLocationService(){
|
|
|
|
Intent intent = new Intent(this, LocationService.class);
|
|
stopService(intent);
|
|
|
|
}
|
|
|
|
private void updateFragments() {
|
|
runOnUiThread(() -> {
|
|
locationListFragment.refresh();
|
|
mapFragment.refresh();
|
|
});
|
|
}
|
|
|
|
private void dismissProgressDialog() {
|
|
runOnUiThread(()-> progressDialog.dismiss());
|
|
}
|
|
|
|
private List<Location> parseLocations(String json) throws JSONException {
|
|
List<Location> locations = new ArrayList<>();
|
|
JSONObject jsonObject = new JSONObject(json);
|
|
JSONArray results = jsonObject.getJSONArray("results");
|
|
|
|
for (int i = 0; i < results.length(); i++){
|
|
JSONObject locationResult = results.getJSONObject(i);
|
|
String locationName = locationResult.getString("location");
|
|
String city = locationResult.getString("city");
|
|
String country = locationResult.getString("country");
|
|
|
|
Location location = new Location(locationName, city, country);
|
|
locations.add(location);
|
|
}
|
|
return locations;
|
|
}
|
|
}
|