From 17d977bb17377919a81781c2789faddf9f0b6202 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 17 Dec 2020 13:41:20 +0100 Subject: [PATCH] fixed loading --- app/src/main/assets/coupons.json | 5 ++++- app/src/main/assets/locations.json | 3 +-- app/src/main/assets/routes.json | 13 ++++++++++++- .../java/com/a1/nextlocation/data/FileIO.java | 19 +++++++++++++++++-- .../nextlocation/fragments/HomeFragment.java | 7 +++++-- .../recyclerview/CouponLoader.java | 11 +++++++++-- .../recyclerview/LocationLoader.java | 8 +++++--- .../recyclerview/RouteLoader.java | 7 ++++--- 8 files changed, 57 insertions(+), 16 deletions(-) diff --git a/app/src/main/assets/coupons.json b/app/src/main/assets/coupons.json index c44dc44..39a85db 100644 --- a/app/src/main/assets/coupons.json +++ b/app/src/main/assets/coupons.json @@ -1,3 +1,6 @@ [ - + { + "code": "2345", + "reward": "fdasfasdf" + } ] \ No newline at end of file diff --git a/app/src/main/assets/locations.json b/app/src/main/assets/locations.json index bebe62d..88cec3e 100644 --- a/app/src/main/assets/locations.json +++ b/app/src/main/assets/locations.json @@ -2,7 +2,6 @@ { "name": "kees kroket", "coordinates": "2.4654645,6.2342323", - "description": "lekkere patatjes", - "imageUrl": "R.id.yeet" + "description": "lekkere patatjes" } ] \ No newline at end of file diff --git a/app/src/main/assets/routes.json b/app/src/main/assets/routes.json index c44dc44..9c84f63 100644 --- a/app/src/main/assets/routes.json +++ b/app/src/main/assets/routes.json @@ -1,3 +1,14 @@ [ - + { + "name": "rondje stad", + "locations": [ + { + "name": "kees kroket", + "coordinates": "2.4654645,6.2342323", + "description": "lekkere patatjes" + } + ], + "totalDistance": 2.3434, + "totalTime": 342342 + } ] \ No newline at end of file diff --git a/app/src/main/java/com/a1/nextlocation/data/FileIO.java b/app/src/main/java/com/a1/nextlocation/data/FileIO.java index 03ad794..a47f76e 100644 --- a/app/src/main/java/com/a1/nextlocation/data/FileIO.java +++ b/app/src/main/java/com/a1/nextlocation/data/FileIO.java @@ -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 { 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(){}.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(); diff --git a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java index 326f020..0b7a629 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -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) { diff --git a/app/src/main/java/com/a1/nextlocation/recyclerview/CouponLoader.java b/app/src/main/java/com/a1/nextlocation/recyclerview/CouponLoader.java index f40fd5f..3cfcf13 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/CouponLoader.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/CouponLoader.java @@ -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> { private final Context context; + private final String TAG = CouponLoader.class.getCanonicalName(); public CouponLoader(Context context) { this.context = context; } @Override - public List load() { + public ArrayList load() { FileIO> fileIO = new FileIO<>(); - ArrayList res = fileIO.readFileData(context, "coupons.json"); + ArrayList res = fileIO.readFileData(context, "coupons.json",new TypeToken>(){}.getType()); + Log.d(TAG, "load: " + res); + return res == null ? new ArrayList<>() : res; + } } diff --git a/app/src/main/java/com/a1/nextlocation/recyclerview/LocationLoader.java b/app/src/main/java/com/a1/nextlocation/recyclerview/LocationLoader.java index fd570bb..f56d8fa 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/LocationLoader.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/LocationLoader.java @@ -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> { @@ -16,11 +18,11 @@ public class LocationLoader implements Loader> { } @Override - public List load() { + public ArrayList load() { FileIO> fileIO = new FileIO<>(); - ArrayList res = fileIO.readFileData(context, "locations.json"); - return res == null ? new ArrayList<>() : res; + ArrayList res = fileIO.readFileData(context,"locations.json",new TypeToken>(){}.getType()); + return res == null ? new ArrayList<>() : res; } } diff --git a/app/src/main/java/com/a1/nextlocation/recyclerview/RouteLoader.java b/app/src/main/java/com/a1/nextlocation/recyclerview/RouteLoader.java index 5b5956f..df65664 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/RouteLoader.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/RouteLoader.java @@ -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> { @@ -17,11 +19,10 @@ public class RouteLoader implements Loader> { } @Override - public List load() { + public ArrayList load() { FileIO> fileIO = new FileIO<>(); - - ArrayList res = fileIO.readFileData(context, "routes.json"); + ArrayList res = fileIO.readFileData(context, "routes.json",new TypeToken>(){}.getType()); return res == null ? new ArrayList<>() : res; }