[Bugfix] Re arrange Regex for Duration, fixing issue if inaccurate results. (#239)

This commit is contained in:
Jace 2022-12-08 02:39:59 +08:00 committed by GitHub
parent 6d13cf0b01
commit 56c79e3b6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1139,21 +1139,8 @@ interface LoadResponse {
fun getDurationFromString(input: String?): Int? { fun getDurationFromString(input: String?): Int? {
val cleanInput = input?.trim()?.replace(" ", "") ?: return null val cleanInput = input?.trim()?.replace(" ", "") ?: return null
Regex("([0-9]*)h.*?([0-9]*)m").find(cleanInput)?.groupValues?.let { values -> //Use first as sometimes the text passes on the 2 other Regex, but failed to provide accurate return value
if (values.size == 3) { Regex("(\\d+\\shr)|(\\d+\\shour)|(\\d+\\smin)|(\\d+\\ssec)").findAll(input).let { values ->
val hours = values[1].toIntOrNull()
val minutes = values[2].toIntOrNull()
if (minutes != null && hours != null) {
return hours * 60 + minutes
}
}
}
Regex("([0-9]*)m").find(cleanInput)?.groupValues?.let { values ->
if (values.size == 2) {
return values[1].toIntOrNull()
}
}
Regex("(\\s\\d+\\shr)|(\\s\\d+\\shour)|(\\s\\d+\\smin)|(\\s\\d+\\ssec)").findAll(input).let { values ->
var seconds = 0 var seconds = 0
values.forEach { values.forEach {
val time_text = it.value val time_text = it.value
@ -1174,6 +1161,23 @@ fun getDurationFromString(input: String?): Int? {
return seconds / 60 return seconds / 60
} }
} }
Regex("([0-9]*)h.*?([0-9]*)m").find(cleanInput)?.groupValues?.let { values ->
if (values.size == 3) {
val hours = values[1].toIntOrNull()
val minutes = values[2].toIntOrNull()
if (minutes != null && hours != null) {
return hours * 60 + minutes
}
}
}
Regex("([0-9]*)m").find(cleanInput)?.groupValues?.let { values ->
if (values.size == 2) {
val return_value = values[1].toIntOrNull()
if (return_value != null) {
return return_value
}
}
}
return null return null
} }