[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
1 changed files with 19 additions and 15 deletions

View File

@ -1139,21 +1139,8 @@ interface LoadResponse {
fun getDurationFromString(input: String?): Int? {
val cleanInput = input?.trim()?.replace(" ", "") ?: return null
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) {
return values[1].toIntOrNull()
}
}
Regex("(\\s\\d+\\shr)|(\\s\\d+\\shour)|(\\s\\d+\\smin)|(\\s\\d+\\ssec)").findAll(input).let { values ->
//Use first as sometimes the text passes on the 2 other Regex, but failed to provide accurate return value
Regex("(\\d+\\shr)|(\\d+\\shour)|(\\d+\\smin)|(\\d+\\ssec)").findAll(input).let { values ->
var seconds = 0
values.forEach {
val time_text = it.value
@ -1174,6 +1161,23 @@ fun getDurationFromString(input: String?): Int? {
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
}