From b52c5556d296d5c83eee08b09dd3594faf57f990 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 17 Dec 2020 11:50:31 +0100 Subject: [PATCH] finished loading --- app/src/main/assets/coupons.json | 3 +++ app/src/main/assets/locations.json | 8 ++++++++ app/src/main/assets/routes.json | 3 +++ .../java/com/a1/nextlocation/data/FileIO.java | 8 ++++++-- .../a1/nextlocation/fragments/HomeFragment.java | 5 +++++ .../nextlocation/recyclerview/CouponLoader.java | 13 ++++++------- .../nextlocation/recyclerview/LocationLoader.java | 15 +++++++-------- .../a1/nextlocation/recyclerview/RouteLoader.java | 14 +++++++------- 8 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 app/src/main/assets/coupons.json create mode 100644 app/src/main/assets/locations.json create mode 100644 app/src/main/assets/routes.json diff --git a/app/src/main/assets/coupons.json b/app/src/main/assets/coupons.json new file mode 100644 index 0000000..c44dc44 --- /dev/null +++ b/app/src/main/assets/coupons.json @@ -0,0 +1,3 @@ +[ + +] \ No newline at end of file diff --git a/app/src/main/assets/locations.json b/app/src/main/assets/locations.json new file mode 100644 index 0000000..bebe62d --- /dev/null +++ b/app/src/main/assets/locations.json @@ -0,0 +1,8 @@ +[ + { + "name": "kees kroket", + "coordinates": "2.4654645,6.2342323", + "description": "lekkere patatjes", + "imageUrl": "R.id.yeet" + } +] \ No newline at end of file diff --git a/app/src/main/assets/routes.json b/app/src/main/assets/routes.json new file mode 100644 index 0000000..c44dc44 --- /dev/null +++ b/app/src/main/assets/routes.json @@ -0,0 +1,3 @@ +[ + +] \ 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 5672d74..03ad794 100644 --- a/app/src/main/java/com/a1/nextlocation/data/FileIO.java +++ b/app/src/main/java/com/a1/nextlocation/data/FileIO.java @@ -2,6 +2,7 @@ package com.a1.nextlocation.data; import android.content.Context; import android.content.res.AssetManager; +import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; @@ -14,18 +15,21 @@ import java.nio.file.Files; import java.nio.file.Paths; public class FileIO { + private final String TAG = FileIO.class.getCanonicalName(); - public Class readFileData(Context context, String fileName) { + public T readFileData(Context context, String fileName) { Gson gson = new Gson(); AssetManager am = context.getAssets(); - Class res = null; + T res = null; try { InputStream is = am.open(fileName); InputStreamReader inputStreamReader = new InputStreamReader(is); res = gson.fromJson(inputStreamReader,new TypeToken(){}.getType()); + Log.d(TAG, "readFileData: got object: " + res); } catch (IOException e) { + Log.d(TAG, "readFileData: exception! " + e.getLocalizedMessage()); e.printStackTrace(); } return res; 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 2fda536..326f020 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/HomeFragment.java @@ -20,6 +20,8 @@ import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import com.a1.nextlocation.R; +import com.a1.nextlocation.recyclerview.CouponListManager; +import com.a1.nextlocation.recyclerview.LocationListManager; import org.osmdroid.api.IMapController; import org.osmdroid.config.Configuration; @@ -112,6 +114,9 @@ public class HomeFragment extends Fragment { } + LocationListManager locationListManager = new LocationListManager(requireContext()); + locationListManager.load(); + } private void requestPermissionsIfNecessary(String... permissions) { ArrayList permissionsToRequest = new ArrayList<>(); 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 d77a156..f40fd5f 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/CouponLoader.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/CouponLoader.java @@ -6,21 +6,20 @@ import com.a1.nextlocation.data.Coupon; import com.a1.nextlocation.data.FileIO; import com.a1.nextlocation.data.Location; +import java.util.ArrayList; import java.util.List; public class CouponLoader implements Loader> { private final Context context; + public CouponLoader(Context context) { this.context = context; } + @Override public List load() { - FileIO> fileIO = new FileIO<>(); - try { - return fileIO.readFileData(context,"coupons.json").newInstance(); - } catch (IllegalAccessException | InstantiationException e) { - e.printStackTrace(); - } - return null; + FileIO> fileIO = new FileIO<>(); + ArrayList res = fileIO.readFileData(context, "coupons.json"); + 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 6a83ccc..fd570bb 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/LocationLoader.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/LocationLoader.java @@ -5,9 +5,10 @@ import android.content.Context; import com.a1.nextlocation.data.FileIO; import com.a1.nextlocation.data.Location; +import java.util.ArrayList; import java.util.List; -public class LocationLoader implements Loader>{ +public class LocationLoader implements Loader> { private final Context context; public LocationLoader(Context context) { @@ -16,12 +17,10 @@ public class LocationLoader implements Loader>{ @Override public List load() { - FileIO> fileIO = new FileIO<>(); - try { - return fileIO.readFileData(context,"locations.json").newInstance(); - } catch (IllegalAccessException | InstantiationException e) { - e.printStackTrace(); - } - return null; + FileIO> fileIO = new FileIO<>(); + + ArrayList res = fileIO.readFileData(context, "locations.json"); + 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 63f46e3..5b5956f 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/RouteLoader.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/RouteLoader.java @@ -6,10 +6,12 @@ import com.a1.nextlocation.data.FileIO; import com.a1.nextlocation.data.Location; import com.a1.nextlocation.data.Route; +import java.util.ArrayList; import java.util.List; public class RouteLoader implements Loader> { private final Context context; + public RouteLoader(Context context) { this.context = context; } @@ -17,12 +19,10 @@ public class RouteLoader implements Loader> { @Override public List load() { - FileIO> fileIO = new FileIO<>(); - try { - return fileIO.readFileData(context,"routes.json").newInstance(); - } catch (IllegalAccessException | InstantiationException e) { - e.printStackTrace(); - } - return null; + FileIO> fileIO = new FileIO<>(); + + ArrayList res = fileIO.readFileData(context, "routes.json"); + return res == null ? new ArrayList<>() : res; + } }