diff --git a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt --- a/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt +++ b/app/src/main/java/it/reyboz/bustorino/backend/mato/MQTTMatoClient.kt @@ -125,6 +125,12 @@ } removed = done || removed } + // remove lines that have no responders + for(line in respondersMap.keys){ + if(respondersMap[line]?.isEmpty() == true){ + respondersMap.remove(line) + } + } Log.d(DEBUG_TAG, "Removed: $removed, respondersMap: $respondersMap") } fun getPositions(): PositionsMap{ diff --git a/app/src/main/java/it/reyboz/bustorino/data/OldDataRepository.java b/app/src/main/java/it/reyboz/bustorino/data/OldDataRepository.java deleted file mode 100644 --- a/app/src/main/java/it/reyboz/bustorino/data/OldDataRepository.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - BusTO - Data components - Copyright (C) 2021 Fabio Mazza - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - */ -package it.reyboz.bustorino.data; - -import android.database.sqlite.SQLiteDatabase; -import it.reyboz.bustorino.backend.Result; -import it.reyboz.bustorino.backend.Stop; - -import java.util.List; -import java.util.concurrent.Executor; - -public class OldDataRepository { - - private final Executor executor; - private final NextGenDB nextGenDB; - - public OldDataRepository(Executor executor, final NextGenDB nextGenDB) { - this.executor = executor; - this.nextGenDB = nextGenDB; - } - - public void requestStopsWithGtfsIDs(final List gtfsIDs, - final Callback> callback){ - executor.execute(() -> { - - try { - //final NextGenDB dbHelper = new NextGenDB(context); - final SQLiteDatabase db = nextGenDB.getReadableDatabase(); - - final List stops = NextGenDB.queryAllStopsWithGtfsIDs(db, gtfsIDs); - //Result> result = Result.success; - - callback.onComplete(Result.success(stops)); - } catch (Exception e){ - callback.onComplete(Result.failure(e)); - } - }); - } - - public interface Callback{ - void onComplete(Result result); - } -} diff --git a/app/src/main/java/it/reyboz/bustorino/data/OldDataRepository.kt b/app/src/main/java/it/reyboz/bustorino/data/OldDataRepository.kt new file mode 100644 --- /dev/null +++ b/app/src/main/java/it/reyboz/bustorino/data/OldDataRepository.kt @@ -0,0 +1,69 @@ +/* + BusTO - Data components + Copyright (C) 2021 Fabio Mazza + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ +package it.reyboz.bustorino.data + +import android.content.Context +import it.reyboz.bustorino.backend.Result +import it.reyboz.bustorino.backend.Stop +import java.util.concurrent.Executor + +class OldDataRepository(private val executor: Executor, private val nextGenDB: NextGenDB) { + + constructor(executor: Executor, context: Context): this(executor, NextGenDB.getInstance(context)) + fun requestStopsWithGtfsIDs( + gtfsIDs: List?, + callback: Callback> + ) { + executor.execute { + try { + //final NextGenDB dbHelper = new NextGenDB(context); + val db = nextGenDB.readableDatabase + val stops: List = NextGenDB.queryAllStopsWithGtfsIDs(db, gtfsIDs) + //Result> result = Result.success; + callback.onComplete(Result.success(stops)) + } catch (e: Exception) { + callback.onComplete(Result.failure(e)) + } + } + } + + fun requestStopsInArea( + latitFrom: Double, + latitTo: Double, + longitFrom: Double, + longitTo: Double, + callback: Callback> + ){ + //Log.d(DEBUG_TAG, "Async Stop Fetcher started working"); + executor.execute { + val stops = nextGenDB.queryAllInsideMapView( + latitFrom, latitTo, + longitFrom, longitTo + ) + if (stops!=null) + callback.onComplete(Result.success(stops)) + } + + } + + + + fun interface Callback { + fun onComplete(result: Result) + } +} diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/FragmentHelper.java b/app/src/main/java/it/reyboz/bustorino/fragments/FragmentHelper.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/FragmentHelper.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/FragmentHelper.java @@ -154,7 +154,7 @@ attachFragmentToContainer(managerWeakRef.get(),listfragment, new AttachParameters("search_"+query, false,addToBackStack)); listfragment.setStopList(resultList); - listenerMain.readyGUIfor(FragmentKind.STOPS); + //listenerMain.readyGUIfor(FragmentKind.STOPS); toggleSpinner(false); } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt @@ -53,6 +53,7 @@ import it.reyboz.bustorino.map.CustomInfoWindow.TouchResponder import it.reyboz.bustorino.map.MapViewModel import it.reyboz.bustorino.map.MarkerUtils +import it.reyboz.bustorino.viewmodels.LinesViewModel import it.reyboz.bustorino.viewmodels.MQTTPositionsViewModel import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -333,7 +334,6 @@ fragmentListener = context } else throw RuntimeException("$context must implement CommonFragmentListener") - fragmentListener.readyGUIfor(FragmentKind.LINES) } @@ -662,6 +662,9 @@ //controller.setZoom() } + //initialize GUI here + fragmentListener.readyGUIfor(FragmentKind.LINES) + } override fun onPause() { diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesFragment.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesFragment.kt @@ -35,9 +35,9 @@ import it.reyboz.bustorino.backend.Stop import it.reyboz.bustorino.data.gtfs.GtfsRoute import it.reyboz.bustorino.data.gtfs.MatoPatternWithStops -import it.reyboz.bustorino.data.gtfs.PatternStop import it.reyboz.bustorino.util.LinesNameSorter import it.reyboz.bustorino.util.PatternWithStopsSorter +import it.reyboz.bustorino.viewmodels.LinesViewModel class LinesFragment : ScreenBaseFragment() { diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesGridShowingFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesGridShowingFragment.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesGridShowingFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesGridShowingFragment.kt @@ -194,7 +194,6 @@ fragmentListener = context } else throw RuntimeException("$context must implement CommonFragmentListener") - fragmentListener.readyGUIfor(FragmentKind.LINES) } override fun getBaseViewForSnackBar(): View? { @@ -237,6 +236,8 @@ arrows[AG_EXTRAURB]?.rotation=0f } } + fragmentListener.readyGUIfor(FragmentKind.LINES) + } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MainScreenFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/MainScreenFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/MainScreenFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MainScreenFragment.java @@ -671,7 +671,7 @@ if (!(existingFrag instanceof NearbyStopsFragment)){ Log.d(DEBUG_TAG, "actually showing Nearby Stops Fragment"); //there is no fragment showing - final NearbyStopsFragment fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.TYPE_STOPS); + final NearbyStopsFragment fragment = NearbyStopsFragment.newInstance(NearbyStopsFragment.FragType.STOPS); FragmentTransaction ft = fragMan.beginTransaction(); diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapFragment.java @@ -51,6 +51,7 @@ import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops; import it.reyboz.bustorino.map.*; import it.reyboz.bustorino.viewmodels.MQTTPositionsViewModel; +import it.reyboz.bustorino.viewmodels.StopsMapViewModel; import org.osmdroid.api.IGeoPoint; import org.osmdroid.api.IMapController; import org.osmdroid.config.Configuration; @@ -104,8 +105,6 @@ protected FragmentListenerMain listenerMain; private HashSet shownStops = null; - //the asynctask used to get the stops from the database - private AsyncStopFetcher stopFetcher = null; private MapView map = null; @@ -118,6 +117,9 @@ private boolean hasMapStartFinished = false; private boolean followingLocation = false; + //the ViewModel from which we get the stop to display in the map + private StopsMapViewModel stopsViewModel; + //private GTFSPositionsViewModel gtfsPosViewModel; //= new ViewModelProvider(this).get(MapViewModel.class); private MQTTPositionsViewModel positionsViewModel; @@ -282,7 +284,9 @@ //gtfsPosViewModel = new ViewModelProvider(this).get(GTFSPositionsViewModel.class); //viewModel - positionsViewModel = new ViewModelProvider(this).get(MQTTPositionsViewModel.class); + ViewModelProvider provider = new ViewModelProvider(this); + positionsViewModel = provider.get(MQTTPositionsViewModel.class); + stopsViewModel = provider.get(StopsMapViewModel.class); if (context instanceof FragmentListenerMain) { listenerMain = (FragmentListenerMain) context; } else { @@ -313,8 +317,6 @@ tripMarkersAnimators.clear(); positionsViewModel.stopPositionsListening(); - if (stopFetcher!= null) - stopFetcher.cancel(true); } /** @@ -553,6 +555,12 @@ } else { Log.e(DEBUG_TAG, "PositionsViewModel is null"); } + if(stopsViewModel !=null){ + + stopsViewModel.getStopsInBoundingBox().observe(getViewLifecycleOwner(), + this::showStopsMarkers + ); + } else Log.d(DEBUG_TAG, "Cannot observe new stops in map, stopsViewModel is null"); map.getOverlays().add(this.busPositionsOverlay); //set map as started hasMapStartFinished = true; @@ -565,7 +573,11 @@ private void requestStopsToShow(){ // get the top, bottom, left and right screen's coordinate BoundingBox bb = map.getBoundingBox(); - double latFrom = bb.getLatSouth(); + Log.d(DEBUG_TAG, "Requesting stops in bounding box, stopViewModel is null "+(stopsViewModel==null)); + if(stopsViewModel!=null){ + stopsViewModel.requestStopsInBoundingBox(bb); + } + /*double latFrom = bb.getLatSouth(); double latTo = bb.getLatNorth(); double lngFrom = bb.getLonWest(); double lngTo = bb.getLonEast(); @@ -574,6 +586,8 @@ stopFetcher = new AsyncStopFetcher(this); stopFetcher.execute( new AsyncStopFetcher.BoundingBoxLimit(lngFrom,lngTo,latFrom, latTo)); + + */ } private void updateBusMarker(final Marker marker, final LivePositionUpdate posUpdate, @Nullable boolean justCreated){ @@ -778,59 +792,4 @@ return null; } - /** - * Simple asyncTask class to load the stops in the background - * Holds a weak reference to the fragment to do callbacks - */ - static class AsyncStopFetcher extends AsyncTask>{ - - final WeakReference fragmentWeakReference; - - public AsyncStopFetcher(MapFragment fragment) { - this.fragmentWeakReference = new WeakReference<>(fragment); - } - - @Override - protected List doInBackground(BoundingBoxLimit... limits) { - if(fragmentWeakReference.get()==null || fragmentWeakReference.get().getContext() == null){ - Log.w(DEBUG_TAG, "AsyncLoad fragmentWeakreference null"); - - return null; - - } - final BoundingBoxLimit limit = limits[0]; - //Log.d(DEBUG_TAG, "Async Stop Fetcher started working"); - - NextGenDB dbHelper = NextGenDB.getInstance(fragmentWeakReference.get().getContext()); - ArrayList stops = dbHelper.queryAllInsideMapView(limit.latitFrom, limit.latitTo, - limit.longFrom, limit.latitTo); - dbHelper.close(); - return stops; - } - - @Override - protected void onPostExecute(List stops) { - super.onPostExecute(stops); - //Log.d(DEBUG_TAG, "Async Stop Fetcher has finished working"); - if(fragmentWeakReference.get()==null) { - Log.w(DEBUG_TAG, "AsyncLoad fragmentWeakreference null"); - return; - } - if (stops!=null) - Log.d(DEBUG_TAG, "AsyncLoad number of stops: "+stops.size()); - fragmentWeakReference.get().showStopsMarkers(stops); - } - - private static class BoundingBoxLimit{ - final double longFrom, longTo, latitFrom, latitTo; - - public BoundingBoxLimit(double longFrom, double longTo, double latitFrom, double latitTo) { - this.longFrom = longFrom; - this.longTo = longTo; - this.latitFrom = latitFrom; - this.latitTo = latitTo; - } - } - - } } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/NearbyStopsFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/NearbyStopsFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/NearbyStopsFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/NearbyStopsFragment.java @@ -67,13 +67,29 @@ public class NearbyStopsFragment extends Fragment implements LoaderManager.LoaderCallbacks { + public enum FragType{ + STOPS(1), ARRIVALS(2); + private final int num; + FragType(int num){ + this.num = num; + } + public static FragType fromNum(int i){ + switch (i){ + case 1: return STOPS; + case 2: return ARRIVALS; + default: + throw new IllegalArgumentException("type not recognized"); + } + } + } + private FragmentListenerMain mListener; private FragmentLocationListener fragmentLocationListener; private final static String DEBUG_TAG = "NearbyStopsFragment"; private final static String FRAGMENT_TYPE_KEY = "FragmentType"; - public final static int TYPE_STOPS = 19, TYPE_ARRIVALS = 20; - private int fragment_type; + //public final static int TYPE_STOPS = 19, TYPE_ARRIVALS = 20; + private FragType fragment_type = FragType.STOPS; public final static String FRAGMENT_TAG="NearbyStopsFrag"; @@ -118,12 +134,12 @@ * this fragment using the provided parameters. * @return A new instance of fragment NearbyStopsFragment. */ - public static NearbyStopsFragment newInstance(int fragmentType) { - if(fragmentType != TYPE_STOPS && fragmentType != TYPE_ARRIVALS ) - throw new IllegalArgumentException("WRONG KIND OF FRAGMENT USED"); + public static NearbyStopsFragment newInstance(FragType type) { + //if(fragmentType != TYPE_STOPS && fragmentType != TYPE_ARRIVALS ) + // throw new IllegalArgumentException("WRONG KIND OF FRAGMENT USED"); NearbyStopsFragment fragment = new NearbyStopsFragment(); final Bundle args = new Bundle(1); - args.putInt(FRAGMENT_TYPE_KEY,fragmentType); + args.putInt(FRAGMENT_TYPE_KEY,type.num); fragment.setArguments(args); return fragment; } @@ -134,7 +150,7 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { - setFragmentType(getArguments().getInt(FRAGMENT_TYPE_KEY)); + setFragmentType(FragType.fromNum(getArguments().getInt(FRAGMENT_TYPE_KEY))); } locManager = AppLocationManager.getInstance(getContext()); fragmentLocationListener = new FragmentLocationListener(this); @@ -189,16 +205,13 @@ * Use this method to set the fragment type * @param type the type, TYPE_ARRIVALS or TYPE_STOPS */ - private void setFragmentType(int type){ - if(type!=TYPE_ARRIVALS && type !=TYPE_STOPS) - throw new IllegalArgumentException("type not recognized"); + private void setFragmentType(FragType type){ this.fragment_type = type; switch(type){ - case TYPE_ARRIVALS: - + case ARRIVALS: TIME_INTERVAL_REQUESTS = 5*1000; break; - case TYPE_STOPS: + case STOPS: TIME_INTERVAL_REQUESTS = 1000; } @@ -238,13 +251,13 @@ //try another location provider } switch(fragment_type){ - case TYPE_STOPS: + case STOPS: if(dataAdapter!=null){ gridRecyclerView.setAdapter(dataAdapter); circlingProgressBar.setVisibility(View.GONE); } break; - case TYPE_ARRIVALS: + case ARRIVALS: if(arrivalsStopAdapter!=null){ gridRecyclerView.setAdapter(arrivalsStopAdapter); circlingProgressBar.setVisibility(View.GONE); @@ -357,10 +370,10 @@ //quick trial to hopefully always get the stops in the correct order Collections.sort(currentNearbyStops,new StopSorterByDistance(lastReceivedLocation)); switch (fragment_type){ - case TYPE_STOPS: + case STOPS: showStopsInRecycler(currentNearbyStops); break; - case TYPE_ARRIVALS: + case ARRIVALS: arrivalsManager = new ArrivalsManager(currentNearbyStops); flatProgressBar.setVisibility(View.VISIBLE); flatProgressBar.setProgress(0); @@ -388,8 +401,8 @@ * Call when you need to switch the type of fragment */ private void switchFragmentType(){ - if(fragment_type==TYPE_ARRIVALS){ - setFragmentType(TYPE_STOPS); + if(fragment_type==FragType.ARRIVALS){ + setFragmentType(FragType.STOPS); switchButton.setText(getString(R.string.show_arrivals)); titleTextView.setText(getString(R.string.nearby_stops_message)); if(arrivalsManager!=null) @@ -397,8 +410,8 @@ if(dataAdapter!=null) gridRecyclerView.setAdapter(dataAdapter); - } else if (fragment_type==TYPE_STOPS){ - setFragmentType(TYPE_ARRIVALS); + } else if (fragment_type==FragType.STOPS){ + setFragmentType(FragType.ARRIVALS); titleTextView.setText(getString(R.string.nearby_arrivals_message)); switchButton.setText(getString(R.string.show_stops)); if(arrivalsStopAdapter!=null) diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/StopListFragment.java b/app/src/main/java/it/reyboz/bustorino/fragments/StopListFragment.java --- a/app/src/main/java/it/reyboz/bustorino/fragments/StopListFragment.java +++ b/app/src/main/java/it/reyboz/bustorino/fragments/StopListFragment.java @@ -17,9 +17,11 @@ */ package it.reyboz.bustorino.fragments; +import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; +import androidx.annotation.NonNull; import androidx.loader.app.LoaderManager; import androidx.loader.content.CursorLoader; import androidx.loader.content.Loader; @@ -29,6 +31,7 @@ import it.reyboz.bustorino.data.AppDataProvider; import it.reyboz.bustorino.data.NextGenDB.Contract.StopsTable; import it.reyboz.bustorino.adapters.StopAdapter; +import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.List; @@ -68,6 +71,7 @@ public void onResume() { super.onResume(); LoaderManager loaderManager = getLoaderManager(); + mListener.readyGUIfor(FragmentKind.STOPS); if(stopList!=null) { mListAdapter = new StopAdapter(getContext(),stopList); resetListAdapter(mListAdapter); @@ -142,4 +146,5 @@ public void onLoaderReset(Loader loader) { loader.abandon(); } + } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/GTFSPositionsViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/GTFSPositionsViewModel.kt rename from app/src/main/java/it/reyboz/bustorino/fragments/GTFSPositionsViewModel.kt rename to app/src/main/java/it/reyboz/bustorino/viewmodels/GTFSPositionsViewModel.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/GTFSPositionsViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/GTFSPositionsViewModel.kt @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -package it.reyboz.bustorino.fragments +package it.reyboz.bustorino.viewmodels import android.app.Application import android.util.Log diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt rename from app/src/main/java/it/reyboz/bustorino/fragments/LinesViewModel.kt rename to app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt @@ -1,8 +1,9 @@ -package it.reyboz.bustorino.fragments +package it.reyboz.bustorino.viewmodels import android.app.Application import android.util.Log import androidx.lifecycle.* +import it.reyboz.bustorino.backend.Result import it.reyboz.bustorino.backend.Stop import it.reyboz.bustorino.data.GtfsRepository import it.reyboz.bustorino.data.NextGenDB diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/MQTTPositionsViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/MQTTPositionsViewModel.kt --- a/app/src/main/java/it/reyboz/bustorino/viewmodels/MQTTPositionsViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/MQTTPositionsViewModel.kt @@ -25,7 +25,6 @@ import it.reyboz.bustorino.data.GtfsRepository import it.reyboz.bustorino.data.MatoPatternsDownloadWorker import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops -import it.reyboz.bustorino.fragments.GTFSPositionsViewModel import kotlinx.coroutines.launch diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/StopsMapViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/StopsMapViewModel.kt new file mode 100644 --- /dev/null +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/StopsMapViewModel.kt @@ -0,0 +1,55 @@ +package it.reyboz.bustorino.viewmodels + +import android.app.Application +import android.util.Log +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.map +import it.reyboz.bustorino.backend.Result +import it.reyboz.bustorino.backend.Stop +import it.reyboz.bustorino.data.GtfsRepository +import it.reyboz.bustorino.data.NextGenDB +import it.reyboz.bustorino.data.OldDataRepository +import it.reyboz.bustorino.data.gtfs.GtfsDatabase +import org.osmdroid.util.BoundingBox +import java.util.ArrayList +import java.util.concurrent.Executors + +class StopsMapViewModel(application: Application): AndroidViewModel(application) { + + + private val executor = Executors.newFixedThreadPool(2) + private val oldRepo = OldDataRepository(executor, NextGenDB.getInstance(application)) + /* + private val boundingBoxLiveData = MutableLiveData() + + fun setStopBoundingBox(bb: BoundingBox){ + boundingBoxLiveData.value = bb + } + + */ + + val stopsInBoundingBox = MutableLiveData>() + + private val callback = + OldDataRepository.Callback> { result -> + result.let { + if(it.isSuccess){ + stopsInBoundingBox.postValue(it.result) + Log.d(DEBUG_TAG, "Setting value of stops in bounding box") + } + + } + } + + fun requestStopsInBoundingBox(bb: BoundingBox) { + bb.let { + Log.d(DEBUG_TAG, "Launching stop request") + oldRepo.requestStopsInArea(it.latSouth, it.latNorth, it.lonWest, it.lonEast, callback) + } + } + companion object{ + private const val DEBUG_TAG = "BusTOStopMapViewModel" + } +} \ No newline at end of file