-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
SmartFolderHelper.kt
269 lines (233 loc) · 7.46 KB
/
SmartFolderHelper.kt
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package net.osmand.plus.myplaces.tracks.filters
import android.os.AsyncTask
import androidx.annotation.WorkerThread
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import net.osmand.PlatformUtil
import net.osmand.StateChangedListener
import net.osmand.plus.OsmandApplication
import net.osmand.plus.configmap.tracks.TrackItem
import net.osmand.plus.myplaces.tracks.TrackFiltersHelper
import net.osmand.plus.settings.backend.preferences.CommonPreference
import net.osmand.plus.track.data.SmartFolder
import net.osmand.util.Algorithms
import net.osmand.util.CollectionUtils
import java.util.Date
class SmartFolderHelper(val app: OsmandApplication) {
private val preference: CommonPreference<String> =
app.settings.registerStringPreference(TRACK_FILTERS_SETTINGS_PREF, "")
.makeGlobal()
.makeShared()
private val gson: Gson = GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.create()
private var smartFolderCollection: MutableList<SmartFolder> = ArrayList()
private var allAvailableTrackItems = HashSet<TrackItem>()
private var updateListeners: MutableList<SmartFolderUpdateListener> = mutableListOf()
private var isWritingSettings = false
private val settingsChangedListener = StateChangedListener<String> {
onSettingsChanged()
}
companion object {
private val LOG = PlatformUtil.getLog(SmartFolderHelper::class.java)
private const val TRACK_FILTERS_SETTINGS_PREF = "track_filters_settings_pref"
}
init {
preference.addListener(settingsChangedListener)
readSettings()
}
private fun onSettingsChanged() {
if (!isWritingSettings) {
updateSmartFolderSettings()
}
}
private fun readSettings() {
val newCollection = ArrayList<SmartFolder>()
val settingsJson = preference.get()
if (!Algorithms.isEmpty(settingsJson)) {
TrackFilterList.parseFilters(settingsJson)?.let { savedFilters ->
for (smartFolder in savedFilters) {
smartFolder.filters?.let {
val newFilters: MutableList<BaseTrackFilter> = mutableListOf()
for (filter in it) {
val newFilter =
TrackFiltersHelper.createFilter(app, filter.trackFilterType, null)
newFilter.initWithValue(filter)
newFilters.add(newFilter)
}
smartFolder.filters = newFilters
}
}
newCollection.addAll(savedFilters)
}
}
smartFolderCollection = newCollection
}
private fun updateSmartFolderSettings() {
SmartFoldersUpdateTask(app).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
}
fun resetSmartFoldersItems() {
val collection = ArrayList(smartFolderCollection)
for (smartFolder in collection) {
smartFolder.resetItems()
}
}
private fun getEnabledFilters(filters: MutableList<BaseTrackFilter>?): ArrayList<BaseTrackFilter> {
val enabledFilters = ArrayList<BaseTrackFilter>()
filters?.let {
for (filter in filters) {
if (filter.isEnabled()) {
enabledFilters.add(filter)
}
}
}
return enabledFilters
}
fun saveSmartFolder(smartFolder: SmartFolder, filters: MutableList<BaseTrackFilter>?) {
val enabledFilters = getEnabledFilters(filters)
smartFolder.filters = enabledFilters
writeSettings()
updateSmartFolderItems(smartFolder)
notifyFolderSavedListeners(smartFolder)
}
fun saveNewSmartFolder(name: String, filters: MutableList<BaseTrackFilter>?) {
val enabledFilters = getEnabledFilters(filters)
val newFolder = SmartFolder(name)
newFolder.creationTime = Date().time
newFolder.filters = enabledFilters
smartFolderCollection.add(newFolder)
updateSmartFolderItems(newFolder)
writeSettings()
notifyFolderCreatedListeners(newFolder)
}
fun notifyUpdateListeners() {
for (listener in updateListeners) {
listener.onSmartFoldersUpdated()
}
}
private fun notifyFolderCreatedListeners(smartFolder: SmartFolder) {
for (listener in updateListeners) {
listener.onSmartFolderCreated(smartFolder)
}
}
private fun notifyFolderUpdatedListeners(smartFolder: SmartFolder) {
for (listener in updateListeners) {
listener.onSmartFolderUpdated(smartFolder)
}
}
private fun notifyFolderSavedListeners(smartFolder: SmartFolder) {
for (listener in updateListeners) {
listener.onSmartFolderSaved(smartFolder)
}
}
private fun notifyFolderRenamedListeners(smartFolder: SmartFolder) {
for (listener in updateListeners) {
listener.onSmartFolderRenamed(smartFolder)
}
}
fun addUpdateListener(listener: SmartFolderUpdateListener) {
if (!updateListeners.contains(listener)) {
updateListeners = CollectionUtils.addToList(updateListeners, listener)
}
}
fun removeUpdateListener(listener: SmartFolderUpdateListener) {
if (updateListeners.contains(listener)) {
updateListeners = CollectionUtils.removeFromList(updateListeners, listener)
}
}
private fun writeSettings() {
isWritingSettings = true
val json = gson.toJson(smartFolderCollection)
preference.set(json)
isWritingSettings = false
}
fun isSmartFolderPresent(name: String): Boolean {
return getSmartFolderByName(name) != null
}
fun areSmartFoldersPresent(): Boolean {
return !smartFolderCollection.isEmpty()
}
private fun getSmartFolderByName(name: String): SmartFolder? {
for (folder in smartFolderCollection) {
if (Algorithms.stringsEqual(folder.folderName, name)) {
return folder
}
}
return null
}
fun getSmartFolders(): MutableList<SmartFolder> {
return ArrayList(smartFolderCollection)
}
fun renameSmartFolder(smartFolder: SmartFolder, newName: String) {
smartFolder.folderName = newName
writeSettings()
notifyFolderRenamedListeners(smartFolder)
}
fun deleteSmartFolder(smartFolder: SmartFolder) {
smartFolderCollection = CollectionUtils.removeFromList(smartFolderCollection, smartFolder)
writeSettings()
notifyUpdateListeners()
}
fun addTrackItemToSmartFolder(item: TrackItem) {
LOG.debug("addTrackItemToSmartFolder " + item.name)
val newSet = HashSet(allAvailableTrackItems)
newSet.add(item)
allAvailableTrackItems = newSet
addTracksToSmartFolders(arrayListOf(item), smartFolderCollection)
}
private fun addTracksToSmartFolders(items: List<TrackItem>, smartFolders: List<SmartFolder>) {
for (item in items) {
for (smartFolder in smartFolders) {
var trackAccepted = true
smartFolder.filters?.let { smartFolderFilters ->
for (filter in smartFolderFilters) {
if (!filter.isTrackAccepted(item)) {
trackAccepted = false
break
}
}
}
if (trackAccepted) {
smartFolder.addTrackItem(item)
}
}
}
}
@WorkerThread
fun updateSmartFolderItems(smartFolder: SmartFolder) {
LOG.debug("updateSmartFolderItems ${smartFolder.folderName}")
smartFolder.resetItems()
addTracksToSmartFolders(ArrayList(allAvailableTrackItems), arrayListOf(smartFolder))
notifyFolderUpdatedListeners(smartFolder)
}
fun getSmartFolder(name: String): SmartFolder? {
for (folder in smartFolderCollection) {
if (Algorithms.stringsEqual(folder.folderName, name)) {
return folder
}
}
return null
}
fun refreshSmartFolder(smartFolder: SmartFolder) {
updateSmartFolderItems(smartFolder)
}
fun getAllAvailableTrackItems(): HashSet<TrackItem> {
return HashSet(allAvailableTrackItems)
}
private inner class SmartFoldersUpdateTask(
private val app: OsmandApplication,
) : AsyncTask<Void, Void, Void?>() {
@Deprecated("Deprecated in Java")
override fun doInBackground(vararg params: Void): Void? {
readSettings()
for (folder in smartFolderCollection) {
updateSmartFolderItems(folder)
}
return null
}
@Deprecated("Deprecated in Java")
override fun onPostExecute(result: Void?) {
notifyUpdateListeners()
}
}
}