Search in sources :

Example 1 with InitializationStatus

use of com.amplifyframework.core.InitializationStatus 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 2 with InitializationStatus

use of com.amplifyframework.core.InitializationStatus in project amplify-android by aws-amplify.

the class CategoryInitializationResult method with.

/**
 * Creates a CategoryInitializationResult using the results from initializing a collection
 * of named plugins.
 * @param pluginResults The results of initialization for a collection of plugins
 * @return A category initialization result
 */
@NonNull
public static CategoryInitializationResult with(@NonNull Map<String, InitializationResult> pluginResults) {
    Objects.requireNonNull(pluginResults);
    InitializationStatus categoryStatus;
    if (anyFailed(pluginResults)) {
        categoryStatus = InitializationStatus.FAILED;
    } else {
        categoryStatus = InitializationStatus.SUCCEEDED;
    }
    return new CategoryInitializationResult(categoryStatus, pluginResults);
}
Also used : InitializationStatus(com.amplifyframework.core.InitializationStatus) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)2 InitializationStatus (com.amplifyframework.core.InitializationStatus)2 WorkerThread (androidx.annotation.WorkerThread)1 AmplifyException (com.amplifyframework.AmplifyException)1 InitializationResult (com.amplifyframework.core.InitializationResult)1 HubChannel (com.amplifyframework.hub.HubChannel)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1