Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AccessControlAdapter.java « board « ui « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c98a5d1009477fb1e9b279c2619d1a480bfc234 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package it.niedermann.nextcloud.deck.ui.board;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageButton;
import androidx.appcompat.widget.SwitchCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;

import java.util.List;

import butterknife.BindDrawable;
import butterknife.BindView;
import butterknife.ButterKnife;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.model.AccessControl;
import it.niedermann.nextcloud.deck.model.enums.DBStatus;
import it.niedermann.nextcloud.deck.util.ViewUtil;

public class AccessControlAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    @NonNull
    private List<AccessControl> accessControls;
    @NonNull
    private AccessControlChangedListener accessControlChangedListener;
    @Nullable
    private Context context;

    AccessControlAdapter(@NonNull List<AccessControl> accessControls, @NonNull AccessControlChangedListener accessControlChangedListener, @Nullable Context context) {
        super();
        this.accessControls = accessControls;
        this.accessControlChangedListener = accessControlChangedListener;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_access_control_owner, parent, false);
            return new OwnerViewHolder(v);
        } else if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_access_control, parent, false);
            return new AccessControlViewHolder(v);
        }
        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        AccessControl ac = accessControls.get(position);
        if (holder instanceof OwnerViewHolder) {
            OwnerViewHolder ownerHolder = (OwnerViewHolder) holder;
            ownerHolder.owner.setText(ac.getUser().getDisplayname());

            if (context != null) {
                try {
                    ViewUtil.addAvatar(context, ownerHolder.avatar, SingleAccountHelper.getCurrentSingleSignOnAccount(context).url, ac.getUser().getUid(), R.drawable.ic_person_grey600_24dp);
                } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
                    e.printStackTrace();
                }
            }
        } else if (holder instanceof AccessControlViewHolder) {
            AccessControlViewHolder acHolder = (AccessControlViewHolder) holder;

            if (context != null) {
                try {
                    ViewUtil.addAvatar(context, acHolder.avatar, SingleAccountHelper.getCurrentSingleSignOnAccount(context).url, ac.getUser().getUid(), R.drawable.ic_person_grey600_24dp);
                } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
                    e.printStackTrace();
                }
            }

            acHolder.username.setText(ac.getUser().getDisplayname());
            acHolder.username.setCompoundDrawables(null, null, ac.getStatus() == DBStatus.LOCAL_EDITED.getId() ? acHolder.syncIcon : null, null);
            // TODO remove from list when deleted
            acHolder.deleteButton.setOnClickListener((v) -> accessControlChangedListener.deleteAccessControl(ac));

            acHolder.switchEdit.setChecked(ac.isPermissionEdit());
            acHolder.switchEdit.setOnCheckedChangeListener((buttonView, isChecked) -> {
                ac.setPermissionEdit(isChecked);
                accessControlChangedListener.updateAccessControl(ac);
            });

            acHolder.switchManage.setChecked(ac.isPermissionManage());
            acHolder.switchManage.setOnCheckedChangeListener((buttonView, isChecked) -> {
                ac.setPermissionManage(isChecked);
                accessControlChangedListener.updateAccessControl(ac);
                acHolder.username.setCompoundDrawables(null, null, null, null);
            });

            acHolder.switchShare.setChecked(ac.isPermissionShare());
            acHolder.switchShare.setOnCheckedChangeListener((buttonView, isChecked) -> {
                ac.setPermissionShare(isChecked);
                accessControlChangedListener.updateAccessControl(ac);
            });
        }
    }

    @Override
    public int getItemCount() {
        return accessControls.size();
    }

    @Override
    public int getItemViewType(int position) {
        return (position == 0) ? TYPE_HEADER : TYPE_ITEM;
    }

    static class AccessControlViewHolder extends RecyclerView.ViewHolder {
        @BindDrawable(R.drawable.ic_sync_blue_24dp)
        Drawable syncIcon;
        @BindView(R.id.avatar)
        ImageView avatar;
        @BindView(R.id.username)
        TextView username;
        @BindView(R.id.delete)
        AppCompatImageButton deleteButton;
        @BindView(R.id.permission_edit)
        SwitchCompat switchEdit;
        @BindView(R.id.permission_manage)
        SwitchCompat switchManage;
        @BindView(R.id.permission_share)
        SwitchCompat switchShare;

        private AccessControlViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class OwnerViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.avatar)
        ImageView avatar;
        @BindView(R.id.owner)
        TextView owner;

        private OwnerViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    public interface AccessControlChangedListener {
        void updateAccessControl(AccessControl accessControl);

        void deleteAccessControl(AccessControl ac);
    }
}