From d569b0cbef6d20c3beee62d9145c127ec600ee3f Mon Sep 17 00:00:00 2001 From: ardevd Date: Thu, 19 Apr 2018 14:47:56 +0200 Subject: [PATCH] activities: Added repository data classes for Activities. --- .../activities/data/ActivitiesRepository.java | 19 +++ .../activities/data/ActivitiesServiceApi.java | 24 ++++ .../data/ActivitiesServiceApiImpl.java | 115 ++++++++++++++++++ .../activities/data/ActivityRepositories.java | 18 +++ .../data/RemoteActivitiesRepository.java | 32 +++++ 5 files changed, 208 insertions(+) create mode 100644 src/main/java/com/owncloud/android/ui/activities/data/ActivitiesRepository.java create mode 100644 src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApi.java create mode 100644 src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApiImpl.java create mode 100644 src/main/java/com/owncloud/android/ui/activities/data/ActivityRepositories.java create mode 100644 src/main/java/com/owncloud/android/ui/activities/data/RemoteActivitiesRepository.java diff --git a/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesRepository.java b/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesRepository.java new file mode 100644 index 0000000000..c9f2f3f3d3 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesRepository.java @@ -0,0 +1,19 @@ +package com.owncloud.android.ui.activities.data; + +import android.support.annotation.NonNull; + +import com.owncloud.android.lib.common.OwnCloudClient; + +import java.util.List; + +/** + * Main entry point for accessing activities data. + */ +public interface ActivitiesRepository { + interface LoadActivitiesCallback { + void onActivitiesLoaded(List activities, OwnCloudClient client, boolean clear); + void onActivitiesLoadedError(String error); + } + + void getActivities(String pageUrl, @NonNull LoadActivitiesCallback callback); +} \ No newline at end of file diff --git a/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApi.java b/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApi.java new file mode 100644 index 0000000000..4ac3b29017 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApi.java @@ -0,0 +1,24 @@ +package com.owncloud.android.ui.activities.data; + +import android.content.Context; + +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.resources.activities.models.Activity; + +import java.util.List; + +/** + * Defines an interface to the Activities service API. All ({@link Activity}) data requests should + * be piped through this interface. + */ + +public interface ActivitiesServiceApi { + + interface ActivitiesServiceCallback { + void onLoaded (T activities, OwnCloudClient client, boolean clear); + void onError (String error); + } + + void getAllActivities(String pageUrl, ActivitiesServiceApi.ActivitiesServiceCallback> callback); + +} diff --git a/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApiImpl.java b/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApiImpl.java new file mode 100644 index 0000000000..3c0f3b346f --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/activities/data/ActivitiesServiceApiImpl.java @@ -0,0 +1,115 @@ +package com.owncloud.android.ui.activities.data; + +import android.accounts.Account; +import android.accounts.AuthenticatorException; +import android.accounts.OperationCanceledException; +import android.content.Context; +import android.os.AsyncTask; +import android.view.View; + +import com.owncloud.android.MainApp; +import com.owncloud.android.R; +import com.owncloud.android.authentication.AccountUtils; +import com.owncloud.android.lib.common.OwnCloudAccount; +import com.owncloud.android.lib.common.OwnCloudClient; +import com.owncloud.android.lib.common.OwnCloudClientManagerFactory; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.lib.resources.activities.GetRemoteActivitiesOperation; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import butterknife.BindString; + +/** + * Implementation of the Activities Service API that communicates with the NextCloud remote server + */ +public class ActivitiesServiceApiImpl implements ActivitiesServiceApi { + + private static final String TAG = ActivitiesServiceApiImpl.class.getSimpleName(); + + @Override + public void getAllActivities(String pageUrl, ActivitiesServiceCallback> callback) { + GetActivityListTask getActivityListTask = new GetActivityListTask(pageUrl, callback); + getActivityListTask.execute(); + } + + private class GetActivityListTask extends AsyncTask { + + private final ActivitiesServiceCallback> mCallback; + private List mActivities; + private String mPageUrl; + String noResultsMessage = "no results"; + private String errorMessage; + private OwnCloudClient ownCloudClient; + + private GetActivityListTask(String pageUrl, ActivitiesServiceCallback> callback) { + mPageUrl = pageUrl; + mCallback = callback; + mActivities = new ArrayList<>(); + } + + + @Override + protected Boolean doInBackground(Void... voids) { + final Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(MainApp.getAppContext()); + final Context context = MainApp.getAppContext(); + OwnCloudAccount ocAccount; + try { + ocAccount = new OwnCloudAccount(currentAccount, context); + ownCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton(). + getClientFor(ocAccount, MainApp.getAppContext()); + ownCloudClient.setOwnCloudVersion(AccountUtils.getServerVersion(currentAccount)); + + GetRemoteActivitiesOperation getRemoteNotificationOperation = new GetRemoteActivitiesOperation(); + if (mPageUrl != null) { + getRemoteNotificationOperation.setNextUrl(mPageUrl); + } + + final RemoteOperationResult result = getRemoteNotificationOperation.execute(ownCloudClient); + + if (result.isSuccess() && result.getData() != null) { + final ArrayList data = result.getData(); + mActivities = (ArrayList) data.get(0); + + mPageUrl = (String) data.get(1); + return true; + } else { + Log_OC.d(TAG, result.getLogMessage()); + // show error + errorMessage = result.getLogMessage(); + if (result.getHttpCode() == 304) { + errorMessage = noResultsMessage; + } + return false; + + } + } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) { + Log_OC.e(TAG, "Account not found", e); + errorMessage = "Account not found"; + } catch (IOException e) { + Log_OC.e(TAG, "IO error", e); + errorMessage = "IO error"; + } catch (OperationCanceledException e) { + Log_OC.e(TAG, "Operation has been canceled", e); + errorMessage = "Operation has been canceled"; + } catch (AuthenticatorException e) { + Log_OC.e(TAG, "Authentication Exception", e); + errorMessage = "Authentication Exception"; + } + + return false; + } + + @Override + protected void onPostExecute(Boolean success) { + super.onPostExecute(success); + if (success) { + mCallback.onLoaded(new ArrayList<>(), ownCloudClient, mPageUrl == null); + } + mCallback.onError(errorMessage); + } + } +} diff --git a/src/main/java/com/owncloud/android/ui/activities/data/ActivityRepositories.java b/src/main/java/com/owncloud/android/ui/activities/data/ActivityRepositories.java new file mode 100644 index 0000000000..cc63bcc755 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/activities/data/ActivityRepositories.java @@ -0,0 +1,18 @@ +package com.owncloud.android.ui.activities.data; + +import android.support.annotation.NonNull; + +public class ActivityRepositories { + + private ActivityRepositories() { + // No instance + } + + private static ActivitiesRepository repository = null; + + public static synchronized ActivitiesRepository getRepository(@NonNull ActivitiesServiceApi activitiesServiceApi) { + return new RemoteActivitiesRepository(activitiesServiceApi); + } + +} + diff --git a/src/main/java/com/owncloud/android/ui/activities/data/RemoteActivitiesRepository.java b/src/main/java/com/owncloud/android/ui/activities/data/RemoteActivitiesRepository.java new file mode 100644 index 0000000000..fdc4bca7b4 --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/activities/data/RemoteActivitiesRepository.java @@ -0,0 +1,32 @@ +package com.owncloud.android.ui.activities.data; + +import android.support.annotation.NonNull; + +import com.owncloud.android.lib.common.OwnCloudClient; + +import java.util.List; + +public class RemoteActivitiesRepository implements ActivitiesRepository { + + private final ActivitiesServiceApi mActivitiesServiceApi; + + public RemoteActivitiesRepository(@NonNull ActivitiesServiceApi activitiesServiceApi) { + mActivitiesServiceApi = activitiesServiceApi; + } + + + @Override + public void getActivities(String pageUrl, @NonNull LoadActivitiesCallback callback) { + mActivitiesServiceApi.getAllActivities(pageUrl, new ActivitiesServiceApi.ActivitiesServiceCallback>() { + @Override + public void onLoaded(List activities, OwnCloudClient client, boolean clear) { + callback.onActivitiesLoaded(activities, client, clear); + } + + @Override + public void onError(String error) { + callback.onActivitiesLoadedError(error); + } + }); + } +}