From 90c0a70b4040b424a55502df64b823622dcbc646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20P=C3=B3=C5=82grabia?= Date: Sat, 24 Sep 2016 10:47:03 +0200 Subject: [PATCH] Refresh event added and compile errors fixed. --- .../callbacks/PanoramioProviderCallback.java | 4 +- .../urbanexplorer/events/RefreshEvent.java | 48 +++++++++++++++++++ .../urbanexplorer/fragments/HomeFragment.java | 12 +++-- .../fragments/WikiLocationsFragment.java | 8 ++-- .../urbanexplorer/utils/SettingsUtils.java | 2 +- 5 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 app/src/main/java/pl/tpolgrabia/urbanexplorer/events/RefreshEvent.java diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java index 3165307..2049d81 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/callbacks/PanoramioProviderCallback.java @@ -1,7 +1,9 @@ package pl.tpolgrabia.urbanexplorer.callbacks; +import org.greenrobot.eventbus.EventBus; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import pl.tpolgrabia.urbanexplorer.events.RefreshEvent; import pl.tpolgrabia.urbanexplorer.fragments.HomeFragment; /** @@ -19,7 +21,7 @@ public class PanoramioProviderCallback implements ProviderStatusCallback { public void callback(String provider, boolean enabled) { if (enabled) { lg.trace("Handling provider enabling - refreshing panoramio listing"); - homeFragment.refresh(); + EventBus.getDefault().post(new RefreshEvent(this)); } } } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/RefreshEvent.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/RefreshEvent.java new file mode 100644 index 0000000..99af6a8 --- /dev/null +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/events/RefreshEvent.java @@ -0,0 +1,48 @@ +package pl.tpolgrabia.urbanexplorer.events; + +import java.sql.Ref; + +/** + * Created by tpolgrabia on 24.09.16. + */ +public class RefreshEvent { + private Object source; + private Long time; + + public RefreshEvent() { + this(null); + } + + public RefreshEvent(Object source) { + this(source, System.currentTimeMillis()); + } + + public RefreshEvent(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 "RefreshEvent{" + + "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 2df7e66..e96263a 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -12,6 +12,7 @@ import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.MainActivity; @@ -19,6 +20,7 @@ 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.events.RefreshEvent; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioItemLongClickHandler; import pl.tpolgrabia.urbanexplorer.handlers.PanoramioLocationsScrollListener; import pl.tpolgrabia.urbanexplorer.utils.*; @@ -30,7 +32,7 @@ import java.util.concurrent.Semaphore; /** * A simple {@link Fragment} subclass. */ -public class HomeFragment extends Fragment implements Refreshable { +public class HomeFragment extends Fragment { private static final Logger lg = LoggerFactory.getLogger(HomeFragment.class); @@ -148,7 +150,7 @@ public class HomeFragment extends Fragment implements Refreshable { return; } int offset = photos.size(); - lg.debug("Fetching additional photos offset: {}, count: {}", offset, SettingsUtils.getPanoramioBulkDataSize(this)); + lg.debug("Fetching additional photos offset: {}, count: {}", offset, SettingsUtils.getPanoramioBulkDataSize(getActivity())); PanoramioUtils.fetchPanoramioImages( activity, location.getLatitude(), @@ -162,7 +164,7 @@ public class HomeFragment extends Fragment implements Refreshable { } private Long fetchLocationPageSize() { - return Long.valueOf(SettingsUtils.getPanoramioBulkDataSize(this)); + return Long.valueOf(SettingsUtils.getPanoramioBulkDataSize(getActivity())); } @Override @@ -212,8 +214,8 @@ public class HomeFragment extends Fragment implements Refreshable { lg.trace("Saved photos: {}", photos); } - @Override - public void refresh() { + @Subscribe + public void refresh(RefreshEvent refreshEvent) { lg.trace("Fetch panoramio photos"); final FragmentActivity activity = getActivity(); if (activity == null) { 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 c514628..56a12b3 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/WikiLocationsFragment.java @@ -16,6 +16,7 @@ import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import pl.tpolgrabia.urbanexplorer.AppConstants; @@ -27,6 +28,7 @@ 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.events.RefreshEvent; import pl.tpolgrabia.urbanexplorer.utils.*; import java.io.*; @@ -39,7 +41,7 @@ import static android.content.Context.LOCATION_SERVICE; /** * A simple {@link Fragment} subclass. */ -public class WikiLocationsFragment extends Fragment implements Refreshable { +public class WikiLocationsFragment extends Fragment { private static final Logger lg = LoggerFactory.getLogger(WikiLocationsFragment.class); private static final String CLASS_TAG = WikiLocationsFragment.class.getSimpleName(); @@ -314,8 +316,8 @@ public class WikiLocationsFragment extends Fragment implements Refreshable { } } - @Override - public void refresh() { + @Subscribe + public void refresh(RefreshEvent event) { appObjects.clear(); fetchWikiLocations(); } diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java index bae49b6..2e36bfc 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/utils/SettingsUtils.java @@ -35,7 +35,7 @@ public class SettingsUtils { } public static int getPanoramioBulkDataSize(Context ctx) { - SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx.getActivity()); + SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(ctx); final String sValue = sharedPrefs.getString(AppConstants.PANORAMIO_BULK_SIZE_KEY, String.valueOf(AppConstants.PANORAMIO_BULK_SIZE_DEF_VALUE)); try {