Added EventBus sending event on loading finish.

master
Tomasz Półgrabia 2016-09-24 10:39:36 +02:00
parent d707da1883
commit e551aad598
6 changed files with 134 additions and 23 deletions

View File

@ -76,4 +76,5 @@ dependencies {
exclude group: 'com.google.android', module: 'android' exclude group: 'com.google.android', module: 'android'
} }
compile 'com.google.code.gson:gson:2.7' compile 'com.google.code.gson:gson:2.7'
compile 'org.greenrobot:eventbus:3.0.0'
} }

View File

@ -11,12 +11,16 @@ import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.view.*; import android.view.*;
import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.DisplayImageOptions;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorer.activities.SettingsActivity; import pl.tpolgrabia.urbanexplorer.activities.SettingsActivity;
import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener;
import pl.tpolgrabia.urbanexplorer.dto.MainActivityState; import pl.tpolgrabia.urbanexplorer.dto.MainActivityState;
import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo;
import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorer.events.DataLoadingStartEvent;
import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment;
import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment; import pl.tpolgrabia.urbanexplorer.fragments.PanoramioShowerFragment;
import pl.tpolgrabia.urbanexplorer.fragments.Refreshable; import pl.tpolgrabia.urbanexplorer.fragments.Refreshable;
@ -92,6 +96,8 @@ public class MainActivity extends ActionBarActivity {
lg.trace("onCreate"); lg.trace("onCreate");
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
HelperUtils.initErrorAndDebugHanlers(this); HelperUtils.initErrorAndDebugHanlers(this);
NetUtils.setGlobalProxyAuth(this); NetUtils.setGlobalProxyAuth(this);
@ -327,6 +333,7 @@ public class MainActivity extends ActionBarActivity {
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
lg.trace("onDestroy"); lg.trace("onDestroy");
EventBus.getDefault().unregister(this);
} }
@Override @Override
@ -373,4 +380,15 @@ public class MainActivity extends ActionBarActivity {
public PanoramioImageInfo getPhotoInfo() { public PanoramioImageInfo getPhotoInfo() {
return photoInfo; return photoInfo;
} }
@Subscribe
public void handleLoadingStart(DataLoadingStartEvent event) {
progressDlg.show();
}
@Subscribe
public void handleLoadingFinish(DataLoadingFinishEvent event) {
progressDlg.dismiss();
}
} }

View File

