Updated History.kt to use kotlin's delegate feature
This commit is contained in:
6
.idea/copyright/Apache.xml
generated
6
.idea/copyright/Apache.xml
generated
@@ -1,6 +0,0 @@
|
||||
<component name="CopyrightManager">
|
||||
<copyright>
|
||||
<option name="notice" value=" Copyright &#36;today.year tom5079 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." />
|
||||
<option name="myName" value="Apache" />
|
||||
</copyright>
|
||||
</component>
|
||||
1
.idea/copyright/profiles_settings.xml
generated
1
.idea/copyright/profiles_settings.xml
generated
@@ -2,7 +2,6 @@
|
||||
<settings>
|
||||
<module2copyright>
|
||||
<element module="Pupil" copyright="GPL" />
|
||||
<element module="libpupil" copyright="Apache" />
|
||||
</module2copyright>
|
||||
</settings>
|
||||
</component>
|
||||
3
.idea/scopes/libpupil.xml
generated
3
.idea/scopes/libpupil.xml
generated
@@ -1,3 +0,0 @@
|
||||
<component name="DependencyValidationManager">
|
||||
<scope name="libpupil" pattern="file[libpupil]:*/" />
|
||||
</component>
|
||||
@@ -33,15 +33,15 @@ import com.google.android.gms.security.ProviderInstaller
|
||||
import com.google.firebase.analytics.FirebaseAnalytics
|
||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||
import xyz.quaver.proxy
|
||||
import xyz.quaver.pupil.util.Histories
|
||||
import xyz.quaver.pupil.util.GalleryList
|
||||
import xyz.quaver.pupil.util.getProxy
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
|
||||
class Pupil : MultiDexApplication() {
|
||||
|
||||
lateinit var histories: Histories
|
||||
lateinit var favorites: Histories
|
||||
lateinit var histories: GalleryList
|
||||
lateinit var favorites: GalleryList
|
||||
|
||||
init {
|
||||
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
||||
@@ -71,8 +71,8 @@ class Pupil : MultiDexApplication() {
|
||||
preference.edit().remove("dl_location").apply()
|
||||
}
|
||||
|
||||
histories = Histories(File(ContextCompat.getDataDir(this), "histories.json"))
|
||||
favorites = Histories(File(ContextCompat.getDataDir(this), "favorites.json"))
|
||||
histories = GalleryList(File(ContextCompat.getDataDir(this), "histories.json"))
|
||||
favorites = GalleryList(File(ContextCompat.getDataDir(this), "favorites.json"))
|
||||
|
||||
if (BuildConfig.DEBUG)
|
||||
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false)
|
||||
|
||||
@@ -53,7 +53,7 @@ import xyz.quaver.pupil.BuildConfig
|
||||
import xyz.quaver.pupil.Pupil
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.types.Tag
|
||||
import xyz.quaver.pupil.util.Histories
|
||||
import xyz.quaver.pupil.util.GalleryList
|
||||
import xyz.quaver.pupil.util.download.Cache
|
||||
import xyz.quaver.pupil.util.wordCapitalize
|
||||
import java.util.*
|
||||
@@ -68,7 +68,7 @@ class GalleryBlockAdapter(private val glide: RequestManager, private val galleri
|
||||
PREV
|
||||
}
|
||||
|
||||
private lateinit var favorites: Histories
|
||||
private lateinit var favorites: GalleryList
|
||||
|
||||
val timer = Timer()
|
||||
|
||||
|
||||
@@ -109,8 +109,8 @@ class MainActivity : AppCompatActivity() {
|
||||
private var loadingJob: Job? = null
|
||||
private var currentPage = 0
|
||||
|
||||
private lateinit var histories: Histories
|
||||
private lateinit var favorites: Histories
|
||||
private lateinit var histories: GalleryList
|
||||
private lateinit var favorites: GalleryList
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@@ -1025,13 +1025,13 @@ class MainActivity : AppCompatActivity() {
|
||||
Mode.HISTORY -> {
|
||||
when {
|
||||
query.isEmpty() -> {
|
||||
histories.toList().also {
|
||||
histories.reversed().also {
|
||||
totalItems = it.size
|
||||
}
|
||||
}
|
||||
else -> {
|
||||
val result = doSearch(query).sorted()
|
||||
histories.filter { result.binarySearch(it) >= 0 }.also {
|
||||
histories.reversed().filter { result.binarySearch(it) >= 0 }.also {
|
||||
totalItems = it.size
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ import xyz.quaver.Code
|
||||
import xyz.quaver.pupil.Pupil
|
||||
import xyz.quaver.pupil.R
|
||||
import xyz.quaver.pupil.adapters.ReaderAdapter
|
||||
import xyz.quaver.pupil.util.Histories
|
||||
import xyz.quaver.pupil.util.GalleryList
|
||||
import xyz.quaver.pupil.util.download.Cache
|
||||
import xyz.quaver.pupil.util.download.DownloadWorker
|
||||
import java.util.*
|
||||
@@ -78,7 +78,7 @@ class ReaderActivity : AppCompatActivity() {
|
||||
|
||||
private var menu: Menu? = null
|
||||
|
||||
private lateinit var favorites: Histories
|
||||
private lateinit var favorites: GalleryList
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
@@ -23,69 +23,57 @@ import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
class Histories(private val file: File) : ArrayList<Int>() {
|
||||
class GalleryList(private val file: File, private val list: MutableSet<Int> = mutableSetOf()) : MutableSet<Int> by list {
|
||||
|
||||
init {
|
||||
if (!file.exists())
|
||||
file.parentFile?.mkdirs()
|
||||
|
||||
try {
|
||||
load()
|
||||
} catch (e: Exception) {
|
||||
save()
|
||||
}
|
||||
load()
|
||||
}
|
||||
|
||||
fun load() : Histories {
|
||||
return apply {
|
||||
super.clear()
|
||||
super.addAll(
|
||||
Json.decodeFromString(file.bufferedReader().use { it.readText() })
|
||||
fun load() {
|
||||
synchronized(this) {
|
||||
if (!file.exists())
|
||||
file.parentFile?.mkdirs()
|
||||
|
||||
list.clear()
|
||||
list.addAll(
|
||||
Json.decodeFromString<List<Int>>(file.bufferedReader().use { it.readText() })
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun save() {
|
||||
file.writeText(Json.encodeToString(toList()))
|
||||
synchronized(this) {
|
||||
file.writeText(Json.encodeToString(list))
|
||||
}
|
||||
}
|
||||
|
||||
override fun add(element: Int): Boolean {
|
||||
load()
|
||||
|
||||
if (contains(element))
|
||||
super.remove(element)
|
||||
|
||||
super.add(0, element)
|
||||
|
||||
save()
|
||||
|
||||
return true
|
||||
return list.add(element).also {
|
||||
save()
|
||||
}
|
||||
}
|
||||
|
||||
override fun addAll(elements: Collection<Int>): Boolean {
|
||||
load()
|
||||
|
||||
for (e in elements) {
|
||||
if (contains(e))
|
||||
super.remove(e)
|
||||
super.add(0, e)
|
||||
return list.addAll(elements).also {
|
||||
save()
|
||||
}
|
||||
|
||||
save()
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun remove(element: Int): Boolean {
|
||||
load()
|
||||
val retval = super.remove(element)
|
||||
save()
|
||||
|
||||
return retval
|
||||
return list.remove(element).also {
|
||||
save()
|
||||
}
|
||||
}
|
||||
|
||||
override fun clear() {
|
||||
super.clear()
|
||||
list.clear()
|
||||
save()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -26,14 +26,14 @@ package xyz.quaver.pupil
|
||||
* See [testing documentation](http://d.android.com/tools/testing).
|
||||
*/
|
||||
|
||||
import android.util.SparseArray
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.junit.Test
|
||||
|
||||
class ExampleUnitTest {
|
||||
|
||||
@Test
|
||||
fun test() {
|
||||
val arr = SparseArray<Float>()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user