Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Geo): Add Geo Rx Bindings #2159

Merged
merged 5 commits into from Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion rxbindings/build.gradle
Expand Up @@ -13,7 +13,11 @@
* permissions and limitations under the License.
*/

apply plugin: 'com.android.library'
plugins {
id "com.android.library"
id "kotlin-android"
}

apply from: rootProject.file("configuration/checkstyle.gradle")
apply from: rootProject.file("configuration/publishing.gradle")

Expand All @@ -26,6 +30,7 @@ dependencies {
testImplementation project(path: ':testutils')
testImplementation dependency.junit
testImplementation dependency.mockito
testImplementation dependency.mockk
testImplementation dependency.androidx.test.core
testImplementation dependency.robolectric
testImplementation project(path: ':rxbindings')
Expand Down
Expand Up @@ -41,6 +41,7 @@ private RxAmplify() {}
@SuppressWarnings({"checkstyle:all"}) public static final RxHubCategoryBehavior Hub = new RxHubBinding();
@SuppressWarnings({"checkstyle:all"}) public static final RxDataStoreCategoryBehavior DataStore = new RxDataStoreBinding();
@SuppressWarnings({"checkstyle:all"}) public static final RxPredictionsCategoryBehavior Predictions = new RxPredictionsBinding();
@SuppressWarnings({"checkstyle:all"}) public static final RxGeoCategoryBehavior Geo = new RxGeoBinding();

/**
* Read the configuration from amplifyconfiguration.json file.
Expand Down
@@ -0,0 +1,95 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.rx;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import com.amplifyframework.core.Amplify;
import com.amplifyframework.geo.GeoCategoryBehavior;
import com.amplifyframework.geo.GeoException;
import com.amplifyframework.geo.models.Coordinates;
import com.amplifyframework.geo.models.MapStyle;
import com.amplifyframework.geo.models.MapStyleDescriptor;
import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions;
import com.amplifyframework.geo.options.GeoSearchByTextOptions;
import com.amplifyframework.geo.options.GetMapStyleDescriptorOptions;
import com.amplifyframework.geo.result.GeoSearchResult;

import java.util.Collection;
import java.util.Objects;

import io.reactivex.rxjava3.core.Single;

final class RxGeoBinding implements RxGeoCategoryBehavior {
private final GeoCategoryBehavior delegate;

RxGeoBinding() {
delegate = Amplify.Geo;
}

@VisibleForTesting
RxGeoBinding(@NonNull GeoCategoryBehavior delegate) {
this.delegate = Objects.requireNonNull(delegate);
}

@Override
public Single<Collection<MapStyle>> getAvailableMaps() {
return toSingle(delegate::getAvailableMaps);
}

@Override
public Single<MapStyle> getDefaultMap() {
return toSingle(delegate::getDefaultMap);
}

@Override
public Single<MapStyleDescriptor> getMapStyleDescriptor() {
return toSingle(delegate::getMapStyleDescriptor);
}

@Override
public Single<MapStyleDescriptor> getMapStyleDescriptor(@NonNull GetMapStyleDescriptorOptions options) {
return toSingle((onResult, onError) -> delegate.getMapStyleDescriptor(options, onResult, onError));
}

@Override
public Single<GeoSearchResult> searchByText(@NonNull String query) {
return toSingle((onResult, onError) -> delegate.searchByText(query, onResult, onError));
}

@Override
public Single<GeoSearchResult> searchByText(@NonNull String query, @NonNull GeoSearchByTextOptions options) {
return toSingle((onResult, onError) -> delegate.searchByText(query, options, onResult, onError));
}

@Override
public Single<GeoSearchResult> searchByCoordinates(@NonNull Coordinates position) {
return toSingle((onResult, onError) -> delegate.searchByCoordinates(position, onResult, onError));
}

@Override
public Single<GeoSearchResult> searchByCoordinates(
@NonNull Coordinates position,
@NonNull GeoSearchByCoordinatesOptions options
) {
return toSingle((onResult, onError) -> delegate.searchByCoordinates(position, options, onResult, onError));
}

private static <T> Single<T> toSingle(RxAdapters.VoidBehaviors.ResultEmitter<T, GeoException> behavior) {
return RxAdapters.VoidBehaviors.toSingle(behavior);
}
}
@@ -0,0 +1,114 @@
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.rx;

import androidx.annotation.NonNull;

import com.amplifyframework.geo.GeoCategoryBehavior;
import com.amplifyframework.geo.GeoException;
import com.amplifyframework.geo.models.Coordinates;
import com.amplifyframework.geo.models.MapStyle;
import com.amplifyframework.geo.models.MapStyleDescriptor;
import com.amplifyframework.geo.options.GeoSearchByCoordinatesOptions;
import com.amplifyframework.geo.options.GeoSearchByTextOptions;
import com.amplifyframework.geo.options.GetMapStyleDescriptorOptions;
import com.amplifyframework.geo.result.GeoSearchResult;

import java.util.Collection;

import io.reactivex.rxjava3.core.Single;

/**
* An Rx-idiomatic expression of the behaviors in {@link GeoCategoryBehavior}.
*/
public interface RxGeoCategoryBehavior {
/**
* Gets a collection of maps and their corresponding styles.
*
* @return An Rx {@link Single} which emits a {@link MapStyle} on success, or a
* {@link GeoException} on failure
*/
Single<Collection<MapStyle>> getAvailableMaps();

/**
* Gets the default map and style from available maps.
*
* @return An Rx {@link Single} which emits a {@link MapStyle} on success, or a
* {@link GeoException} on failure
*/
Single<MapStyle> getDefaultMap();

/**
* Uses default options to get map style descriptor JSON.
*
* @return An Rx {@link Single} which emits a {@link MapStyleDescriptor} on success, or a
* {@link GeoException} on failure
*/
Single<MapStyleDescriptor> getMapStyleDescriptor();

/**
* Uses given options to get map style descriptor JSON.
*
* @param options Options to specify for this operation.
* @return An Rx {@link Single} which emits a {@link MapStyleDescriptor} on success, or a
* {@link GeoException} on failure
*/
Single<MapStyleDescriptor> getMapStyleDescriptor(@NonNull GetMapStyleDescriptorOptions options);

/**
* Searches for locations that match text query.
*
* @param query Search query text.
* @return An Rx {@link Single} which emits a {@link GeoSearchResult} on success, or a
* {@link GeoException} on failure
*/
Single<GeoSearchResult> searchByText(@NonNull String query);

/**
* Searches for locations that match text query.
*
* @param query Search query text.
* @param options Search options to use.
* @return An Rx {@link Single} which emits a {@link GeoSearchResult} on success, or a
* {@link GeoException} on failure
*/
Single<GeoSearchResult> searchByText(
@NonNull String query,
@NonNull GeoSearchByTextOptions options
);

/**
* Searches for location with given set of coordinates.
*
* @param position Coordinates to look-up.
* @return An Rx {@link Single} which emits a {@link GeoSearchResult} on success, or a
* {@link GeoException} on failure
*/
Single<GeoSearchResult> searchByCoordinates(@NonNull Coordinates position);

/**
* Searches for location with given set of coordinates.
*
* @param position Coordinates to look-up.
* @param options Search options to use.
* @return An Rx {@link Single} which emits a {@link GeoSearchResult} on success, or a
* {@link GeoException} on failure
*/
Single<GeoSearchResult> searchByCoordinates(
@NonNull Coordinates position,
@NonNull GeoSearchByCoordinatesOptions options
);
}