@@ -314,19 +314,23 @@ internal fun getPrevIndexWithStep(
314314private inline fun TimerEntity.forEachStep (
315315 block : (index: TimerIndex , group: StepEntity .Group ? , step: StepEntity .Step ) -> Unit ,
316316) {
317- var index = getFirstIndex()
318- val lastIndex = getLastIndex()
317+ var index = getFirstIndexOrNull() ? : return
318+ val lastIndex = getLastIndexOrNull() ? : return
319319 var step = getStep(index)
320320 while (step != null ) {
321321 block(index, getGroup(index), step)
322322
323323 if (index == lastIndex) break
324324
325- index = getNextIndexWithStep(
326- steps = steps,
327- totalLoop = loop,
328- currentIndex = index,
329- ).first
325+ index = if (steps.isNotEmpty()) {
326+ getNextIndexWithStep(
327+ steps = steps,
328+ totalLoop = loop,
329+ currentIndex = index,
330+ ).first
331+ } else {
332+ lastIndex
333+ }
330334 step = getStep(index)
331335 }
332336}
@@ -356,8 +360,9 @@ fun TimerEntity.getGroup(index: TimerIndex): StepEntity.Group? {
356360 return steps.getOrNull(index.stepIndex) as ? StepEntity .Group
357361}
358362
359- fun TimerEntity.getFirstIndex (): TimerIndex {
363+ fun TimerEntity.getFirstIndexOrNull (): TimerIndex ? {
360364 if (startStep != null ) return TimerIndex .Start
365+ if (steps.isEmpty()) return if (endStep != null ) TimerIndex .End else null
361366 return when (steps[0 ]) {
362367 is StepEntity .Step -> TimerIndex .Step (loopIndex = 0 , stepIndex = 0 )
363368 is StepEntity .Group -> TimerIndex .Group (
@@ -368,8 +373,13 @@ fun TimerEntity.getFirstIndex(): TimerIndex {
368373 }
369374}
370375
371- fun TimerEntity.getLastIndex (): TimerIndex {
376+ fun TimerEntity.getFirstIndex (): TimerIndex {
377+ return checkNotNull(getFirstIndexOrNull()) { " No first index $this " }
378+ }
379+
380+ fun TimerEntity.getLastIndexOrNull (): TimerIndex ? {
372381 if (endStep != null ) return TimerIndex .End
382+ if (steps.isEmpty()) return if (startStep != null ) TimerIndex .Start else null
373383 val lastIndex = steps.size - 1
374384 return when (val last = steps[lastIndex]) {
375385 is StepEntity .Step -> TimerIndex .Step (loopIndex = loop - 1 , stepIndex = lastIndex)
@@ -386,6 +396,10 @@ fun TimerEntity.getLastIndex(): TimerIndex {
386396 }
387397}
388398
399+ fun TimerEntity.getLastIndex (): TimerIndex {
400+ return checkNotNull(getLastIndexOrNull()) { " No last index $this " }
401+ }
402+
389403fun TimerEntity.getTimerLoop (index : TimerIndex ): Int = when (index) {
390404 is TimerIndex .Start -> 0
391405 is TimerIndex .Step -> index.loopIndex
0 commit comments