diff --git a/app/build.gradle b/app/build.gradle index 1bda5e2..e2d6d90 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -76,4 +76,5 @@ dependencies { exclude group: 'com.google.android', module: 'android' } compile 'com.google.code.gson:gson:2.7' + compile 'org.greenrobot:eventbus:3.0.0' } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index b48b171..f1192e5 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -11,12 +11,16 @@ import android.support.v4.view.GestureDetectorCompat; import android.support.v7.app.ActionBarActivity; import android.view.*; import com.nostra13.universalimageloader.core.DisplayImageOptions; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.activities.SettingsActivity; import pl.tpolgrabia.urbanexplorer.callbacks.StandardLocationListener; import pl.tpolgrabia.urbanexplorer.dto.MainActivityState; 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.PanoramioShowerFragment; import pl.tpolgrabia.urbanexplorer.fragments.Refreshable; @@ -92,6 +96,8 @@ public class MainActivity extends ActionBarActivity { lg.trace("onCreate"); setContentView(R.layout.activity_main); + EventBus.getDefault().register(this); + HelperUtils.initErrorAndDebugHanlers(this); NetUtils.setGlobalProxyAuth(this); @@ -327,6 +333,7 @@ public class MainActivity extends ActionBarActivity { protected void onDestroy() { super.onDestroy(); lg.trace("onDestroy"); + EventBus.getDefault().unregister(this); } @Override @@ -373,4 +380,15 @@ public class MainActivity extends ActionBarActivity { public PanoramioImageInfo getPhotoInfo() { return photoInfo; } + + @Subscribe + public void handleLoadingStart(DataLoadingStartEvent event) { + progressDlg.show(); + } + + @Subscribe + public void handleLoadingFinish(DataLoadingFinishEvent event) { + progressDlg.dismiss(); + } + } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/DataLoadingFinishEvent.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/DataLoadingFinishEvent.java new file mode 100644 index 0000000..992cc9f --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/DataLoadingFinishEvent.java @@ -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 + + '}'; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/DataLoadingStartEvent.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/DataLoadingStartEvent.java new file mode 100644 index 0000000..5b1a131 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/DataLoadingStartEvent.java @@ -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 + + '}'; + } +} diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index cdf302a..2df7e66 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -11,12 +11,14 @@ import android.view.ViewGroup; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; +import org.greenrobot.eventbus.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.MainActivity; import pl.tpolgrabia.urbanexplorer.R; import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.dto.panoramio.PanoramioImageInfo; +import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioItemLongClickHandler; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioLocationsScrollListener; import pl.tpolgrabia.urbanexplorer.utils.*; @@ -49,6 +51,7 @@ public class HomeFragment extends Fragment implements Refreshable { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lg.trace("onCreate {}", System.identityHashCode(this)); + EventBus.getDefault().register(this); loading = new Semaphore(1, true); noMorePhotos = false; updateLocationInfo(); @@ -57,15 +60,10 @@ public class HomeFragment extends Fragment implements Refreshable { @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - initLocationCallback(); - } - - private void initLocationCallback() { MainActivity mainActivity = ((MainActivity) getActivity()); - mainActivity.getLocationCallback() - .addCallback(new PanoramioLocationCallback(this)); - mainActivity.getLocationCallback() - .addProviderCallback(new PanoramioProviderCallback(this)); + final StandardLocationListener locationCallback = mainActivity.getLocationCallback(); + locationCallback.addCallback(new PanoramioLocationCallback(this)); + locationCallback.addProviderCallback(new PanoramioProviderCallback(this)); } public void updateGeocodedLocation() { @@ -124,24 +122,24 @@ public class HomeFragment extends Fragment implements Refreshable { MainActivity mainActivity = (MainActivity)getActivity(); if (noMorePhotos) { lg.trace("No more photos - last query was zero result"); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } if (!initialized) { lg.trace("Fetching additional photos blocked till system is initialized"); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } if (getView() == null) { lg.trace("Application still not initialized"); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } final Location location = LocationUtils.getLastKnownLocation(activity); if (location == null) { lg.info("Location still not available"); Toast.makeText(activity, "Location still not available", Toast.LENGTH_SHORT).show(); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } lg.trace("Fetching additional photos. Trying loading acquirng lock"); @@ -202,6 +200,7 @@ public class HomeFragment extends Fragment implements Refreshable { public void onDestroy() { super.onDestroy(); lg.trace("onDestroy"); + EventBus.getDefault().unregister(this); CacheUtils.savePostsToCache(getActivity(), photos); } @@ -227,7 +226,7 @@ public class HomeFragment extends Fragment implements Refreshable { final Location location = LocationUtils.getLastKnownLocation(activity); if (location == null) { 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(); return; } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java index 59b13a2..c514628 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -15,6 +15,7 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; +import org.greenrobot.eventbus.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.AppConstants; @@ -24,6 +25,8 @@ import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter; import pl.tpolgrabia.urbanexplorer.callbacks.*; import pl.tpolgrabia.urbanexplorer.dto.wiki.WikiCacheDto; 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 java.io.*; @@ -58,6 +61,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lg.trace("onCreate {}", System.identityHashCode(this)); + EventBus.getDefault().register(this); appObjects = savedInstanceState == null ? new ArrayList() : (ArrayList)savedInstanceState.getSerializable(WIKI_APP_OBJECTS); @@ -123,10 +127,6 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { return inflatedView; } - public void clearData() { - appObjects.clear(); - } - public void fetchWikiLocations() { lg.trace("Fetch wiki locations"); @@ -140,13 +140,13 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { if (lastFetchSize == 0) { lg.trace("There is no results"); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } if (!appObjects.isEmpty()) { lg.trace("There are fetched objects"); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } @@ -155,13 +155,13 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { if (location == null) { lg.info("Sorry, location is still not available"); Toast.makeText(activity, "Sorry, location is still not available", Toast.LENGTH_SHORT).show(); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } if (getView() == null) { lg.info("Wiki view is not yet initialized"); - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); return; } @@ -198,7 +198,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { return; } - mainActivity.hideProgress(); + EventBus.getDefault().post(new DataLoadingFinishEvent(this)); } } ); @@ -282,6 +282,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { @Override public void onDestroy() { super.onDestroy(); + EventBus.getDefault().unregister(this); lg.trace("onDestroy {}", System.identityHashCode(this)); try (BufferedWriter bw = new BufferedWriter( @@ -315,7 +316,7 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { @Override public void refresh() { - clearData(); + appObjects.clear(); fetchWikiLocations(); } }