aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/activity/EditExtractionActivity.java
blob: 3876d50680e8cdbc164dee99db195c5a0598bc0b (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
164
165
166
167
168
169
170
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2012-2014 lacolaco.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.lacolaco.smileessence.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.util.SparseBooleanArray;
import android.view.*;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;
import net.lacolaco.smileessence.Application;
import net.lacolaco.smileessence.R;
import net.lacolaco.smileessence.data.ExtractionWord;
import net.lacolaco.smileessence.logging.Logger;
import net.lacolaco.smileessence.view.DialogHelper;
import net.lacolaco.smileessence.view.adapter.CustomListAdapter;
import net.lacolaco.smileessence.view.dialog.EditTextDialogFragment;

import java.util.ArrayList;
import java.util.List;

public class EditExtractionActivity extends Activity implements AbsListView.MultiChoiceModeListener {
    private List<ExtractionWord> list;
    private CustomListAdapter<ExtractionWord> adapter;

    private ListView getListView() {
        return (ListView) findViewById(R.id.listview_edit_list);
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        menu.clear();
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.edit_list, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_edit_list_delete: {
                deleteSelectedItems();
            }
        }
        mode.finish();
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
    }

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Logger.debug("onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_edit_list);

        ListView listView = getListView();
        list = new ArrayList<>();
        adapter = new CustomListAdapter<ExtractionWord>() {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = EditExtractionActivity.this.getLayoutInflater().inflate(R.layout.list_item_simple_text, null);
                }
                TextView textView = (TextView) convertView.findViewById(R.id.list_item_textview);
                textView.setText(getItem(position).getPatternString());
                return convertView;
            }

            @Override
            protected List<ExtractionWord> getList() {
                return list;
            }
        };
        list.addAll(ExtractionWord.cached());
        adapter.update();
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem add = menu.add(Menu.NONE, R.id.menu_edit_list_add, Menu.NONE, "");
        add.setIcon(android.R.drawable.ic_menu_add);
        add.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_edit_list_add: {
                addNewExtractionWord();
                break;
            }
            case android.R.id.home: {
                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
        }
        return true;
    }

    // -------------------------- OTHER METHODS --------------------------

    public void deleteSelectedItems() {
        SparseBooleanArray checkedItems = getListView().getCheckedItemPositions();
        for (int i = adapter.getCount() - 1; i > -1; i--) {
            if (checkedItems.get(i)) {
                ExtractionWord ew = adapter.getItem(i);
                list.remove(ew);
                ew.remove();
            }
        }
        adapter.update();
    }

    private void addNewExtractionWord() {
        EditTextDialogFragment dialogFragment = new EditTextDialogFragment() {
            @Override
            public void onTextInput(String text) {
                if (TextUtils.isEmpty(text.trim())) {
                    return;
                }
                ExtractionWord extractionWord = ExtractionWord.add(text);
                list.add(extractionWord);
                adapter.update();
            }
        };
        dialogFragment.setParams(getString(R.string.dialog_title_add), "");
        dialogFragment.show(getFragmentManager(), "EDIT_TEXT");
    }
}