Updated History.kt to use kotlin's delegate feature

This commit is contained in:
tom5079
2020-08-26 18:01:12 +09:00
parent 216914882c
commit ceac01533a
9 changed files with 38 additions and 60 deletions

View File

@@ -1,6 +0,0 @@
<component name="CopyrightManager">
<copyright>
<option name="notice" value=" Copyright &amp;#36;today.year tom5079&#10;&#10; Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);&#10; you may not use this file except in compliance with the License.&#10; You may obtain a copy of the License at&#10;&#10; http://www.apache.org/licenses/LICENSE-2.0&#10;&#10; Unless required by applicable law or agreed to in writing, software&#10; distributed under the License is distributed on an &quot;AS IS&quot; BASIS,&#10; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&#10; See the License for the specific language governing permissions and&#10; limitations under the License." />
<option name="myName" value="Apache" />
</copyright>
</component>

View File

@@ -2,7 +2,6 @@
<settings>
<module2copyright>
<element module="Pupil" copyright="GPL" />
<element module="libpupil" copyright="Apache" />
</module2copyright>
</settings>
</component>

View File

@@ -1,3 +0,0 @@
<component name="DependencyValidationManager">
<scope name="libpupil" pattern="file[libpupil]:*/" />
</component>

View File

@@ -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)

View File

@@ -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()

View File

@@ -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
}
}

View File

@@ -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)

View File

@@ -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()
}
}

View File

@@ -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>()
}
}