Parse date and title from circular string

This commit is contained in:
2019-09-16 20:02:14 +02:00
committed by Matte23
parent 4a8298e69f
commit 4c5f7ad8de

View File

@@ -20,12 +20,14 @@ package net.underdesk.circolapp.data
import androidx.room.Entity import androidx.room.Entity
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import java.util.regex.Pattern
@Entity(tableName = "circulars") @Entity(tableName = "circulars")
data class Circular( data class Circular(
@PrimaryKey val id: Long, @PrimaryKey val id: Long,
val name: String, val name: String,
val url: String, val url: String,
val date: String,
val attachmentsNames: MutableList<String> = mutableListOf(), val attachmentsNames: MutableList<String> = mutableListOf(),
val attachmentsUrls: MutableList<String> = mutableListOf() val attachmentsUrls: MutableList<String> = mutableListOf()
) { ) {
@@ -33,7 +35,21 @@ data class Circular(
fun generateFromString(string: String, url: String): Circular { fun generateFromString(string: String, url: String): Circular {
val id = string.split(" ")[1] val id = string.split(" ")[1]
return Circular(id.toLong(), string, url) val dateRegex = """(\d{2}\/\d{2}\/\d{4})"""
val matcherDate = Pattern.compile(dateRegex).matcher(string)
var title = string.removeSuffix("-signed")
return if (matcherDate.find()) {
title = title.removeRange(0, matcherDate.end())
.removePrefix(" ")
.removePrefix("_")
.removePrefix(" ")
Circular(id.toLong(), title, url, matcherDate.group(1) ?: "")
} else {
Circular(id.toLong(), title, url, "")
}
} }
} }
} }