Skip to content

Commit

Permalink
Allow customers to create any number of instances of the search engin…
Browse files Browse the repository at this point in the history
…e. (#34)
  • Loading branch information
DzmitryFomchyn authored Apr 14, 2022
1 parent d535f25 commit 939dced
Show file tree
Hide file tree
Showing 72 changed files with 2,396 additions and 1,856 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@

## 1.0.0-beta.29-SNAPSHOT

### Breaking changes
- [CORE] `ServiceProvider.globalDataProvidersRegistry()` has been removed. Now customers should use `SearchEngine.registerDataProvider()` for the data providers registration. Interface `IndexableDataProvidersRegistry` and its internal classes are not available anymore either.
- [CORE] Now `IndexableDataProvider`s should provide `priority` field which affects `IndexableRecord`'s ranking in case of multiple data providers added to a search engine.
- [CORE] `IndexableDataProviderEngineLayer` has been renamed to `IndexableDataProviderEngine`. Also, functions `registerIndexableDataProviderEngineLayer()` and `unregisterIndexableDataProviderEngineLayer()` of `IndexableDataProvider` have been renamed to `registerIndexableDataProviderEngine()` and `unregisterIndexableDataProviderEngine()`.
- [CORE] Functionality of `CategorySearchEngine`, and `ReverseGeocodingSearchEngine` have been merged into `SearchEngine`. Also, functions `MapboxSearchSdk.getCategorySearchEngine()`, `MapboxSearchSdk.getReverseGeocodingSearchEngine()` have been removed, call `MapboxSearchSdk.getSearchEngine()` or `MapboxSearchSdk.createSearchEngine()` instead.
- [CORE] Constants `MapboxSearchSdk.LAYER_PRIORITY_HISTORY`, `MapboxSearchSdk.LAYER_PRIORITY_FAVORITES` have been moved `HistoryDataProvider.PROVIDER_PRIORITY`, and `FavoritesDataProvider.PROVIDER_PRIORITY`.
- [CORE] `MapboxSearchSdk.addDataProviderInitializationCallback()` and `MapboxSearchSdk.removeDataProviderInitializationCallback()` have been removed along with `DataProviderInitializationCallback` interface. Use `HistoryDataProvider.OnDataProviderEngineRegisterListener()`, and `FavoritesDataProvider.OnDataProviderEngineRegisterListener()` instead.
- [CORE] Fields `SearchSdkSettings.geocodingEndpointBaseUrl`, and `SearchSdkSettings.singleBoxSearchBaseUrl` have been removed. Now you can configure search engine endpoints with `SearchEngineSettings`. `SearchSdkSettings.Builder` and corresponding functions have also been removed. `MapboxSearchSdk.initialize()` now can accept `SearchEngineSettings`.
- [CORE] Class `OfflineSearchSettings` has been renamed to `OfflineSearchEngineSettings`. `MapboxSearchSdk.initialize()` argument `offlineSearchSettings` has also been renamed to `offlineSearchEngineSettings`.

### New features
- [CORE] Now customers can create several independent from each other `SearchEngine`s. See `MapboxSearchSdk.createSearchEngine()`.

### Mapbox dependencies
- Search Native SDK `0.51.0`
- Common SDK `21.2.0`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.Application
import android.os.Build
import android.os.LocaleList
import com.mapbox.search.MapboxSearchSdk
import com.mapbox.search.SearchSdkSettings
import com.mapbox.search.SearchEngineSettings
import com.mapbox.search.common.FixedPointLocationEngine
import com.mapbox.search.sample.BuildConfig
import com.mapbox.search.sample.Constants.TEST_USER_LOCATION
Expand All @@ -28,7 +28,7 @@ class TestApp : Application() {
application = this,
accessToken = BuildConfig.MAPBOX_API_TOKEN,
locationEngine = FixedPointLocationEngine(TEST_USER_LOCATION),
searchSdkSettings = SearchSdkSettings(
searchEngineSettings = SearchEngineSettings(
singleBoxSearchBaseUrl = "http://localhost:${MockWebServerRule.DEFAULT_PORT}/"
),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import android.os.StrictMode.VmPolicy
import com.mapbox.android.core.location.LocationEngineProvider
import com.mapbox.common.TileStore
import com.mapbox.search.MapboxSearchSdk
import com.mapbox.search.OfflineSearchSettings
import com.mapbox.search.OfflineSearchEngineSettings
import com.mapbox.search.SearchSdkSettings

open class SampleApplication : Application() {
Expand All @@ -26,7 +26,7 @@ open class SampleApplication : Application() {
accessToken = BuildConfig.MAPBOX_API_TOKEN,
locationEngine = LocationEngineProvider.getBestLocationEngine(this),
searchSdkSettings = SearchSdkSettings(maxHistoryRecordsAmount = 5),
offlineSearchSettings = OfflineSearchSettings(tileStore = TileStore.create()),
offlineSearchEngineSettings = OfflineSearchEngineSettings(tileStore = TileStore.create()),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mapbox.search.CategorySearchEngine;
import com.mapbox.search.CategorySearchOptions;
import com.mapbox.search.MapboxSearchSdk;
import com.mapbox.search.ResponseInfo;
import com.mapbox.search.SearchCallback;
import com.mapbox.search.SearchEngine;
import com.mapbox.search.SearchRequestTask;
import com.mapbox.search.result.SearchResult;

import java.util.List;

public class CategorySearchJavaExampleActivity extends AppCompatActivity {

private CategorySearchEngine categorySearchEngine;
private SearchEngine searchEngine;
private SearchRequestTask searchRequestTask;

private final SearchCallback searchCallback = new SearchCallback() {
Expand All @@ -43,13 +43,13 @@ public void onError(@NonNull Exception e) {
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

categorySearchEngine = MapboxSearchSdk.getCategorySearchEngine();
searchEngine = MapboxSearchSdk.getSearchEngine();

final CategorySearchOptions options = new CategorySearchOptions.Builder()
.limit(1)
.build();

searchRequestTask = categorySearchEngine.search("cafe", options, searchCallback);
searchRequestTask = searchEngine.search("cafe", options, searchCallback);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package com.mapbox.search.sample.api
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.search.CategorySearchEngine
import com.mapbox.search.CategorySearchOptions
import com.mapbox.search.MapboxSearchSdk
import com.mapbox.search.ResponseInfo
import com.mapbox.search.SearchCallback
import com.mapbox.search.SearchEngine
import com.mapbox.search.SearchRequestTask
import com.mapbox.search.result.SearchResult

class CategorySearchKotlinExampleActivity : AppCompatActivity() {

private lateinit var categorySearchEngine: CategorySearchEngine
private lateinit var searchEngine: SearchEngine
private lateinit var searchRequestTask: SearchRequestTask

private val searchCallback: SearchCallback = object : SearchCallback {
Expand All @@ -34,9 +34,8 @@ class CategorySearchKotlinExampleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

categorySearchEngine = MapboxSearchSdk.getCategorySearchEngine()

searchRequestTask = categorySearchEngine.search(
searchEngine = MapboxSearchSdk.getSearchEngine()
searchRequestTask = searchEngine.search(
"cafe",
CategorySearchOptions(limit = 1),
searchCallback
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.mapbox.geojson.Point;
import com.mapbox.search.AsyncOperationTask;
import com.mapbox.search.CompletionCallback;
import com.mapbox.search.IndexableDataProvidersRegistry;
import com.mapbox.search.MapboxSearchSdk;
import com.mapbox.search.ResponseInfo;
import com.mapbox.search.SearchEngine;
Expand All @@ -19,7 +18,7 @@
import com.mapbox.search.SearchSelectionCallback;
import com.mapbox.search.record.FavoriteRecord;
import com.mapbox.search.record.IndexableDataProvider;
import com.mapbox.search.record.IndexableDataProviderEngineLayer;
import com.mapbox.search.record.IndexableDataProviderEngine;
import com.mapbox.search.record.IndexableRecord;
import com.mapbox.search.result.SearchResult;
import com.mapbox.search.result.SearchResultType;
Expand Down Expand Up @@ -86,12 +85,12 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
searchEngine = MapboxSearchSdk.getSearchEngine();

Log.i("SearchApiExample", "Start CustomDataProvider registering...");
registerProviderTask = MapboxSearchSdk.getServiceProvider().globalDataProvidersRegistry().register(

registerProviderTask = searchEngine.registerDataProvider(
customDataProvider,
200,
new IndexableDataProvidersRegistry.Callback() {
new CompletionCallback<Unit>() {
@Override
public void onSuccess() {
public void onComplete(Unit result) {
Log.i("SearchApiExample", "CustomDataProvider is registered");
searchRequestTask = searchEngine.search(
"Underdog",
Expand All @@ -117,11 +116,12 @@ protected void onDestroy() {
if (searchRequestTask != null) {
searchRequestTask.cancel();
}
MapboxSearchSdk.getServiceProvider().globalDataProvidersRegistry().unregister(

searchEngine.unregisterDataProvider(
customDataProvider,
new IndexableDataProvidersRegistry.Callback() {
new CompletionCallback<Unit>() {
@Override
public void onSuccess() {
public void onComplete(Unit result) {
Log.i("SearchApiExample", "CustomDataProvider is unregistered");
}

Expand All @@ -131,7 +131,6 @@ public void onError(@NonNull Exception e) {
}
}
);

super.onDestroy();
}

Expand All @@ -152,7 +151,7 @@ private IndexableRecord createRecord(String name, Point coordinate) {

private static class InMemoryDataProvider<R extends IndexableRecord> implements IndexableDataProvider<R> {

private final List<IndexableDataProviderEngineLayer> dataProviderEngineLayers = new ArrayList<>();
private final List<IndexableDataProviderEngine> dataProviderEngines = new ArrayList<>();
private final Map<String, R> records = new LinkedHashMap<>();

private final Executor mainThreadExecutor = SearchSdkMainThreadWorker.INSTANCE.getMainExecutor();
Expand All @@ -169,36 +168,41 @@ public String getDataProviderName() {
return "SAMPLE_APP_CUSTOM_DATA_PROVIDER";
}

@Override
public int getPriority() {
return 200;
}

@NonNull
@Override
public AsyncOperationTask registerIndexableDataProviderEngineLayer(
@NonNull IndexableDataProviderEngineLayer dataProviderEngine,
public AsyncOperationTask registerIndexableDataProviderEngine(
@NonNull IndexableDataProviderEngine dataProviderEngine,
@NonNull Executor executor,
@NonNull CompletionCallback<Unit> callback
) {
dataProviderEngine.addAll(records.values());
dataProviderEngineLayers.add(dataProviderEngine);
dataProviderEngines.add(dataProviderEngine);
executor.execute(() -> callback.onComplete(Unit.INSTANCE));
return CompletedAsyncOperationTask.getInstance();
}

@NonNull
@Override
public AsyncOperationTask registerIndexableDataProviderEngineLayer(
@NonNull IndexableDataProviderEngineLayer dataProviderEngine,
public AsyncOperationTask registerIndexableDataProviderEngine(
@NonNull IndexableDataProviderEngine dataProviderEngine,
@NonNull CompletionCallback<Unit> callback
) {
return registerIndexableDataProviderEngineLayer(dataProviderEngine, mainThreadExecutor, callback);
return registerIndexableDataProviderEngine(dataProviderEngine, mainThreadExecutor, callback);
}

@NonNull
@Override
public AsyncOperationTask unregisterIndexableDataProviderEngineLayer(
@NonNull IndexableDataProviderEngineLayer dataProviderEngine,
public AsyncOperationTask unregisterIndexableDataProviderEngine(
@NonNull IndexableDataProviderEngine dataProviderEngine,
@NonNull Executor executor,
@NonNull CompletionCallback<Boolean> callback
) {
boolean isRemoved = dataProviderEngineLayers.remove(dataProviderEngine);
boolean isRemoved = dataProviderEngines.remove(dataProviderEngine);
if (isRemoved) {
dataProviderEngine.clear();
}
Expand All @@ -208,11 +212,11 @@ public AsyncOperationTask unregisterIndexableDataProviderEngineLayer(

@NonNull
@Override
public AsyncOperationTask unregisterIndexableDataProviderEngineLayer(
@NonNull IndexableDataProviderEngineLayer dataProviderEngine,
public AsyncOperationTask unregisterIndexableDataProviderEngine(
@NonNull IndexableDataProviderEngine dataProviderEngine,
@NonNull CompletionCallback<Boolean> callback
) {
return unregisterIndexableDataProviderEngineLayer(dataProviderEngine, mainThreadExecutor, callback);
return unregisterIndexableDataProviderEngine(dataProviderEngine, mainThreadExecutor, callback);
}

@NonNull
Expand Down Expand Up @@ -269,8 +273,8 @@ public AsyncOperationTask add(
@NonNull Executor executor,
@NonNull CompletionCallback<Unit> callback
) {
for (IndexableDataProviderEngineLayer layer : dataProviderEngineLayers) {
layer.add(record);
for (IndexableDataProviderEngine engine : dataProviderEngines) {
engine.add(record);
}
records.put(record.getId(), record);
executor.execute(() -> callback.onComplete(Unit.INSTANCE));
Expand All @@ -290,8 +294,8 @@ public AsyncOperationTask addAll(
@NonNull Executor executor,
@NonNull CompletionCallback<Unit> callback
) {
for (IndexableDataProviderEngineLayer layer : dataProviderEngineLayers) {
layer.addAll(records);
for (IndexableDataProviderEngine engine : dataProviderEngines) {
engine.addAll(records);
}
for (R record : records) {
this.records.put(record.getId(), record);
Expand All @@ -313,8 +317,8 @@ public AsyncOperationTask update(
@NonNull Executor executor,
@NonNull CompletionCallback<Unit> callback
) {
for (IndexableDataProviderEngineLayer layer : dataProviderEngineLayers) {
layer.update(record);
for (IndexableDataProviderEngine engine : dataProviderEngines) {
engine.update(record);
}
records.put(record.getId(), record);
executor.execute(() -> callback.onComplete(Unit.INSTANCE));
Expand All @@ -334,8 +338,8 @@ public AsyncOperationTask remove(
@NonNull Executor executor,
@NonNull CompletionCallback<Boolean> callback
) {
for (IndexableDataProviderEngineLayer layer : dataProviderEngineLayers) {
layer.remove(id);
for (IndexableDataProviderEngine engine : dataProviderEngines) {
engine.remove(id);
}
boolean isRemoved = records.remove(id) != null;
executor.execute(() -> callback.onComplete(isRemoved));
Expand All @@ -351,8 +355,8 @@ public AsyncOperationTask remove(@NonNull String id, @NonNull CompletionCallback
@NonNull
@Override
public AsyncOperationTask clear(@NonNull Executor executor, @NonNull CompletionCallback<Unit> callback) {
for (IndexableDataProviderEngineLayer layer : dataProviderEngineLayers) {
layer.clear();
for (IndexableDataProviderEngine engine : dataProviderEngines) {
engine.clear();
}
records.clear();
executor.execute(() -> callback.onComplete(Unit.INSTANCE));
Expand Down
Loading

0 comments on commit 939dced

Please sign in to comment.