| 1 | /* |
| 2 | * Copyright (C) 2010, 2011 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.util.Log; |
| 26 | |
| 27 | public class SimpleDataViewer implements DataViewer |
| 28 | { |
| 29 | private DataManager dataManager; |
| 30 | |
| 31 | private List<ItemDesc> itemData = new ArrayList<ItemDesc>(); |
| 32 | private List<ListDesc> listData = new ArrayList<ListDesc>(); |
| 33 | |
| 34 | private ListDesc selectedList; |
| 35 | |
| 36 | |
| 37 | public SimpleDataViewer(DataManager dataManager) |
| 38 | { |
| 39 | this.dataManager = dataManager; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | @Override |
| 44 | public List<ItemDesc> getItemData() |
| 45 | { |
| 46 | return itemData; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | @Override |
| 51 | public List<ListDesc> getListData() |
| 52 | { |
| 53 | return listData; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void setSelectedList(ListDesc selectedList) |
| 58 | { |
| 59 | this.selectedList = selectedList; |
| 60 | if(selectedList == null) |
| 61 | { |
| 62 | itemData.clear(); |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | fetchItems(selectedList.getId()); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public ListDesc getSelectedList() |
| 72 | { |
| 73 | return selectedList; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void fetchLists() |
| 78 | { |
| 79 | listData = dataManager.fetchLists(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public ListDesc fetchList(int listId) |
| 84 | { |
| 85 | ListDesc rv = null; |
| 86 | |
| 87 | for(ListDesc list : listData) |
| 88 | { |
| 89 | if(list.getId() == listId) |
| 90 | { |
| 91 | rv = list; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return rv; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void fetchItems(int listId) |
| 101 | { |
| 102 | itemData.clear(); |
| 103 | dataManager.fetchItems(listId, itemData); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void updateItemActiveness(ItemDesc item, boolean active) |
| 108 | { |
| 109 | int itemId = item.getId(); |
| 110 | dataManager.updateItemActiveness(itemId, active); |
| 111 | if(selectedList != null) |
| 112 | { |
| 113 | fetchItems(selectedList.getId()); |
| 114 | } |
| 115 | fetchLists(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void updateItemStarness(ItemDesc item, boolean star) |
| 120 | { |
| 121 | int itemId = item.getId(); |
| 122 | dataManager.updateItemStarness(itemId, star); |
| 123 | if(selectedList != null) |
| 124 | { |
| 125 | fetchItems(selectedList.getId()); |
| 126 | } |
| 127 | fetchLists(); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void updateItemLabel(ItemDesc item, String newLabel) |
| 132 | { |
| 133 | int itemId = item.getId(); |
| 134 | dataManager.updateItemLabel(itemId, newLabel); |
| 135 | if(selectedList != null) |
| 136 | { |
| 137 | fetchItems(selectedList.getId()); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void updateListLabel(int listId, String newLabel) |
| 143 | { |
| 144 | dataManager.updateListLabel(listId, newLabel); |
| 145 | fetchLists(); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void moveItem(ItemDesc item, int toListId) |
| 150 | { |
| 151 | int itemId = item.getId(); |
| 152 | dataManager.moveItem(itemId, toListId); |
| 153 | if(selectedList != null) |
| 154 | { |
| 155 | fetchItems(selectedList.getId()); |
| 156 | } |
| 157 | fetchLists(); |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public void createList(String label) |
| 162 | { |
| 163 | dataManager.createList(label); |
| 164 | fetchLists(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void createItem(String label) |
| 169 | { |
| 170 | int listId = selectedList.getId(); |
| 171 | dataManager.createItem(listId, label); |
| 172 | fetchItems(listId); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void deleteInactive() |
| 177 | { |
| 178 | if(selectedList == null) |
| 179 | { |
| 180 | Log.e(L.TAG, "deleteInactive() called but no list is selected"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | int listId = selectedList.getId(); |
| 185 | dataManager.deleteInactive(listId); |
| 186 | fetchItems(listId); |
| 187 | fetchLists(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void deleteList(int listId) |
| 192 | { |
| 193 | dataManager.deleteList(listId); |
| 194 | fetchLists(); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void deleteItem(ItemDesc item) |
| 199 | { |
| 200 | int itemId = item.getId(); |
| 201 | dataManager.deleteItem(itemId); |
| 202 | if(selectedList != null) |
| 203 | { |
| 204 | fetchItems(selectedList.getId()); |
| 205 | } |
| 206 | fetchLists(); |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void invalidateCache() |
| 211 | { |
| 212 | // no cache to void |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void flush() |
| 217 | { |
| 218 | // Do nothing |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void close() |
| 223 | { |
| 224 | // Do nothing |
| 225 | } |
| 226 | |
| 227 | } |