Skip to content

Commit

Permalink
also extract execute action task
Browse files Browse the repository at this point in the history
ignore compound warning
renamed RemoteOperation

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
  • Loading branch information
tobiasKaminsky committed Feb 12, 2019
1 parent 8f1955b commit fc5db89
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 89 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ dependencies {
// dependencies for app building
implementation 'androidx.multidex:multidex:2.0.1'
// implementation project('nextcloud-android-library')
genericImplementation 'com.github.nextcloud:android-library:master-SNAPSHOT'
gplayImplementation 'com.github.nextcloud:android-library:master-SNAPSHOT'
versionDevImplementation 'com.github.nextcloud:android-library:master-SNAPSHOT'
genericImplementation 'com.github.nextcloud:android-library:deleteNotification-SNAPSHOT'
gplayImplementation 'com.github.nextcloud:android-library:deleteNotification-SNAPSHOT'
versionDevImplementation 'com.github.nextcloud:android-library:deleteNotification-SNAPSHOT'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.content.Intent;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
Expand All @@ -47,9 +48,11 @@
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.notifications.GetRemoteNotificationsOperation;
import com.owncloud.android.lib.resources.notifications.GetNotificationsRemoteOperation;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.ui.adapter.NotificationListAdapter;
import com.owncloud.android.ui.asynctasks.DeleteAllNotificationsTask;
import com.owncloud.android.ui.notifications.NotificationsContract;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.PushUtils;
import com.owncloud.android.utils.ThemeUtils;
Expand All @@ -68,7 +71,7 @@
/**
* Activity displaying all server side stored notification items.
*/
public class NotificationsActivity extends FileActivity {
public class NotificationsActivity extends FileActivity implements NotificationsContract.View {

private static final String TAG = NotificationsActivity.class.getSimpleName();

Expand Down Expand Up @@ -269,7 +272,7 @@ private void fetchAndSetData() {
recyclerView.setAdapter(adapter);
}

RemoteOperation getRemoteNotificationOperation = new GetRemoteNotificationsOperation();
RemoteOperation getRemoteNotificationOperation = new GetNotificationsRemoteOperation();
final RemoteOperationResult result = getRemoteNotificationOperation.execute(client);

if (result.isSuccess() && result.getNotificationData() != null) {
Expand Down Expand Up @@ -305,6 +308,11 @@ private void hideRefreshLayoutLoader() {
});
}

public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.notifications_actions_menu, menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
Expand All @@ -319,6 +327,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
break;

case R.id.action_empty_notifications:
new DeleteAllNotificationsTask(client, this).execute();
break;

default:
retval = super.onOptionsItemSelected(item);
break;
Expand Down Expand Up @@ -353,4 +365,41 @@ protected void onResume() {

setDrawerMenuItemChecked(R.id.nav_notifications);
}

@Override
public void onRemovedNotification(boolean isSuccess, NotificationListAdapter.NotificationViewHolder holder) {
if (isSuccess) {
adapter.removeNotification(holder);

if (adapter.getItemCount() == 0) {
setEmptyContent(noResultsHeadline, noResultsMessage);
swipeListRefreshLayout.setVisibility(View.GONE);
swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
}
} else {
DisplayUtils.showSnackMessage(this, getString(R.string.remove_notification_failed));
}
}

@Override
public void onRemovedAllNotifications(boolean isSuccess) {
if (isSuccess) {
adapter.removeAllNotifications();
setEmptyContent(noResultsHeadline, noResultsMessage);
swipeListRefreshLayout.setVisibility(View.GONE);
swipeEmptyListRefreshLayout.setVisibility(View.VISIBLE);
} else {
DisplayUtils.showSnackMessage(this, getString(R.string.clear_notifications_failed));
}
}

@Override
public void onActionCallback(boolean isSuccess, NotificationListAdapter.NotificationViewHolder holder) {
if (isSuccess) {
adapter.removeNotification(holder);
} else {
adapter.setButtonEnabled(holder, true);
DisplayUtils.showSnackMessage(this, getString(R.string.notification_action_failed));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import android.graphics.Typeface;
import android.graphics.drawable.PictureDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
Expand All @@ -50,25 +49,18 @@
import com.google.android.material.button.MaterialButton;
import com.owncloud.android.R;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.notifications.models.Action;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.lib.resources.notifications.models.RichObject;
import com.owncloud.android.ui.activity.NotificationsActivity;
import com.owncloud.android.ui.asynctasks.DeleteNotificationTask;
import com.owncloud.android.ui.asynctasks.NotificationExecuteActionTask;
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.ThemeUtils;
import com.owncloud.android.utils.svg.SvgDecoder;
import com.owncloud.android.utils.svg.SvgDrawableTranscoder;
import com.owncloud.android.utils.svg.SvgSoftwareLayerSetter;

import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -176,11 +168,14 @@ public void onBindViewHolder(@NonNull NotificationViewHolder holder, int positio
button.setOnClickListener(v -> {
setButtonEnabled(holder, false);

new ExecuteActionTask(holder).execute(action);
new NotificationExecuteActionTask(client, holder, notificationsActivity).execute(action);
});

holder.buttons.addView(button);
}

holder.dismiss.setOnClickListener(v -> new DeleteNotificationTask(client, notification, holder,
notificationsActivity).execute());
}

private SpannableStringBuilder makeSpecialPartsBold(Notification notification) {
Expand Down Expand Up @@ -211,67 +206,23 @@ private SpannableStringBuilder makeSpecialPartsBold(Notification notification) {
return ssb;
}

private class ExecuteActionTask extends AsyncTask<Action, Void, Boolean> {

private NotificationViewHolder holder;

ExecuteActionTask(NotificationViewHolder holder) {
this.holder = holder;
}

@Override
protected Boolean doInBackground(Action... actions) {
HttpMethod method;
Action action = actions[0];

switch (action.type) {
case "GET":
method = new GetMethod(action.link);
break;

case "POST":
method = new PostMethod(action.link);
break;

case "DELETE":
method = new DeleteMethod(action.link);
break;

default:
// do nothing
return false;
}

method.setRequestHeader(RemoteOperation.OCS_API_HEADER, RemoteOperation.OCS_API_HEADER_VALUE);

int status;
try {
status = client.executeMethod(method);
} catch (IOException e) {
Log_OC.e(TAG, "Execution of notification action failed: " + e);
return false;
}
public void removeNotification(NotificationViewHolder holder) {
int position = holder.getAdapterPosition();

return status == HttpStatus.SC_OK || status == HttpStatus.SC_ACCEPTED;
if (position >= 0 && position < notificationsList.size()) {
notificationsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, notificationsList.size());
}
}

@Override
protected void onPostExecute(Boolean success) {
if (success) {
int position = holder.getAdapterPosition();

if (position >= 0 && position < notificationsList.size()) {
notificationsList.remove(position);
notifyItemRemoved(position);
}
} else {
setButtonEnabled(holder, true);
DisplayUtils.showSnackMessage(notificationsActivity, "Failed to execute action!");
}
}
public void removeAllNotifications() {
notificationsList.clear();
notifyDataSetChanged();
}

private void setButtonEnabled(NotificationViewHolder holder, boolean enabled) {

public void setButtonEnabled(NotificationViewHolder holder, boolean enabled) {
for (int i = 0; i < holder.buttons.getChildCount(); i++) {
holder.buttons.getChildAt(i).setEnabled(enabled);
}
Expand Down Expand Up @@ -310,7 +261,7 @@ public int getItemCount() {
return notificationsList.size();
}

static class NotificationViewHolder extends RecyclerView.ViewHolder {
public static class NotificationViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.notification_icon)
public ImageView icon;
@BindView(R.id.notification_subject)
Expand All @@ -321,6 +272,8 @@ static class NotificationViewHolder extends RecyclerView.ViewHolder {
public TextView dateTime;
@BindView(R.id.notification_buttons)
public LinearLayout buttons;
@BindView(R.id.notification_dismiss)
public ImageView dismiss;

private NotificationViewHolder(View itemView) {
super(itemView);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android.ui.asynctasks;

import android.os.AsyncTask;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.notifications.DeleteAllNotificationsRemoteOperation;
import com.owncloud.android.lib.resources.notifications.models.Action;
import com.owncloud.android.ui.activity.NotificationsActivity;
import com.owncloud.android.ui.notifications.NotificationsContract;

public class DeleteAllNotificationsTask extends AsyncTask<Action, Void, Boolean> {
private OwnCloudClient client;
private NotificationsContract.View notificationsActivity;

public DeleteAllNotificationsTask(OwnCloudClient client, NotificationsActivity notificationsActivity) {
this.client = client;
this.notificationsActivity = notificationsActivity;
}

@Override
protected Boolean doInBackground(Action... actions) {

RemoteOperationResult result = new DeleteAllNotificationsRemoteOperation().execute(client);

return result.isSuccess();
}

@Override
protected void onPostExecute(Boolean success) {
notificationsActivity.onRemovedAllNotifications(success);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Nextcloud Android client application
*
* @author Tobias Kaminsky
* Copyright (C) 2019 Tobias Kaminsky
* Copyright (C) 2019 Nextcloud GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package com.owncloud.android.ui.asynctasks;

import android.os.AsyncTask;

import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.notifications.DeleteNotificationRemoteOperation;
import com.owncloud.android.lib.resources.notifications.models.Action;
import com.owncloud.android.lib.resources.notifications.models.Notification;
import com.owncloud.android.ui.activity.NotificationsActivity;
import com.owncloud.android.ui.adapter.NotificationListAdapter;
import com.owncloud.android.ui.notifications.NotificationsContract;

public class DeleteNotificationTask extends AsyncTask<Action, Void, Boolean> {
private Notification notification;
private NotificationListAdapter.NotificationViewHolder holder;
private OwnCloudClient client;
private NotificationsContract.View notificationsActivity;

public DeleteNotificationTask(OwnCloudClient client, Notification notification,
NotificationListAdapter.NotificationViewHolder holder,
NotificationsActivity notificationsActivity) {
this.client = client;
this.notification = notification;
this.holder = holder;
this.notificationsActivity = notificationsActivity;
}

@Override
protected Boolean doInBackground(Action... actions) {

RemoteOperationResult result = new DeleteNotificationRemoteOperation(notification.notificationId)
.execute(client);

return result.isSuccess();
}

@Override
protected void onPostExecute(Boolean success) {
notificationsActivity.onRemovedNotification(success, holder);
}
}
Loading

0 comments on commit fc5db89

Please sign in to comment.