aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/data/ExtractionWord.kt
blob: 5f5331122aa42ffb4cfaeffd20740ffa3eebf72f (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
package net.lacolaco.smileessence.data

import android.content.ContentValues
import java.util.regex.Pattern

@Deprecated("Remove when ManagePagesActivity is done")
class ExtractionWord private constructor(val patternString: String) {
    val pattern: Pattern by lazy {
        Pattern.compile(patternString)
    }

    fun remove() {
        if (DbHelper.db.delete("extraction_words", "pattern_string = ?", arrayOf(patternString)) == 0)
            throw IllegalStateException("Could not remove patternString=$patternString from extraction_words")
        cache.remove(this)
    }

    companion object {
        private lateinit var cache: MutableSet<ExtractionWord>

        fun cached(): Collection<ExtractionWord> {
            return cache
        }

        fun load() {
            cache = HashSet()
            DbHelper.db.query("extraction_words", null, null, null, null, null, null).use { cursor ->
                while (cursor != null && cursor.moveToNext())
                    cache.add(ExtractionWord(cursor.getString(cursor.getColumnIndexOrThrow("pattern_string"))))
            }
        }

        fun add(patternString: String): ExtractionWord {
            val extractionWord = ExtractionWord(patternString)
            val values = ContentValues()
            values.put("pattern_string", extractionWord.patternString)
            DbHelper.db.insertOrThrow("extraction_words", null, values)
            cache.add(extractionWord)
            return extractionWord
        }
    }
}