Moving loading wiki app objects from cache to utils class.

master
Tomasz Półgrabia 2016-09-24 11:37:04 +02:00
parent 6374d983ba
commit dba3d40db8
3 changed files with 52 additions and 39 deletions

View File

@ -1,9 +1,9 @@
package pl.tpolgrabia.urbanexplorer.fragments;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
@ -11,24 +11,19 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
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;
import pl.tpolgrabia.urbanexplorer.MainActivity;
import pl.tpolgrabia.urbanexplorer.R;
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.RefreshEvent;
import pl.tpolgrabia.urbanexplorer.utils.*;
import java.io.*;
import java.util.ArrayList;
import static android.content.Context.LOCATION_SERVICE;
@ -56,24 +51,12 @@ public class WikiLocationsFragment extends Fragment {
super.onCreate(savedInstanceState);
lg.trace("onCreate {}", System.identityHashCode(this));
EventBus.getDefault().register(this);
appObjects = savedInstanceState == null ? new ArrayList<WikiAppObject>()
: (ArrayList<WikiAppObject>)savedInstanceState.getSerializable(WIKI_APP_OBJECTS);
}
if (appObjects == null) {
try (InputStreamReader ir = new InputStreamReader(
new FileInputStream(
new File(getActivity().getCacheDir(),
AppConstants.WIKI_CACHE_FILENAME)))) {
WikiCacheDto dto = new Gson().fromJson(ir, WikiCacheDto.class);
appObjects = new ArrayList<>(dto.getAppObject());
} catch (FileNotFoundException e) {
lg.error("File not found", e);
} catch (IOException e) {
lg.error("I/O error", e);
}
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
appObjects = CacheUtils.fetchAppObjectsFromCache(getActivity(), savedInstanceState);
}
@Override
@ -105,6 +88,7 @@ public class WikiLocationsFragment extends Fragment {
final FragmentActivity activity = getActivity();
if (activity == null) {
lg.warn("Activity shouldn't be null. No headless fragment");
EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return;
}
@ -120,22 +104,7 @@ public class WikiLocationsFragment extends Fragment {
return;
}
final Location location = LocationUtils.getLastKnownLocation(activity);
if (location == null) {
lg.info("Sorry, location is still not available");
Toast.makeText(activity, "Sorry, location is still not available", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new DataLoadingFinishEvent(this));
return;
}
WikiUtils.fetchAppData(activity,
location.getLatitude(),
location.getLongitude(),
SettingsUtils.fetchRadiusLimit(getActivity()),
SettingsUtils.fetchSearchLimit(getActivity()),
new WikiFetchAppDataCallback(this, activity)
);
WikiUtils.fetchAppData(activity, new WikiFetchAppDataCallback(this, activity));
}
@Override

View File

@ -135,4 +135,26 @@ public class CacheUtils {
lg.error("I/O error", e);
}
}
public static ArrayList<WikiAppObject> fetchAppObjectsFromCache(Context ctx, Bundle savedInstanceState) {
ArrayList<WikiAppObject> appObjects = savedInstanceState == null ? new ArrayList<WikiAppObject>()
: (ArrayList<WikiAppObject>)savedInstanceState.getSerializable(WikiLocationsFragment.WIKI_APP_OBJECTS);
if (appObjects == null) {
try (InputStreamReader ir = new InputStreamReader(
new FileInputStream(
new File(ctx.getCacheDir(),
AppConstants.WIKI_CACHE_FILENAME)))) {
WikiCacheDto dto = new Gson().fromJson(ir, WikiCacheDto.class);
appObjects = new ArrayList<>(dto.getAppObject());
} catch (FileNotFoundException e) {
lg.error("File not found", e);
} catch (IOException e) {
lg.error("I/O error", e);
}
}
return appObjects;
}
}

View File

@ -8,11 +8,13 @@ import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import org.apache.commons.lang3.StringUtils;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiFetchAppDataCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiLocationGeoCoderCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiResponseCallback;
import pl.tpolgrabia.urbanexplorer.callbacks.WikiStatus;
@ -24,6 +26,7 @@ import pl.tpolgrabia.urbanexplorer.dto.wiki.generator.WikiThumbnail;
import pl.tpolgrabia.urbanexplorer.dto.wiki.geosearch.WikiGeoObject;
import pl.tpolgrabia.urbanexplorer.dto.wiki.geosearch.WikiGeoResponse;
import pl.tpolgrabia.urbanexplorer.dto.wiki.geosearch.WikiGeoResponseCallback;
import pl.tpolgrabia.urbanexplorer.events.DataLoadingFinishEvent;
import pl.tpolgrabia.urbanexplorer.fragments.WikiLocationsFragment;
import java.util.*;
@ -375,4 +378,23 @@ public class WikiUtils {
location.getLongitude(),
clbk);
}
public static void fetchAppData(Context ctx, WikiFetchAppDataCallback clbk) {
final Location location = LocationUtils.getLastKnownLocation(ctx);
if (location == null) {
lg.info("Sorry, location is still not available");
Toast.makeText(ctx, "Sorry, location is still not available", Toast.LENGTH_SHORT).show();
EventBus.getDefault().post(new DataLoadingFinishEvent(ctx));
return;
}
fetchAppData(ctx,
location.getLatitude(),
location.getLongitude(),
SettingsUtils.fetchRadiusLimit(ctx),
SettingsUtils.fetchSearchLimit(ctx),
clbk
);
}
}