1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.dashboard.suggestions;
18
19import android.content.Context;
20import android.support.v7.widget.RecyclerView;
21import android.support.v7.widget.helper.ItemTouchHelper;
22
23import com.android.settings.R;
24import com.android.settings.overlay.FeatureFactory;
25import com.android.settingslib.drawer.Tile;
26import com.android.settingslib.suggestions.SuggestionParser;
27
28public class SuggestionDismissController extends ItemTouchHelper.SimpleCallback {
29
30    public interface Callback {
31
32        /**
33         * Returns suggestion tile data from the callback
34         */
35        Tile getSuggestionForPosition(int position);
36
37        /**
38         * Called when a suggestion is dismissed.
39         */
40        void onSuggestionDismissed(Tile suggestion);
41    }
42
43    private final Context mContext;
44    private final SuggestionFeatureProvider mSuggestionFeatureProvider;
45    private final SuggestionParser mSuggestionParser;
46    private final Callback mCallback;
47
48    public SuggestionDismissController(Context context, RecyclerView recyclerView,
49            SuggestionParser parser, Callback callback) {
50        super(0, ItemTouchHelper.START | ItemTouchHelper.END);
51        mContext = context;
52        mSuggestionParser = parser;
53        mSuggestionFeatureProvider = FeatureFactory.getFactory(context)
54                .getSuggestionFeatureProvider(context);
55        mCallback = callback;
56        final ItemTouchHelper itemTouchHelper = new ItemTouchHelper(this);
57        itemTouchHelper.attachToRecyclerView(recyclerView);
58    }
59
60    @Override
61    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
62            RecyclerView.ViewHolder target) {
63        return true;
64    }
65
66    @Override
67    public int getSwipeDirs(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
68        final int layoutId = viewHolder.getItemViewType();
69        if (layoutId == R.layout.suggestion_tile
70                || layoutId == R.layout.suggestion_tile_remote_container) {
71            // Only return swipe direction for suggestion tiles. All other types are not swipeable.
72            return super.getSwipeDirs(recyclerView, viewHolder);
73        }
74        return 0;
75    }
76
77    @Override
78    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
79        if (mCallback == null) {
80            return;
81        }
82        final Tile suggestion = mCallback.getSuggestionForPosition(viewHolder.getAdapterPosition());
83        mSuggestionFeatureProvider.dismissSuggestion(mContext, mSuggestionParser, suggestion);
84        mCallback.onSuggestionDismissed(suggestion);
85    }
86}
87