Search in sources :

Example 1 with HubChannel

use of com.amplifyframework.hub.HubChannel in project amplify-android by aws-amplify.

the class HubAccumulator method create.

/**
 * Creates a {@link HubAccumulator} that accumulates events arriving on a particular channel,
 * whose event name is the given enum value (as string). For example, the accumulator
 * created as below will match all events with name
 * {@link DataStoreChannelEventName#OUTBOX_MUTATION_PROCESSED}:
 *
 *   HubAccumulator.create(HubChannel.DATASTORE, DataStoreChannelEventName.PUBLISH_TO_CLOUD);
 *
 * @param channel Channel on which to listen for events
 * @param enumeratedEventName A enum value, the toString() of which is expected as the name of
 *                             a hub event. Only events with this name will be accumulated.
 * @param quantity Number of events to accumulate
 * @param <E> The type of enumeration
 * @return A HubAccumulator
 */
@NonNull
public static <E extends Enum<E>> HubAccumulator create(@NonNull HubChannel channel, @NonNull E enumeratedEventName, int quantity) {
    Objects.requireNonNull(channel);
    Objects.requireNonNull(enumeratedEventName);
    HubEventFilter filter = event -> enumeratedEventName.toString().equals(event.getName());
    return new HubAccumulator(channel, filter, quantity);
}
Also used : Amplify(com.amplifyframework.core.Amplify) HubChannel(com.amplifyframework.hub.HubChannel) Immutable(com.amplifyframework.util.Immutable) NonNull(androidx.annotation.NonNull) HubEventFilters(com.amplifyframework.hub.HubEventFilters) DataStoreChannelEventName(com.amplifyframework.datastore.DataStoreChannelEventName) AtomicReference(java.util.concurrent.atomic.AtomicReference) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) HubEventFilter(com.amplifyframework.hub.HubEventFilter) SubscriptionToken(com.amplifyframework.hub.SubscriptionToken) HubEvent(com.amplifyframework.hub.HubEvent) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) HubEventFilter(com.amplifyframework.hub.HubEventFilter) NonNull(androidx.annotation.NonNull)

Example 2 with HubChannel

use of com.amplifyframework.hub.HubChannel in project amplify-android by aws-amplify.

the class Category method initialize.

/**
 * Initialize the category. This asynchronous call is made only after
 * the category has been successfully configured. Whereas configuration is a short-lived
 * synchronous phase of setup, initialization may require disk/network resources, etc.
 * @param context An Android Context
 * @return A category initialization result
 */
@NonNull
@WorkerThread
public final synchronized CategoryInitializationResult initialize(@NonNull Context context) {
    final Map<String, InitializationResult> pluginInitializationResults = new HashMap<>();
    if (!State.CONFIGURED.equals(state.get())) {
        for (P plugin : getPlugins()) {
            InitializationResult result = InitializationResult.failure(new AmplifyException("Tried to init before category was not configured.", "Call configure() on category, first."));
            pluginInitializationResults.put(plugin.getPluginKey(), result);
        }
    } else {
        state.set(State.CONFIGURING);
        for (P plugin : getPlugins()) {
            InitializationResult result;
            try {
                plugin.initialize(context);
                result = InitializationResult.success();
            } catch (AmplifyException pluginInitializationFailure) {
                result = InitializationResult.failure(pluginInitializationFailure);
            }
            pluginInitializationResults.put(plugin.getPluginKey(), result);
        }
    }
    final CategoryInitializationResult result = CategoryInitializationResult.with(pluginInitializationResults);
    categoryInitializationResult.set(result);
    if (result.isFailure()) {
        state.set(State.INITIALIZATION_FAILED);
    } else {
        state.set(State.INITIALIZED);
    }
    HubChannel hubChannel = HubChannel.forCategoryType(getCategoryType());
    InitializationStatus status = result.isFailure() ? InitializationStatus.FAILED : InitializationStatus.SUCCEEDED;
    Amplify.Hub.publish(hubChannel, HubEvent.create(status, result));
    return result;
}
Also used : InitializationResult(com.amplifyframework.core.InitializationResult) AmplifyException(com.amplifyframework.AmplifyException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HubChannel(com.amplifyframework.hub.HubChannel) InitializationStatus(com.amplifyframework.core.InitializationStatus) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 3 with HubChannel

use of com.amplifyframework.hub.HubChannel in project amplify-android by aws-amplify.

the class AWSDataStorePlugin method configure.

/**
 * {@inheritDoc}
 */
@Override
public void configure(@NonNull JSONObject pluginConfiguration, @NonNull Context context) throws DataStoreException {
    try {
        // Applies user-provided configs on-top-of any values from the file.
        this.pluginConfiguration = DataStoreConfiguration.builder(pluginConfiguration, userProvidedConfiguration).build();
    } catch (DataStoreException badConfigException) {
        throw new DataStoreException("There was an issue configuring the plugin from the amplifyconfiguration.json", badConfigException, "Check the attached exception for more details and " + "be sure you are only calling Amplify.configure once");
    }
    HubChannel hubChannel = HubChannel.forCategoryType(getCategoryType());
    Amplify.Hub.subscribe(hubChannel, event -> InitializationStatus.SUCCEEDED.toString().equals(event.getName()), event -> categoryInitializationsPending.countDown());
}
Also used : HubChannel(com.amplifyframework.hub.HubChannel)

Aggregations

HubChannel (com.amplifyframework.hub.HubChannel)3 NonNull (androidx.annotation.NonNull)2 WorkerThread (androidx.annotation.WorkerThread)1 AmplifyException (com.amplifyframework.AmplifyException)1 Amplify (com.amplifyframework.core.Amplify)1 InitializationResult (com.amplifyframework.core.InitializationResult)1 InitializationStatus (com.amplifyframework.core.InitializationStatus)1 DataStoreChannelEventName (com.amplifyframework.datastore.DataStoreChannelEventName)1 HubEvent (com.amplifyframework.hub.HubEvent)1 HubEventFilter (com.amplifyframework.hub.HubEventFilter)1 HubEventFilters (com.amplifyframework.hub.HubEventFilters)1 SubscriptionToken (com.amplifyframework.hub.SubscriptionToken)1 Immutable (com.amplifyframework.util.Immutable)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Objects (java.util.Objects)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1