Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions app/src/main/java/com/omega_r/base/simple/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,37 @@ class MainActivity : OmegaActivity(), MainView {
@InjectPresenter
override lateinit var presenter: MainPresenter

private val images = listOf(
Image.from("https://images.wallpaperscraft.ru/image/gora_vershina_pik_146078_3840x2400.jpg"),
Image.from("https://hubblesite.org/uploads/image_file/image_attachment/31803/STSCI-H-p1935b-m-2000x1827.png"),
Image.from("https://hubblesite.org/uploads/image_file/image_attachment/31726/STSCI-H-p1918a-f-2000x2000.png"),
Image.from("https://images.wallpaperscraft.ru/image/basketbolnoe_koltso_shchitok_koltso_146103_3840x2160.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/kot_okno_vzgliad_146100_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/reka_obryv_skaly_146093_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/ozero_bereg_kamni_146091_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/gory_skaly_zasnezhennyj_146085_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/piatna_kraska_rzhavchina_146084_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/zdanie_arhitektura_minimalizm_146082_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/gora_vershina_pik_146078_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/setchatyj_struktura_relef_146075_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/zontiki_raznotsvetnyj_dekoratsiia_146072_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/tsvety_fioletovyj_buket_146070_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/okno_steklo_mokryj_146068_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/limon_dolki_pattern_146063_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/siluet_temnyj_zakat_146060_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/tsitata_chtenie_um_146059_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/devushka_siluet_solntse_146058_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/zdanie_arhitektura_sovremennyj_146056_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/mercedes_mashina_chernyj_146054_3840x2400.jpg"),
Image.from("https://images.wallpaperscraft.ru/image/kamen_skala_sneg_146052_3840x2400.jpg"),
Image.from("https://hubblesite.org/uploads/image_file/image_attachment/31803/STSCI-H-p1935b-m-2000x1827.png")
)

private val adapter = OmegaAutoAdapter.create(R.layout.item_test_3, ::onClickItem) {
bindImage(R.id.imageview)
}.apply {
watcher = OmegaListAdapter.ImagePreloadWatcher(this)
list = listOf(
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?156"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?256"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?35"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?45"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?56"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?64"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?79"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?85"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?91"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?102"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?113"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?124"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?135"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?146"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?1578"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?169"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?174"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?185"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?1956"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?201"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?212"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?2212"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?231"),
Image.from("https://i.pinimg.com/originals/d6/68/ab/d668abc72809303852c27275e6a56775.gif?242")
)
list = images
}

private val recyclerView: RecyclerView by bind(R.id.recyclerview, adapter) {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ ext {
}// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.31'
ext.kotlin_version = '1.3.41'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed May 08 11:14:06 MSK 2019
#Wed Aug 21 10:45:33 MSK 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
49 changes: 39 additions & 10 deletions lib/src/main/java/com/omega_r/base/adapters/OmegaListAdapter.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.omega_r.base.adapters

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.omega_r.libs.omegatypes.Image
import kotlin.math.max
import kotlin.math.min

/**
* Created by Anton Knyazev on 04.04.2019.
Expand Down Expand Up @@ -52,22 +57,46 @@ abstract class OmegaListAdapter<M, VH> : OmegaAdapter<VH>(), ListableAdapter<M>

}

class ImagePreloadWatcher<M : Image>(private val adapter: OmegaListAdapter<M, *>) : Watcher {
class ImagePreloadWatcher<M : Image>(
private val adapter: OmegaListAdapter<M, *>,
private val maxPreloadCount: Int = 4
) : Watcher {

private var lastPosition: Int = -1
private var lastBindPosition = -1

override fun bindPosition(position: Int, recyclerView: RecyclerView) {
val childCount = recyclerView.childCount
val preloadPosition = if (lastPosition < position) {
position + childCount
} else {
position - childCount
val context = recyclerView.context
val from: Int
val to: Int
if (position > lastBindPosition) {
from = position + 1
to = from + maxPreloadCount - 1
preload(from, to, context)
} else if (position < lastBindPosition) {
from = position - 1
to = from - maxPreloadCount + 1
preload(from, to, context)
}
adapter.list.getOrNull(preloadPosition)?.preload(recyclerView.context)

lastPosition = position
lastBindPosition = position
}

private fun preload(from: Int, to: Int, context: Context) {
val size = adapter.list.size
val start = max(0, min(from, size - 1))
val end = max(0, min(to, size - 1))

if (from < to) {
// Increasing
for (i in start until end) {
adapter.list.getOrNull(i)?.preload(context)
}
} else {
// Decreasing
for (i in end downTo start) {
adapter.list.getOrNull(i)?.preload(context)
}
}
}
}

}