denzcoskun / ImageSlideshow

Android image slider.
Apache License 2.0
347 stars 142 forks source link

Slideshow on specific delay each of currentItem #80

Open marinsagovac opened 2 years ago

marinsagovac commented 2 years ago

I managed and change code due of my requirements that each slideshow have "own" time delay, for example:

D/currpagelist: 1
D/th: 1000
D/currpagelist: 2
D/th: 5000

Problem is that delay overrides times and of Thread issues that I am not sure how to control and wait until next delay is finished. I also want to avoid using Thread function.

Here is sample of issue, that two slides overrides due of period member method:

D/currpagelist: 1
D/th: 1000
D/currpagelist: 1
D/th: 1000
D/currpagelist: 1
D/th: 1000
D/currpagelist: 1
D/th: 1000
D/currpagelist: 1
D/th: 1000
D/currpagelist: 2
D/th: 5000

Then I tried like this:


val handler = Handler()
val update = Runnable {
    if (currentPage == imageCount) {
        currentPage = 0
    }
    viewPager!!.setCurrentItem(currentPage++, false)
}

swipeTimer = Timer()
        swipeTimer.schedule(object : java.util.TimerTask() {
            override fun run() {
                if (currentPage == 0) {
                    currentPage++;
                }

                if (currentPage > 0) {

                    if (viewPager!!.currentItem != currentPage) {
                        val vs = currentPage - 1;

                        Log.d("currpagelist", currentPage.toString())
                        Log.d("th", tvperiod[vs].toString());

                        Thread.interrupted()
                        Thread.sleep(tvperiod[vs].toLong())

                        handler.post(update)
                    } else {

                    }
                }
            }
        }, 0, 1000)

How can I make to currentPage to wait a specific delay than go to next currentPage without overrides and using schedule periods? Any other solutions for that?

marinsagovac commented 2 years ago

I ended up successfully using ChronoUnit to keep a valid time of each slide image that should be update.

private fun scheduleTimer(period: Long) {
        val handler = Handler()
        val update = Runnable {
            if (currentPage == imageCount) {
                currentPage = 0
            }
            viewPager!!.setCurrentItem(currentPage++, false)
        }

        var then: LocalDateTime = LocalDateTime.now()

        swipeTimer = Timer()
        swipeTimer.schedule(object : java.util.TimerTask() {
            @RequiresApi(Build.VERSION_CODES.O)
            override fun run() {
                if (currentPage == 0) {
                    currentPage++;
                }

                if (currentPage > 0) {
                    val vs = currentPage - 1;
                    var periodCurrentTv = tvperiod[vs]
                    if (ChronoUnit.SECONDS.between(then, LocalDateTime.now()) >= periodCurrentTv/1000) {
                        Log.d("timer20secs", (periodCurrentTv.toLong()).toString())
                        handler.post(update)
                        then = LocalDateTime.now()
                    };
                }
            }
        }, 0, 100) // period
    }

Result:

/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000
D/timer20secs: 5000
D/timer20secs: 1000

Now, there aren't duplicate updates. If you have some suggestion to improve this other than using Thread you can also comment here.