99import androidx .constraintlayout .widget .ConstraintLayout ;
1010import androidx .fragment .app .Fragment ;
1111import androidx .core .content .res .ResourcesCompat ;
12+ import androidx .lifecycle .Lifecycle ;
13+ import androidx .lifecycle .LifecycleObserver ;
14+ import androidx .lifecycle .OnLifecycleEvent ;
15+ import androidx .lifecycle .ViewModelProvider ;
1216import androidx .recyclerview .widget .LinearLayoutManager ;
1317import androidx .recyclerview .widget .RecyclerView ;
1418import android .text .TextUtils ;
3135
3236import org .nitri .opentopo .adapter .WayPointListAdapter ;
3337import org .nitri .opentopo .domain .DistancePoint ;
38+ import org .nitri .opentopo .model .GpxViewModel ;
3439import org .nitri .opentopo .model .WayPointHeaderItem ;
3540import org .nitri .opentopo .model .WayPointItem ;
3641import org .nitri .opentopo .model .WayPointListItem ;
4954public class GpxDetailFragment extends Fragment implements WayPointListAdapter .OnItemClickListener , WayPointDetailDialogFragment .Callback {
5055
5156 private OnFragmentInteractionListener mListener ;
52- private Gpx mGpx ;
5357 private List <DistancePoint > mTrackDistanceLine ;
5458 private double mDistance ;
5559 private boolean mElevation ;
@@ -66,6 +70,8 @@ public class GpxDetailFragment extends Fragment implements WayPointListAdapter.O
6670 private WayPointListAdapter mWayPointListAdapter ;
6771 private WebView wvDescription ;
6872 private int mSelectedIndex ;
73+ private GpxViewModel mGpxViewModel ;
74+ private ConstraintLayout chartContainer ;
6975
7076
7177 public GpxDetailFragment () {
@@ -74,25 +80,13 @@ public GpxDetailFragment() {
7480
7581
7682 public static GpxDetailFragment newInstance () {
77- GpxDetailFragment fragment = new GpxDetailFragment ();
78- return fragment ;
83+ return new GpxDetailFragment ();
7984 }
8085
8186 @ Override
8287 public void onCreate (Bundle savedInstanceState ) {
8388 super .onCreate (savedInstanceState );
8489 setHasOptionsMenu (true );
85- setRetainInstance (true );
86- mGpx = mListener .getGpx ();
87- if (mGpx != null && mGpx .getTracks () != null ) {
88- for (Track track : mGpx .getTracks ()) {
89- buildTrackDistanceLine (track );
90- }
91- if (getActivity () != null ) {
92- mTfRegular = Typeface .createFromAsset (getActivity ().getAssets (), "OpenSans-Regular.ttf" );
93- mTfLight = Typeface .createFromAsset (getActivity ().getAssets (), "OpenSans-Light.ttf" );
94- }
95- }
9690 mWayPointListAdapter = new WayPointListAdapter (mWayPointListItems , this );
9791 }
9892
@@ -105,35 +99,41 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
10599 wvDescription = rootView .findViewById (R .id .wvDescription );
106100 wvDescription .setBackgroundColor (Color .TRANSPARENT );
107101 tvLength = rootView .findViewById (R .id .tvLength );
108- ConstraintLayout chartContainer = rootView .findViewById (R .id .chartContainer );
102+ chartContainer = rootView .findViewById (R .id .chartContainer );
109103 mElevationChart = rootView .findViewById (R .id .elevationChart );
110104 RecyclerView wayPointRecyclerView = rootView .findViewById (R .id .way_point_recycler_view );
111105 wayPointRecyclerView .setNestedScrollingEnabled (false );
112106 wayPointRecyclerView .setLayoutManager (new LinearLayoutManager (getActivity ()));
113107 wayPointRecyclerView .setAdapter (mWayPointListAdapter );
114108
115109
116- if (mElevation ) {
117- setUpElevationChart ();
118- setChartData ();
119- } else {
120- chartContainer .setVisibility (View .GONE );
121- }
122110 return rootView ;
123111 }
124112
125113 @ Override
126114 public void onViewCreated (@ NonNull View view , @ Nullable Bundle savedInstanceState ) {
127115 super .onViewCreated (view , savedInstanceState );
128116
117+ mGpxViewModel = new ViewModelProvider (requireActivity ()).get (GpxViewModel .class );
118+
119+ if (mGpxViewModel .gpx != null && mGpxViewModel .gpx .getTracks () != null ) {
120+ for (Track track : mGpxViewModel .gpx .getTracks ()) {
121+ buildTrackDistanceLine (track );
122+ }
123+ /* if (getActivity() != null) {
124+ mTfRegular = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Regular.ttf");
125+ mTfLight = Typeface.createFromAsset(getActivity().getAssets(), "OpenSans-Light.ttf");
126+ }*/
127+ }
128+
129129 // For now, use title and description of first track
130- if (mGpx != null && mGpx . getTracks () != null && mGpx .getTracks ().get (0 ) != null ) {
131- if (TextUtils .isEmpty (mGpx .getTracks ().get (0 ).getTrackName ())) {
130+ if (mGpxViewModel . gpx != null && mGpxViewModel . gpx . getTracks () != null && mGpxViewModel . gpx .getTracks ().get (0 ) != null ) {
131+ if (TextUtils .isEmpty (mGpxViewModel . gpx .getTracks ().get (0 ).getTrackName ())) {
132132 tvName .setVisibility (View .GONE );
133133 } else {
134- tvName .setText (mGpx .getTracks ().get (0 ).getTrackName ());
134+ tvName .setText (mGpxViewModel . gpx .getTracks ().get (0 ).getTrackName ());
135135 }
136- String description = mGpx .getTracks ().get (0 ).getTrackDesc ();
136+ String description = mGpxViewModel . gpx .getTracks ().get (0 ).getTrackDesc ();
137137 if (TextUtils .isEmpty (description )) {
138138 tvDescription .setVisibility (View .GONE );
139139 wvDescription .setVisibility (View .GONE );
@@ -151,13 +151,20 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
151151 }
152152 }
153153
154+ if (mElevation ) {
155+ setUpElevationChart ();
156+ setChartData ();
157+ } else {
158+ chartContainer .setVisibility (View .GONE );
159+ }
160+
154161 if (mDistance > 0 ) {
155162 tvLength .setText (String .format (Locale .getDefault (), "%.2f km" , mDistance / 1000f ));
156163 } else {
157164 tvLength .setVisibility (View .GONE );
158165 }
159166
160- if (mGpx != null && mGpx .getWayPoints () != null ) {
167+ if (mGpxViewModel . gpx != null && mGpxViewModel . gpx .getWayPoints () != null ) {
161168 buildWayPointList ();
162169 mWayPointListAdapter .notifyDataSetChanged ();
163170 }
@@ -201,10 +208,10 @@ private void buildWayPointList() {
201208 String defaultType = getString (R .string .poi );
202209 List <WayPoint > wayPoints ;
203210 mWayPointListItems .clear ();
204- for (String type : Util .getWayPointTypes (mGpx , defaultType )) {
205- wayPoints = Util .getWayPointsByType (mGpx , type );
211+ for (String type : Util .getWayPointTypes (mGpxViewModel . gpx , defaultType )) {
212+ wayPoints = Util .getWayPointsByType (mGpxViewModel . gpx , type );
206213 if (type .equals (defaultType ))
207- wayPoints .addAll (Util .getWayPointsByType (mGpx , null ));
214+ wayPoints .addAll (Util .getWayPointsByType (mGpxViewModel . gpx , null ));
208215 if (wayPoints .size () > 0 ) {
209216 mWayPointListItems .add (new WayPointHeaderItem (type ));
210217 for (WayPoint wayPoint : wayPoints ) {
0 commit comments