@ -0,0 +1,46 @@
package pl.tpolgrabia.urbanexplorer.events;
/**
* Created by tpolgrabia on 24.09.16.
*/
public class DataLoadingFinishEvent {
private Object source;
private Long time;
public DataLoadingFinishEvent() {
this(null);
}
public DataLoadingFinishEvent(Object source) {
this(source, System.currentTimeMillis());
}
public DataLoadingFinishEvent(Object source, Long time) {
this.source = source;
this.time = time;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
@Override
public String toString() {
return "DataLoadingFinishEvent{" +
"source=" + source +
", time=" + time +
'}';
}
}

View File

@ -0,0 +1,46 @@
package pl.tpolgrabia.urbanexplorer.events;
/**
* Created by tpolgrabia on 24.09.16.
*/
public class DataLoadingStartEvent {
private Object source;
private Long time;
public DataLoadingStartEvent() {
this(null);
}
public DataLoadingStartEvent(Object source) {
this(source, System.currentTimeMillis());
}
public DataLoadingStartEvent(Object source, Long time) {
this.source = source;
this.time = time;
}
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
@Override
public String toString() {
return "DataLoadingStartEvent{" +
"source=" + source +
", time=" + time +
'}';
}
}

View File

@ -11,12 +11,14 @@ import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.R;
import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.callbacks.*;
import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo;
import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorer.handlers.PanoramioItemLongClickHandler; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioItemLongClickHandler;
import pl.tpolgrabia.urbanexplorer.handlers.PanoramioLocationsScrollListener; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioLocationsScrollListener;
import pl.tpolgrabia.urbanexplorer.utils.*; import pl.tpolgrabia.urbanexplorer.utils.*;
@ -49,6 +51,7 @@ public class HomeFragment extends Fragment implements Refreshable {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
lg.trace("onCreate {}", System.identityHashCode(this)); lg.trace("onCreate {}", System.identityHashCode(this));
EventBus.getDefault().register(this);
loading = new Semaphore(1, true); loading = new Semaphore(1, true);
noMorePhotos = false; noMorePhotos = false;
updateLocationInfo(); updateLocationInfo();
@ -57,15 +60,10 @@ public class HomeFragment extends Fragment implements Refreshable {
@Override @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) { public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
initLocationCallback();
}
private void initLocationCallback() {
MainActivity mainActivity = ((MainActivity) getActivity()); MainActivity mainActivity = ((MainActivity) getActivity());
mainActivity.getLocationCallback() final StandardLocationListener locationCallback = mainActivity.getLocationCallback();
.addCallback(new PanoramioLocationCallback(this)); locationCallback.addCallback(new PanoramioLocationCallback(this));
mainActivity.getLocationCallback() locationCallback.addProviderCallback(new PanoramioProviderCallback(this));
.addProviderCallback(new PanoramioProviderCallback(this));
} }
public void updateGeocodedLocation() { public void updateGeocodedLocation() {
@ -124,24 +122,24 @@ public class HomeFragment extends Fragment implements Refreshable {
MainActivity mainActivity = (MainActivity)getActivity(); MainActivity mainActivity = (MainActivity)getActivity();
if (noMorePhotos) { if (noMorePhotos) {
lg.trace("No more photos - last query was zero result"); lg.trace("No more photos - last query was zero result");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
if (!initialized) { if (!initialized) {
lg.trace("Fetching additional photos blocked till system is initialized"); lg.trace("Fetching additional photos blocked till system is initialized");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
if (getView() == null) { if (getView() == null) {
lg.trace("Application still not initialized"); lg.trace("Application still not initialized");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
final Location location = LocationUtils.getLastKnownLocation(activity); final Location location = LocationUtils.getLastKnownLocation(activity);
if (location == null) { if (location == null) {
lg.info("Location still not available"); lg.info("Location still not available");
Toast.makeText(activity, "Location still not available", Toast.LENGTH_SHORT).show(); Toast.makeText(activity, "Location still not available", Toast.LENGTH_SHORT).show();
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
lg.trace("Fetching additional photos. Trying loading acquirng lock"); lg.trace("Fetching additional photos. Trying loading acquirng lock");
@ -202,6 +200,7 @@ public class HomeFragment extends Fragment implements Refreshable {
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
lg.trace("onDestroy"); lg.trace("onDestroy");
EventBus.getDefault().unregister(this);
CacheUtils.savePostsToCache(getActivity(), photos); CacheUtils.savePostsToCache(getActivity(), photos);
} }
@ -227,7 +226,7 @@ public class HomeFragment extends Fragment implements Refreshable {
final Location location = LocationUtils.getLastKnownLocation(activity); final Location location = LocationUtils.getLastKnownLocation(activity);
if (location == null) { if (location == null) {
lg.info("Location is still not available"); lg.info("Location is still not available");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
Toast.makeText(getActivity(), "Location is still not available", Toast.LENGTH_SHORT).show(); Toast.makeText(getActivity(), "Location is still not available", Toast.LENGTH_SHORT).show();
return; return;
} }

View File

@ -15,6 +15,7 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorer.AppConstants; import pl.tpolgrabia.urbanexplorer.AppConstants;
@ -24,6 +25,8 @@ import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter;
import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.callbacks.*;
import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto; import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto;
import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject; import pl.tpolgrabia.urbanexplorer.dto.wiki.app.WikiAppObject;
import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorer.events.DataLoadingStartEvent;
import pl.tpolgrabia.urbanexplorer.utils.*; import pl.tpolgrabia.urbanexplorer.utils.*;
import java.io.*; import java.io.*;
@ -58,6 +61,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
lg.trace("onCreate {}", System.identityHashCode(this)); lg.trace("onCreate {}", System.identityHashCode(this));
EventBus.getDefault().register(this);
appObjects = savedInstanceState == null ? new ArrayList<WikiAppObject>() appObjects = savedInstanceState == null ? new ArrayList<WikiAppObject>()
: (ArrayList<WikiAppObject>)savedInstanceState.getSerializable(WIKI_APP_OBJECTS); : (ArrayList<WikiAppObject>)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);
@ -123,10 +127,6 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
return inflatedView; return inflatedView;
} }
public void clearData() {
appObjects.clear();
}
public void fetchWikiLocations() { public void fetchWikiLocations() {
lg.trace("Fetch wiki locations"); lg.trace("Fetch wiki locations");
@ -140,13 +140,13 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
if (lastFetchSize == 0) { if (lastFetchSize == 0) {
lg.trace("There is no results"); lg.trace("There is no results");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
if (!appObjects.isEmpty()) { if (!appObjects.isEmpty()) {
lg.trace("There are fetched objects"); lg.trace("There are fetched objects");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
@ -155,13 +155,13 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
if (location == null) { if (location == null) {
lg.info("Sorry, location is still not available"); lg.info("Sorry, location is still not available");
Toast.makeText(activity, "Sorry, location is still not available", Toast.LENGTH_SHORT).show(); Toast.makeText(activity, "Sorry, location is still not available", Toast.LENGTH_SHORT).show();
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
if (getView() == null) { if (getView() == null) {
lg.info("Wiki view is not yet initialized"); lg.info("Wiki view is not yet initialized");
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return; return;
} }
@ -198,7 +198,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
return; return;
} }
mainActivity.hideProgress(); EventBus.getDefault().post(new DataLoadingFinishEvent(this));
} }
} }
); );
@ -282,6 +282,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
EventBus.getDefault().unregister(this);
lg.trace("onDestroy {}", System.identityHashCode(this)); lg.trace("onDestroy {}", System.identityHashCode(this));
try (BufferedWriter bw = new BufferedWriter( try (BufferedWriter bw = new BufferedWriter(
@ -315,7 +316,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable {
@Override @Override
public void refresh() { public void refresh() {
clearData(); appObjects.clear();
fetchWikiLocations(); fetchWikiLocations();
} }
} }