fixed loading
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
[
|
||||
|
||||
{
|
||||
"code": "2345",
|
||||
"reward": "fdasfasdf"
|
||||
}
|
||||
]
|
||||
@@ -2,7 +2,6 @@
|
||||
{
|
||||
"name": "kees kroket",
|
||||
"coordinates": "2.4654645,6.2342323",
|
||||
"description": "lekkere patatjes",
|
||||
"imageUrl": "R.id.yeet"
|
||||
"description": "lekkere patatjes"
|
||||
}
|
||||
]
|
||||
@@ -1,3 +1,14 @@
|
||||
[
|
||||
|
||||
{
|
||||
"name": "rondje stad",
|
||||
"locations": [
|
||||
{
|
||||
"name": "kees kroket",
|
||||
"coordinates": "2.4654645,6.2342323",
|
||||
"description": "lekkere patatjes"
|
||||
}
|
||||
],
|
||||
"totalDistance": 2.3434,
|
||||
"totalTime": 342342
|
||||
}
|
||||
]
|
||||
@@ -7,10 +7,12 @@ import android.util.Log;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@@ -18,16 +20,29 @@ public class FileIO<T> {
|
||||
private final String TAG = FileIO.class.getCanonicalName();
|
||||
|
||||
|
||||
public T readFileData(Context context, String fileName) {
|
||||
public T readFileData(Context context, String fileName, Type typeOf) {
|
||||
Gson gson = new Gson();
|
||||
AssetManager am = context.getAssets();
|
||||
T res = null;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
try {
|
||||
InputStream is = am.open(fileName);
|
||||
InputStreamReader inputStreamReader = new InputStreamReader(is);
|
||||
res = gson.fromJson(inputStreamReader,new TypeToken<T>(){}.getType());
|
||||
BufferedReader reader = new BufferedReader(inputStreamReader);
|
||||
String line;
|
||||
while ((line = reader.readLine())!= null) {
|
||||
sb.append(line);
|
||||
}
|
||||
|
||||
|
||||
Log.d(TAG, "readFileData: got string: " + sb.toString());
|
||||
res = gson.fromJson(sb.toString(),typeOf);
|
||||
Log.d(TAG, "readFileData: got object: " + res);
|
||||
|
||||
reader.close();
|
||||
inputStreamReader.close();
|
||||
is.close();
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.d(TAG, "readFileData: exception! " + e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -20,8 +20,10 @@ import androidx.core.content.ContextCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.a1.nextlocation.R;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.a1.nextlocation.recyclerview.CouponListManager;
|
||||
import com.a1.nextlocation.recyclerview.LocationListManager;
|
||||
import com.a1.nextlocation.recyclerview.RouteListManager;
|
||||
|
||||
import org.osmdroid.api.IMapController;
|
||||
import org.osmdroid.config.Configuration;
|
||||
@@ -114,8 +116,9 @@ public class HomeFragment extends Fragment {
|
||||
|
||||
}
|
||||
|
||||
LocationListManager locationListManager = new LocationListManager(requireContext());
|
||||
locationListManager.load();
|
||||
CouponListManager couponListManager = new CouponListManager(requireContext());
|
||||
couponListManager.load();
|
||||
Log.d(TAG, "initMap: " + couponListManager.getCoupon(0).getCode());
|
||||
|
||||
}
|
||||
private void requestPermissionsIfNecessary(String... permissions) {
|
||||
|
||||
@@ -1,25 +1,32 @@
|
||||
package com.a1.nextlocation.recyclerview;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.a1.nextlocation.data.Coupon;
|
||||
import com.a1.nextlocation.data.FileIO;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CouponLoader implements Loader<List<Coupon>> {
|
||||
private final Context context;
|
||||
private final String TAG = CouponLoader.class.getCanonicalName();
|
||||
|
||||
public CouponLoader(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Coupon> load() {
|
||||
public ArrayList<Coupon> load() {
|
||||
FileIO<ArrayList<Coupon>> fileIO = new FileIO<>();
|
||||
ArrayList<Coupon> res = fileIO.readFileData(context, "coupons.json");
|
||||
ArrayList<Coupon> res = fileIO.readFileData(context, "coupons.json",new TypeToken<ArrayList<Coupon>>(){}.getType());
|
||||
Log.d(TAG, "load: " + res);
|
||||
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,10 @@ import android.content.Context;
|
||||
|
||||
import com.a1.nextlocation.data.FileIO;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class LocationLoader implements Loader<List<Location>> {
|
||||
@@ -16,11 +18,11 @@ public class LocationLoader implements Loader<List<Location>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Location> load() {
|
||||
public ArrayList<Location> load() {
|
||||
FileIO<ArrayList<Location>> fileIO = new FileIO<>();
|
||||
|
||||
ArrayList<Location> res = fileIO.readFileData(context, "locations.json");
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
ArrayList<Location> res = fileIO.readFileData(context,"locations.json",new TypeToken<ArrayList<Location>>(){}.getType());
|
||||
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import android.content.Context;
|
||||
import com.a1.nextlocation.data.FileIO;
|
||||
import com.a1.nextlocation.data.Location;
|
||||
import com.a1.nextlocation.data.Route;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class RouteLoader implements Loader<List<Route>> {
|
||||
@@ -17,11 +19,10 @@ public class RouteLoader implements Loader<List<Route>> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Route> load() {
|
||||
public ArrayList<Route> load() {
|
||||
|
||||
FileIO<ArrayList<Route>> fileIO = new FileIO<>();
|
||||
|
||||
ArrayList<Route> res = fileIO.readFileData(context, "routes.json");
|
||||
ArrayList<Route> res = fileIO.readFileData(context, "routes.json",new TypeToken<ArrayList<Route>>(){}.getType());
|
||||
return res == null ? new ArrayList<>() : res;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user