| 1 | /* |
| 2 | * Copyright (C) 2013 Keith Kildare |
| 3 | * |
| 4 | * This file is part of SimplyDo. |
| 5 | * |
| 6 | * SimplyDo is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * SimplyDo is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with SimplyDo. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | package kdk.android.simplydo; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
| 24 | |
| 25 | import android.app.AlertDialog; |
| 26 | import android.app.Dialog; |
| 27 | import android.content.Context; |
| 28 | import android.content.DialogInterface; |
| 29 | import android.util.Log; |
| 30 | import android.widget.ArrayAdapter; |
| 31 | import android.widget.Button; |
| 32 | import android.widget.Toast; |
| 33 | |
| 34 | public class MoveToAction |
| 35 | { |
| 36 | private Context context; |
| 37 | private DataViewer dataViewer; |
| 38 | private ListPropertiesAdapter listPropertiesAdapter; |
| 39 | private ItemPropertiesAdapter itemPropertiesAdapter; |
| 40 | |
| 41 | private ArrayAdapter<String> aa; |
| 42 | private Button okButton; |
| 43 | private DialogInterface.OnClickListener itemClickedListener; |
| 44 | private DialogInterface.OnClickListener listSelectedListener; |
| 45 | private DialogInterface.OnClickListener cancelClickedListener; |
| 46 | private Integer selectedItem = null; |
| 47 | private List<ListDesc> dataViewList = new ArrayList<ListDesc>(); |
| 48 | private ItemDesc ctxItem; |
| 49 | |
| 50 | public MoveToAction( |
| 51 | Context context, |
| 52 | DataViewer dataViewer, |
| 53 | ListPropertiesAdapter listPropertiesAdapter, |
| 54 | ItemPropertiesAdapter itemPropertiesAdapter) |
| 55 | { |
| 56 | this.context = context; |
| 57 | this.dataViewer = dataViewer; |
| 58 | this.listPropertiesAdapter = listPropertiesAdapter; |
| 59 | this.itemPropertiesAdapter = itemPropertiesAdapter; |
| 60 | |
| 61 | itemClickedListener = new DialogInterface.OnClickListener() { |
| 62 | @Override |
| 63 | public void onClick(DialogInterface dialog, int which) |
| 64 | { |
| 65 | itemClicked(dialog, which); |
| 66 | } |
| 67 | }; |
| 68 | listSelectedListener = new DialogInterface.OnClickListener() { |
| 69 | @Override |
| 70 | public void onClick(DialogInterface dialog, int id) { |
| 71 | listSelected(); |
| 72 | } |
| 73 | }; |
| 74 | cancelClickedListener = new DialogInterface.OnClickListener() { |
| 75 | @Override |
| 76 | public void onClick(DialogInterface dialog, int id) { |
| 77 | Log.v(L.TAG, "MoveToAction.cancelClickedListener: Cancel"); |
| 78 | endDialog(); |
| 79 | } |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | public Dialog createDialog() |
| 85 | { |
| 86 | aa = new ArrayAdapter<String>(context, android.R.layout.select_dialog_singlechoice); |
| 87 | |
| 88 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 89 | builder.setTitle(R.string.moveToTitle); |
| 90 | builder.setSingleChoiceItems(aa, -1, itemClickedListener); |
| 91 | builder.setPositiveButton(R.string.moveToPositive, listSelectedListener); |
| 92 | builder.setNegativeButton(R.string.moveToNegative, cancelClickedListener); |
| 93 | builder.setCancelable(true); |
| 94 | |
| 95 | AlertDialog alertDialog = builder.create(); |
| 96 | return alertDialog; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | public void prepareDialog(Dialog dialog, ItemDesc ctxItem) |
| 101 | { |
| 102 | if(aa == null) |
| 103 | { |
| 104 | Log.e(L.TAG, "MoveToAction.prepareDialog() called before createDialog()"); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | ListDesc selectedListDesc = dataViewer.getSelectedList(); |
| 110 | dataViewList.clear(); |
| 111 | dataViewList.addAll(dataViewer.getListData()); |
| 112 | dataViewList.remove(selectedListDesc); |
| 113 | |
| 114 | aa.clear(); |
| 115 | int selectedId = selectedListDesc.getId(); |
| 116 | for(int i = 0; i < dataViewList.size(); i++) |
| 117 | { |
| 118 | ListDesc listDesc = dataViewList.get(i); |
| 119 | if(listDesc.getId() != selectedId) |
| 120 | { |
| 121 | aa.add(listDesc.getLabel()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | aa.notifyDataSetChanged(); |
| 126 | |
| 127 | AlertDialog alertDialog = (AlertDialog)dialog; |
| 128 | okButton = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); |
| 129 | okButton.setEnabled(selectedItem != null); |
| 130 | |
| 131 | this.ctxItem = ctxItem; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | private void itemClicked(DialogInterface dialog, int which) |
| 136 | { |
| 137 | Log.d(L.TAG, "MoveToAction.itemClicked(): Selected " + which); |
| 138 | |
| 139 | if(!okButton.isEnabled()) |
| 140 | { |
| 141 | okButton.setEnabled(true); |
| 142 | } |
| 143 | |
| 144 | selectedItem = which; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | private void listSelected() |
| 149 | { |
| 150 | if(selectedItem == null) |
| 151 | { |
| 152 | Log.e(L.TAG, "MoveToAction.listSelected(): called without a selected item"); |
| 153 | return; |
| 154 | } |
| 155 | Log.d(L.TAG, "MoveToAction.listSelected(): called"); |
| 156 | |
| 157 | ListDesc listDesc = dataViewList.get(selectedItem); |
| 158 | dataViewer.moveItem(ctxItem, listDesc.getId()); |
| 159 | itemPropertiesAdapter.notifyDataSetChanged(); |
| 160 | listPropertiesAdapter.notifyDataSetChanged(); |
| 161 | |
| 162 | String itemMoved = String.format(context.getString(R.string.moveToMoved), ctxItem.getLabel(), listDesc.getLabel()); |
| 163 | Toast t = Toast.makeText(context, itemMoved, Toast.LENGTH_SHORT); |
| 164 | t.show(); |
| 165 | |
| 166 | endDialog(); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | private void endDialog() |
| 171 | { |
| 172 | // make sure left overs from prepareDialog don't hold unneeded objects |
| 173 | ctxItem = null; |
| 174 | okButton = null; |
| 175 | dataViewList.clear(); |
| 176 | aa.clear(); |
| 177 | } |
| 178 | } |