Make Circular entity id equals as circular number and save attachments to Circular entity

This commit is contained in:
2019-09-13 18:13:15 +02:00
committed by Matte23
parent d87210e1ca
commit b4d8fb3c77
6 changed files with 89 additions and 5 deletions

View File

@@ -1,3 +1,21 @@
/*
* Circolapp
* Copyright (C) 2019 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.adapters
import android.view.LayoutInflater

View File

@@ -22,8 +22,10 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
@Database(entities = [Circular::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun circularDao(): CircularDao

View File

@@ -23,7 +23,17 @@ import androidx.room.PrimaryKey
@Entity(tableName = "circulars")
data class Circular(
@PrimaryKey(autoGenerate = true) val id: Long?,
@PrimaryKey val id: Long,
val name: String,
val url: String
)
val url: String,
val attachmentsNames: MutableList<String> = mutableListOf(),
val attachmentsUrls: MutableList<String> = mutableListOf()
) {
companion object {
fun generateFromString(string: String, url: String): Circular {
val id = string.split(" ")[1]
return Circular(id.toLong(), string, url)
}
}
}

View File

@@ -25,7 +25,7 @@ import androidx.room.Query
@Dao
interface CircularDao {
@Query("SELECT * FROM circulars")
@Query("SELECT * FROM circulars ORDER BY id DESC")
fun getCirculars(): List<Circular>
@Insert(onConflict = OnConflictStrategy.REPLACE)

View File

@@ -0,0 +1,49 @@
/*
* Circolapp
* Copyright (C) 2019 Matteo Schiff
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.underdesk.circolapp.data
import androidx.room.TypeConverter
class Converters {
@TypeConverter
fun stringToList(data: String?): List<String> {
val list: MutableList<String> = mutableListOf()
if (data != null) {
for (attachment in data.split(",")) {
list.add(attachment)
}
}
return list.dropLast(1)
}
@TypeConverter
fun listToString(list: List<String>): String {
var string = ""
for (attachment in list) {
string += "$attachment,"
}
return string
}
}

View File

@@ -43,7 +43,12 @@ class DataFetcher {
val list = ArrayList<Circular>()
htmlList.forEach { element ->
list.add(Circular(null, element.text(), element.attr("href")))
if (element.text().startsWith("All")) {
list.last().attachmentsNames.add(element.text())
list.last().attachmentsUrls.add(element.attr("href"))
} else {
list.add(Circular.generateFromString(element.text(), element.attr("href")))
}
}
return list