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>
|
<settings>
|
||||||
<module2copyright>
|
<module2copyright>
|
||||||
<element module="Pupil" copyright="GPL" />
|
<element module="Pupil" copyright="GPL" />
|
||||||
<element module="libpupil" copyright="Apache" />
|
|
||||||
</module2copyright>
|
</module2copyright>
|
||||||
</settings>
|
</settings>
|
||||||
</component>
|
</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.analytics.FirebaseAnalytics
|
||||||
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
import com.google.firebase.crashlytics.FirebaseCrashlytics
|
||||||
import xyz.quaver.proxy
|
import xyz.quaver.proxy
|
||||||
import xyz.quaver.pupil.util.Histories
|
import xyz.quaver.pupil.util.GalleryList
|
||||||
import xyz.quaver.pupil.util.getProxy
|
import xyz.quaver.pupil.util.getProxy
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class Pupil : MultiDexApplication() {
|
class Pupil : MultiDexApplication() {
|
||||||
|
|
||||||
lateinit var histories: Histories
|
lateinit var histories: GalleryList
|
||||||
lateinit var favorites: Histories
|
lateinit var favorites: GalleryList
|
||||||
|
|
||||||
init {
|
init {
|
||||||
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
|
||||||
@@ -71,8 +71,8 @@ class Pupil : MultiDexApplication() {
|
|||||||
preference.edit().remove("dl_location").apply()
|
preference.edit().remove("dl_location").apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
histories = Histories(File(ContextCompat.getDataDir(this), "histories.json"))
|
histories = GalleryList(File(ContextCompat.getDataDir(this), "histories.json"))
|
||||||
favorites = Histories(File(ContextCompat.getDataDir(this), "favorites.json"))
|
favorites = GalleryList(File(ContextCompat.getDataDir(this), "favorites.json"))
|
||||||
|
|
||||||
if (BuildConfig.DEBUG)
|
if (BuildConfig.DEBUG)
|
||||||
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false)
|
FirebaseAnalytics.getInstance(this).setAnalyticsCollectionEnabled(false)
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ import xyz.quaver.pupil.BuildConfig
|
|||||||
import xyz.quaver.pupil.Pupil
|
import xyz.quaver.pupil.Pupil
|
||||||
import xyz.quaver.pupil.R
|
import xyz.quaver.pupil.R
|
||||||
import xyz.quaver.pupil.types.Tag
|
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.download.Cache
|
||||||
import xyz.quaver.pupil.util.wordCapitalize
|
import xyz.quaver.pupil.util.wordCapitalize
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -68,7 +68,7 @@ class GalleryBlockAdapter(private val glide: RequestManager, private val galleri
|
|||||||
PREV
|
PREV
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var favorites: Histories
|
private lateinit var favorites: GalleryList
|
||||||
|
|
||||||
val timer = Timer()
|
val timer = Timer()
|
||||||
|
|
||||||
|
|||||||
@@ -109,8 +109,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
private var loadingJob: Job? = null
|
private var loadingJob: Job? = null
|
||||||
private var currentPage = 0
|
private var currentPage = 0
|
||||||
|
|
||||||
private lateinit var histories: Histories
|
private lateinit var histories: GalleryList
|
||||||
private lateinit var favorites: Histories
|
private lateinit var favorites: GalleryList
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
@@ -1025,13 +1025,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
Mode.HISTORY -> {
|
Mode.HISTORY -> {
|
||||||
when {
|
when {
|
||||||
query.isEmpty() -> {
|
query.isEmpty() -> {
|
||||||
histories.toList().also {
|
histories.reversed().also {
|
||||||
totalItems = it.size
|
totalItems = it.size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val result = doSearch(query).sorted()
|
val result = doSearch(query).sorted()
|
||||||
histories.filter { result.binarySearch(it) >= 0 }.also {
|
histories.reversed().filter { result.binarySearch(it) >= 0 }.also {
|
||||||
totalItems = it.size
|
totalItems = it.size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ import xyz.quaver.Code
|
|||||||
import xyz.quaver.pupil.Pupil
|
import xyz.quaver.pupil.Pupil
|
||||||
import xyz.quaver.pupil.R
|
import xyz.quaver.pupil.R
|
||||||
import xyz.quaver.pupil.adapters.ReaderAdapter
|
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.Cache
|
||||||
import xyz.quaver.pupil.util.download.DownloadWorker
|
import xyz.quaver.pupil.util.download.DownloadWorker
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -78,7 +78,7 @@ class ReaderActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
private var menu: Menu? = null
|
private var menu: Menu? = null
|
||||||
|
|
||||||
private lateinit var favorites: Histories
|
private lateinit var favorites: GalleryList
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|||||||
@@ -23,69 +23,57 @@ import kotlinx.serialization.encodeToString
|
|||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import java.io.File
|
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 {
|
init {
|
||||||
if (!file.exists())
|
load()
|
||||||
file.parentFile?.mkdirs()
|
|
||||||
|
|
||||||
try {
|
|
||||||
load()
|
|
||||||
} catch (e: Exception) {
|
|
||||||
save()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load() : Histories {
|
fun load() {
|
||||||
return apply {
|
synchronized(this) {
|
||||||
super.clear()
|
if (!file.exists())
|
||||||
super.addAll(
|
file.parentFile?.mkdirs()
|
||||||
Json.decodeFromString(file.bufferedReader().use { it.readText() })
|
|
||||||
|
list.clear()
|
||||||
|
list.addAll(
|
||||||
|
Json.decodeFromString<List<Int>>(file.bufferedReader().use { it.readText() })
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun save() {
|
fun save() {
|
||||||
file.writeText(Json.encodeToString(toList()))
|
synchronized(this) {
|
||||||
|
file.writeText(Json.encodeToString(list))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun add(element: Int): Boolean {
|
override fun add(element: Int): Boolean {
|
||||||
load()
|
load()
|
||||||
|
|
||||||
if (contains(element))
|
return list.add(element).also {
|
||||||
super.remove(element)
|
save()
|
||||||
|
}
|
||||||
super.add(0, element)
|
|
||||||
|
|
||||||
save()
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun addAll(elements: Collection<Int>): Boolean {
|
override fun addAll(elements: Collection<Int>): Boolean {
|
||||||
load()
|
load()
|
||||||
|
|
||||||
for (e in elements) {
|
return list.addAll(elements).also {
|
||||||
if (contains(e))
|
save()
|
||||||
super.remove(e)
|
|
||||||
super.add(0, e)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
save()
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun remove(element: Int): Boolean {
|
override fun remove(element: Int): Boolean {
|
||||||
load()
|
load()
|
||||||
val retval = super.remove(element)
|
|
||||||
save()
|
|
||||||
|
|
||||||
return retval
|
return list.remove(element).also {
|
||||||
|
save()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun clear() {
|
override fun clear() {
|
||||||
super.clear()
|
list.clear()
|
||||||
save()
|
save()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,14 +26,14 @@ package xyz.quaver.pupil
|
|||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
* See [testing documentation](http://d.android.com/tools/testing).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import android.util.SparseArray
|
import kotlinx.serialization.json.Json
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
class ExampleUnitTest {
|
class ExampleUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun test() {
|
fun test() {
|
||||||
val arr = SparseArray<Float>()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user