Browse Source

Add range check

cooperku_kdanmobile 5 years ago
parent
commit
5d623a141e

+ 7 - 2
src/main/java/com/kdanmobile/reader/additionalpage/AdditionalPageConverter.kt

@@ -3,6 +3,7 @@ package com.kdanmobile.reader.additionalpage
 import androidx.annotation.IntRange
 import com.kdanmobile.kmpdfkit.globaldata.Config
 import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
+import kotlin.math.max
 
 /**
  * 額外頁面頁碼轉換器
@@ -15,9 +16,9 @@ import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
  */
 class AdditionalPageConverter(
         //  原始文件每隔幾頁是「額外頁面」
-        @IntRange(from = 1) private val additionalPageInterval: Int = DEFAULT_ADDITIONAL_PAGE_INTERVAL,
+        @IntRange(from = 1) additionalPageInterval: Int = DEFAULT_ADDITIONAL_PAGE_INTERVAL,
         //  第一個額外頁面至少是從原始文件第幾頁開始
-        @IntRange(from = 1) private val firstAdditionalPageIndex: Int = DEFAULT_FIRST_ADDITIONAL_PAGE_INDEX
+        @IntRange(from = 1) firstAdditionalPageIndex: Int = DEFAULT_FIRST_ADDITIONAL_PAGE_INDEX
 ) {
 
     companion object {
@@ -27,6 +28,10 @@ class AdditionalPageConverter(
         const val DEFAULT_FIRST_ADDITIONAL_PAGE_INDEX = 1
     }
 
+    private val additionalPageInterval: Int = max(DEFAULT_ADDITIONAL_PAGE_INTERVAL, additionalPageInterval)
+
+    private val firstAdditionalPageIndex: Int = max(DEFAULT_FIRST_ADDITIONAL_PAGE_INDEX, firstAdditionalPageIndex)
+
     var kmpdfDocumentController: KMPDFDocumentController? = null
 
     /**

+ 7 - 2
src/main/java/com/kdanmobile/reader/additionalpage/AdditionalPageDisplayStrategy.kt

@@ -3,6 +3,7 @@ package com.kdanmobile.reader.additionalpage
 import android.util.SparseBooleanArray
 import androidx.annotation.IntRange
 import kotlin.math.abs
+import kotlin.math.max
 
 /**
  * 額外頁面顯示策略
@@ -11,9 +12,9 @@ import kotlin.math.abs
 class AdditionalPageDisplayStrategy(
         var displayStrategyType: AdditionalPageDisplayStrategyType = AdditionalPageDisplayStrategyType.PAGE_INTERVAL,
         //  至少每隔幾頁(包含額外頁面)才可以顯示廣告
-        @IntRange(from = 4) private val pageInterval: Int = DEFAULT_PAGE_INTERVAL,
+        @IntRange(from = 4) pageInterval: Int = DEFAULT_PAGE_INTERVAL,
         //  至少每隔幾秒才可以顯示廣告
-        @IntRange(from = 5) private val timeInterval: Int = DEFAULT_TIME_INTERVAL
+        @IntRange(from = 5) timeInterval: Int = DEFAULT_TIME_INTERVAL
 ) {
 
     companion object {
@@ -23,6 +24,10 @@ class AdditionalPageDisplayStrategy(
         const val DEFAULT_TIME_INTERVAL = 5
     }
 
+    private val pageInterval: Int = max(DEFAULT_PAGE_INTERVAL, pageInterval)
+
+    private val timeInterval: Int = max(DEFAULT_TIME_INTERVAL, timeInterval)
+
     /**
      * 額外頁面最後顯示時間,與「TIME_INTERVAL」相關的顯示策略會使用到此變數
      */

+ 4 - 1
src/main/java/com/kdanmobile/reader/additionalpage/AdditionalPageManager.kt

@@ -3,6 +3,7 @@ package com.kdanmobile.reader.additionalpage
 import android.util.SparseBooleanArray
 import androidx.annotation.IntRange
 import kotlin.math.abs
+import kotlin.math.max
 
 /**
  * 額外頁面管理員
@@ -14,7 +15,7 @@ import kotlin.math.abs
 class AdditionalPageManager(
         val pageConverter: AdditionalPageConverter = AdditionalPageConverter(),
         val displayStrategy: AdditionalPageDisplayStrategy = AdditionalPageDisplayStrategy(),
-        @IntRange(from = 3) private val nextInterval: Int = DEFAULT_NEXT_INTERVAL
+        @IntRange(from = 3) nextInterval: Int = DEFAULT_NEXT_INTERVAL
 ) {
 
     companion object {
@@ -22,6 +23,8 @@ class AdditionalPageManager(
         const val DEFAULT_NEXT_INTERVAL = 3
     }
 
+    private val nextInterval: Int = max(DEFAULT_NEXT_INTERVAL, nextInterval)
+
     //  額外頁面顯示策略類型
     var displayStrategyType: AdditionalPageDisplayStrategyType
         get() {