summaryrefslogtreecommitdiffstats
path: root/android/source/src/java/org/libreoffice/ColorPaletteAdapter.java
blob: 6ec6aa138f663c87928e4c88ceee6f444f8dcda6 (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
package org.libreoffice;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;


public class ColorPaletteAdapter extends RecyclerView.Adapter<ColorPaletteAdapter.ColorPaletteViewHolder> {

    int[][] color_palette;
    Context mContext;
    int upperSelectedBox = -1;
    int selectedBox = 0;
    boolean animate;
    ColorPaletteListener colorPaletteListener;

    public ColorPaletteAdapter(Context mContext, ColorPaletteListener colorPaletteListener) {
        this.mContext = mContext;
        this.color_palette = new int[11][8];
        this.colorPaletteListener = colorPaletteListener;
    }

    @Override
    public ColorPaletteViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(mContext).inflate(R.layout.colorbox, parent, false);
        return new ColorPaletteViewHolder(item);
    }


    public int getSelectedBox() {
        return selectedBox;
    }

    @Override
    public void onBindViewHolder(final ColorPaletteViewHolder holder, int position) {

        holder.colorBox.setBackgroundColor(color_palette[upperSelectedBox][position]);
        if (selectedBox == position) {
            holder.colorBox.setImageResource(R.drawable.ic_done_all_white_12dp);
        } else {
            holder.colorBox.setImageDrawable(null);
        }

        holder.colorBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LibreOfficeMainActivity.setDocumentChanged(true);
                setPosition(holder.getAdapterPosition());
            }
        });
        if (animate) //it will only animate when the upper color box is selected
            setAnimation(holder.colorBox);

    }

    private void setAnimation(View viewToAnimate) {
        Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.fade_in);
        viewToAnimate.startAnimation(animation);
    }

    @Override
    public int getItemCount() {
        return color_palette[0].length;
    }

    private void setPosition(int position) {
        this.selectedBox = position;
        colorPaletteListener.applyColor(color_palette[upperSelectedBox][position]);
        animate = false;
        updateAdapter();
    }

    public void setPosition(int upperSelectedBox, int position) {
        if (this.upperSelectedBox != upperSelectedBox) {
            this.upperSelectedBox = upperSelectedBox;
            this.selectedBox = position;
            colorPaletteListener.applyColor(color_palette[upperSelectedBox][position]);
            animate = true;
            updateAdapter();
        }
    }

    /*
        this is for InvalidationHandler when .uno:FontColor is captured
     */
    public void changePosition(int upperSelectedBox, int position) {
            if(this.upperSelectedBox != upperSelectedBox){
                this.upperSelectedBox = upperSelectedBox;
                animate=true;
            }

            this.selectedBox = position;

            updateAdapter();

    }

    public void setColorPalette(int[][] color_palette) {
        this.color_palette = color_palette;
        this.upperSelectedBox = 0;
        this.selectedBox = 0;
    }

    private void updateAdapter(){

        LOKitShell.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                ColorPaletteAdapter.this.notifyDataSetChanged();
            }
        });
    }


    class ColorPaletteViewHolder extends RecyclerView.ViewHolder {

        ImageButton colorBox;

        public ColorPaletteViewHolder(View itemView) {
            super(itemView);
            colorBox = itemView.findViewById(R.id.fontColorBox);
        }
    }


}