diff --git a/app/src/main/java/com/a1/nextlocation/data/Route.java b/app/src/main/java/com/a1/nextlocation/data/Route.java index 7ff67dc..baf6016 100644 --- a/app/src/main/java/com/a1/nextlocation/data/Route.java +++ b/app/src/main/java/com/a1/nextlocation/data/Route.java @@ -1,5 +1,8 @@ package com.a1.nextlocation.data; +import android.os.Parcel; +import android.os.Parcelable; + import androidx.annotation.NonNull; import org.jetbrains.annotations.NotNull; @@ -7,7 +10,7 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; -public class Route { +public class Route implements Parcelable { @NonNull @@ -29,6 +32,25 @@ public class Route { } + protected Route(Parcel in) { + this.name = in.readString(); + this.locations = in.createTypedArrayList(Location.CREATOR); + this.totalDistance = in.readFloat(); + this.totalTime = in.readInt(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public Route createFromParcel(Parcel in) { + return new Route(in); + } + + @Override + public Route[] newArray(int size) { + return new Route[size]; + } + }; + public void addLocation(Location location) { this.locations.add(location); } @@ -69,4 +91,16 @@ public class Route { this.totalTime = totalTime; } + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(name); + parcel.writeTypedList(locations); + parcel.writeFloat(totalDistance); + parcel.writeInt(totalTime); + } } diff --git a/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java index 4bc159d..26c53a2 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/LocationFragment.java @@ -36,21 +36,24 @@ public class LocationFragment extends Fragment { View view = inflater.inflate(R.layout.fragment_location, container, false); - locationRecyclerView = view.findViewById(R.id.locationRecyclerView); - locationRecyclerView.setHasFixedSize(true); - layoutManager = new LinearLayoutManager(this.getContext()); - LocationListManager locationListManager = new LocationListManager(this.getContext()); - locationListManager.load(); - locationList = locationListManager.getLocationList(); - locationAdapter = new LocationAdapter(this.getContext(), locationList, clickedPosition -> { + this.locationRecyclerView = view.findViewById(R.id.locationRecyclerView); + this.locationRecyclerView.setHasFixedSize(true); + this.layoutManager = new LinearLayoutManager(this.getContext()); + + LocationListManager.INSTANCE.setContext(this.getContext()); + LocationListManager.INSTANCE.load(); + this.locationList = LocationListManager.INSTANCE.getLocationList(); + + this.locationAdapter = new LocationAdapter(this.getContext(), this.locationList, clickedPosition -> { LocationDetailFragment locationDetailFragment = new LocationDetailFragment(); Bundle locationBundle = new Bundle(); - locationBundle.putParcelable("location", locationList.get(clickedPosition)); + locationBundle.putParcelable("location", this.locationList.get(clickedPosition)); locationDetailFragment.setArguments(locationBundle); ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, locationDetailFragment).addToBackStack(null).commit(); }); - locationRecyclerView.setLayoutManager(layoutManager); - locationRecyclerView.setAdapter(locationAdapter); + + this.locationRecyclerView.setLayoutManager(this.layoutManager); + this.locationRecyclerView.setAdapter(this.locationAdapter); return view; } } \ No newline at end of file diff --git a/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java b/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java index 59043de..9ac07c3 100644 --- a/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java +++ b/app/src/main/java/com/a1/nextlocation/fragments/RouteFragment.java @@ -3,15 +3,29 @@ package com.a1.nextlocation.fragments; import android.os.Bundle; import androidx.fragment.app.Fragment; +import androidx.fragment.app.FragmentActivity; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.a1.nextlocation.R; +import com.a1.nextlocation.data.Route; +import com.a1.nextlocation.recyclerview.RouteAdapter; +import com.a1.nextlocation.recyclerview.RouteListManager; + +import java.util.ArrayList; +import java.util.List; public class RouteFragment extends Fragment { + private RecyclerView routeRecyclerView; + private RecyclerView.LayoutManager layoutManager; + private List routeList; + private RouteAdapter routeAdapter; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -19,9 +33,27 @@ public class RouteFragment extends Fragment { } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - // Inflate the layout for this fragment - return inflater.inflate(R.layout.fragment_route, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_route, container, false); + + this.routeRecyclerView = view.findViewById(R.id.routeRecyclerView); + this.routeRecyclerView.setHasFixedSize(true); + this.layoutManager = new LinearLayoutManager(this.getContext()); + + RouteListManager.INSTANCE.setContext(this.getContext()); + RouteListManager.INSTANCE.load(); + this.routeList = RouteListManager.INSTANCE.getRouteList(); + + this.routeAdapter = new RouteAdapter(this.getContext(), this.routeList, clickedPosition -> { + RouteDetailFragment routeDetailFragment = new RouteDetailFragment(); + Bundle routeBundle = new Bundle(); + routeBundle.putParcelable("route", this.routeList.get(clickedPosition)); + routeDetailFragment.setArguments(routeBundle); + ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_layout, routeDetailFragment).addToBackStack(null).commit(); + }); + + this.routeRecyclerView.setLayoutManager(this.layoutManager); + this.routeRecyclerView.setAdapter(this.routeAdapter); + return view; } } \ No newline at end of file diff --git a/app/src/main/java/com/a1/nextlocation/recyclerview/RouteAdapter.java b/app/src/main/java/com/a1/nextlocation/recyclerview/RouteAdapter.java index 693945b..6664c6c 100644 --- a/app/src/main/java/com/a1/nextlocation/recyclerview/RouteAdapter.java +++ b/app/src/main/java/com/a1/nextlocation/recyclerview/RouteAdapter.java @@ -1,12 +1,17 @@ package com.a1.nextlocation.recyclerview; import android.content.Context; +import android.util.Log; +import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; +import com.a1.nextlocation.R; +import com.a1.nextlocation.data.Location; import com.a1.nextlocation.data.Route; import java.util.List; @@ -23,14 +28,23 @@ public class RouteAdapter extends RecyclerView.Adapter route, CouponAdapter.OnItemClickListener listener){ @@ -42,12 +56,15 @@ public class RouteAdapter extends RecyclerView.Adapter