aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/activity/EditExtractionActivity.java
blob: c53a4e1f6f9deabf28c5510f26c0ee9ba6d5368a (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * 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.AdapterView;
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.entity.IdObject;
import net.lacolaco.smileessence.logging.Logger;
import net.lacolaco.smileessence.view.DialogHelper;
import net.lacolaco.smileessence.view.adapter.OrderedCustomListAdapter;
import net.lacolaco.smileessence.view.dialog.EditTextDialogFragment;
import net.lacolaco.smileessence.viewmodel.IViewModel;

public class EditExtractionActivity extends Activity implements AbsListView.MultiChoiceModeListener {

    // ------------------------------ FIELDS ------------------------------

    private OrderedCustomListAdapter<ExtractionWordViewModel> adapter;

    // --------------------- GETTER / SETTER METHODS ---------------------

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

    // ------------------------ INTERFACE METHODS ------------------------


    // --------------------- Interface Callback ---------------------

    @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) {
    }

    // --------------------- Interface MultiChoiceModeListener ---------------------

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

    // ------------------------ OVERRIDE METHODS ------------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(Application.getInstance().getThemeResId());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_edit_list);

        initializeViews();
        Logger.debug("onCreate");
    }

    @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)) {
                ExtractionWordViewModel vm = adapter.getItem(i);
                vm.getExtractionWord().remove();
                adapter.removeItem(vm);
            }
        }
        adapter.notifyDataSetChanged();
        updateListView();
    }

    private void addNewExtractionWord() {
        EditTextDialogFragment dialogFragment = new EditTextDialogFragment() {
            @Override
            public void onTextInput(String text) {
                if (TextUtils.isEmpty(text.trim())) {
                    return;
                }
                ExtractionWord extractionWord = ExtractionWord.add(text);
                adapter.addItem(new ExtractionWordViewModel(extractionWord));
                adapter.notifyDataSetChanged();
                updateListView();
            }
        };
        dialogFragment.setParams(getString(R.string.dialog_title_add), "");
        DialogHelper.showDialog(this, dialogFragment);
    }

    private void initializeViews() {
        ListView listView = getListView();
        adapter = new OrderedCustomListAdapter<>(this);
        for (ExtractionWord ew : ExtractionWord.cached()) {
            adapter.addItem(new ExtractionWordViewModel(ew));
        }
        adapter.update();
        listView.setAdapter(adapter);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
        listView.setMultiChoiceModeListener(this);
    }

    private void updateListView() {
        getListView().requestLayout();
    }


    // --------------------- Interface IViewModel ---------------------


    private static class ExtractionWordViewModel implements IViewModel, IdObject {
        private final ExtractionWord ew;

        public ExtractionWordViewModel(ExtractionWord ew) {
            this.ew = ew;
        }

        public ExtractionWord getExtractionWord() {
            return ew;
        }

        @Override
        public View getView(Activity activity, LayoutInflater inflater, View convertedView) {
            if (convertedView == null) {
                convertedView = inflater.inflate(R.layout.list_item_simple_text, null);
            }
            TextView textView = (TextView) convertedView.findViewById(R.id.list_item_textview);
            textView.setText(ew.getPatternString());
            return convertedView;
        }

        @Override
        public long getId() {
            return ew.getPatternString().hashCode();
        }
    }
}