Add support for Prever

This commit is contained in:
Matte23
2021-02-15 19:52:34 +01:00
parent 0a414b0dbc
commit 24d51aaeec
12 changed files with 244 additions and 15 deletions

View File

@@ -44,6 +44,7 @@ import net.underdesk.circolapp.data.AndroidCircularRepository
import net.underdesk.circolapp.data.AndroidDatabase
import net.underdesk.circolapp.databinding.ItemCircularBinding
import net.underdesk.circolapp.fragments.NewReminderFragment
import net.underdesk.circolapp.server.AndroidServerApi
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.shared.data.CircularRepository
import net.underdesk.circolapp.utils.DownloadableFile
@@ -57,6 +58,7 @@ class CircularLetterAdapter(
RecyclerView.Adapter<CircularLetterAdapter.CircularLetterViewHolder>() {
private lateinit var context: Context
private lateinit var circularRepository: CircularRepository
private var idsAreHumanReadable = true
private val adapterCallback: AdapterCallback = mainActivity
private var collapsedItems = -1
@@ -90,14 +92,22 @@ class CircularLetterAdapter(
val binding = ItemCircularBinding.inflate(LayoutInflater.from(parent.context), parent, false)
context = parent.context
circularRepository = AndroidCircularRepository.getInstance(context)
idsAreHumanReadable = AndroidServerApi.getInstance(context).idsAreHumanReadable()
return CircularLetterViewHolder(binding)
}
override fun onBindViewHolder(holder: CircularLetterViewHolder, position: Int) {
holder.number.text = context.getString(R.string.notification_title, circulars[position].id)
holder.title.text = circulars[position].name
if (idsAreHumanReadable) {
holder.number.text =
context.getString(R.string.notification_title_id, circulars[position].id)
holder.date.text = circulars[position].date
} else {
holder.number.text =
context.getString(R.string.notification_title_date, circulars[position].date)
holder.date.text = ""
}
holder.title.text = circulars[position].name
val observer = Observer<Boolean> {
if (it) {

View File

@@ -30,19 +30,21 @@ import androidx.core.app.NotificationManagerCompat
import androidx.core.app.TaskStackBuilder
import net.underdesk.circolapp.MainActivity
import net.underdesk.circolapp.R
import net.underdesk.circolapp.server.AndroidServerApi
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.works.PollWork
object NotificationsUtils {
fun createNotificationsForCirculars(circulars: List<Circular>, context: Context) {
createNotificationChannel(context)
val idsAreHumanReadable = AndroidServerApi.getInstance(context).idsAreHumanReadable()
val summaryStyle = NotificationCompat.InboxStyle()
.setBigContentTitle(context.getString(R.string.notification_summary_title))
.setSummaryText(context.getString(R.string.notification_summary))
for (circular in circulars) {
createNotification(circular, context)
createNotification(circular, context, idsAreHumanReadable)
summaryStyle.addLine(circular.name)
}
@@ -67,7 +69,11 @@ object NotificationsUtils {
}
}
private fun createNotification(circular: Circular, context: Context) {
private fun createNotification(
circular: Circular,
context: Context,
idsAreHumanReadable: Boolean
) {
val mainIntent = Intent(context, MainActivity::class.java)
val viewIntent = Intent(Intent.ACTION_VIEW)
@@ -88,7 +94,6 @@ object NotificationsUtils {
val builder = NotificationCompat.Builder(context, PollWork.CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(context.getString(R.string.notification_title, circular.id))
.setContentText(circular.name)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
@@ -99,6 +104,17 @@ object NotificationsUtils {
.bigText(circular.name)
)
if (idsAreHumanReadable) {
builder.setContentTitle(context.getString(R.string.notification_title_id, circular.id))
} else {
builder.setContentTitle(
context.getString(
R.string.notification_title_date,
circular.date
)
)
}
with(NotificationManagerCompat.from(context)) {
notify(circular.id.toInt(), builder.build())
}

View File

@@ -22,7 +22,8 @@
<string name="preferences_polling_summary">Sconsigliato. Disabilitare questa opzione per utilizzare le notifiche push</string>
<string name="preferences_poll_interval_summary">Quanto tempo attendere tra una sincronizzazione ed un altra</string>
<string name="notification_title">Circolare numero %1$d</string>
<string name="notification_title_id">Circolare numero %1$d</string>
<string name="notification_title_date">Circolare pubblicata il %1$s</string>
<string name="notification_summary_title">Nuove circolari pubblicate</string>
<string name="notification_summary">Nuove circolari</string>
<plurals name="notification_summary_text">

View File

@@ -22,7 +22,8 @@
<string name="preferences_polling_summary">Not recommended. Disable this option to use push notifications</string>
<string name="preferences_poll_interval_summary">How long to wait before checking if new circulars are published</string>
<string name="notification_title">Circular letter number %1$d</string>
<string name="notification_title_id">Circular letter number %1$d</string>
<string name="notification_title_date">Circular published on %1$s</string>
<string name="notification_summary_title">New circulars published</string>
<string name="notification_summary">New circulars</string>
<plurals name="notification_summary_text">

View File

@@ -20,6 +20,7 @@ object Dependencies {
object Kotlin {
const val version = "1.4.21"
const val core = "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${version}"
const val serializationJson = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"
const val coroutinesCore = "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2-native-mt"
const val coroutinesAndroid =
"org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.2-native-mt"
@@ -65,10 +66,6 @@ object Dependencies {
const val slf4j = "org.slf4j:slf4j-simple:1.7.30"
}
object Serialization {
const val json = "org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.1"
}
object SQLDelight {
const val version = "1.4.4"
const val sqlDelightRuntime = "com.squareup.sqldelight:runtime:$version"

View File

@@ -44,20 +44,19 @@ kotlin {
sourceSets {
val commonMain by getting {
dependencies {
// KotlinX
implementation(Dependencies.Kotlin.coroutinesCore) {
version {
strictly("1.4.2-native-mt")
}
}
implementation(Dependencies.Kotlin.serializationJson)
// Ktor
implementation(Dependencies.Ktor.ktorCore)
implementation(Dependencies.Ktor.ktorJson)
implementation(Dependencies.Ktor.ktorSerialization)
// Serialization
implementation(Dependencies.Serialization.json)
// SqlDelight
implementation(Dependencies.SQLDelight.sqlDelightRuntime)
implementation(Dependencies.SQLDelight.sqlDelightCoroutines)

View File

@@ -25,6 +25,7 @@ abstract class Server(
val ktorClient: HttpClient
) {
abstract val serverID: Int
open val idsAreHumanReadable = true
abstract suspend fun getCircularsFromServer(): Pair<List<Circular>, ServerAPI.Companion.Result>
abstract suspend fun getRealUrl(rawUrl: String): Pair<String, ServerAPI.Companion.Result>
abstract suspend fun newCircularsAvailable(): Pair<Boolean, ServerAPI.Companion.Result>

View File

@@ -24,12 +24,15 @@ import net.underdesk.circolapp.shared.PlatformDispatcher
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.shared.server.curie.CurieServer
import net.underdesk.circolapp.shared.server.porporato.PorporatoServer
import net.underdesk.circolapp.shared.server.prever.PreverAgrarioServer
import net.underdesk.circolapp.shared.server.prever.PreverAlberghieroServer
class ServerAPI(serverName: Servers) {
private val ktorClient = KtorFactory().createClient()
private var server: Server
fun serverID(): Int = server.serverID
fun idsAreHumanReadable() = server.idsAreHumanReadable
init {
server = createServer(serverName, ktorClient)
@@ -61,7 +64,7 @@ class ServerAPI(serverName: Servers) {
companion object {
enum class Servers {
CURIE, PORPORATO
CURIE, PORPORATO, PREVER_AGRARIO, PREVER_ALBERGHIERO
}
enum class Result {
@@ -85,16 +88,22 @@ class ServerAPI(serverName: Servers) {
fun getServerName(server: Servers) = when (server) {
Servers.CURIE -> "Liceo scientifico Maria Curie"
Servers.PORPORATO -> "Liceo G.F. Porporato"
Servers.PREVER_AGRARIO -> "I.I.S. Arturo Prever - Agrario"
Servers.PREVER_ALBERGHIERO -> "I.I.S. Arturo Prever - Alberghiero"
}
fun getServerWebsite(server: Servers) = when (server) {
Servers.CURIE -> "https://www.curiepinerolo.edu.it/"
Servers.PORPORATO -> "https://www.liceoporporato.edu.it/"
Servers.PREVER_AGRARIO -> "https://www.prever.edu.it/agrario/"
Servers.PREVER_ALBERGHIERO -> "https://www.prever.edu.it/alberghiero/"
}
fun createServer(server: Servers, ktorClient: HttpClient) = when (server) {
Servers.CURIE -> CurieServer(ktorClient)
Servers.PORPORATO -> PorporatoServer(ktorClient)
Servers.PREVER_AGRARIO -> PreverAgrarioServer(ktorClient)
Servers.PREVER_ALBERGHIERO -> PreverAlberghieroServer(ktorClient)
}
}
}

View File

@@ -0,0 +1,34 @@
/*
* Circolapp
* Copyright (C) 2019-2021 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.shared.server.pojo
import kotlinx.serialization.Serializable
@Serializable
data class Post(
val id: Long,
val date: String,
val link: String,
val title: Title
)
@Serializable
data class Title(
val rendered: String
)

View File

@@ -0,0 +1,107 @@
/*
* Circolapp
* Copyright (C) 2019-2021 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.shared.server.prever
import io.ktor.client.*
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.utils.io.errors.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.underdesk.circolapp.shared.data.Circular
import net.underdesk.circolapp.shared.server.Server
import net.underdesk.circolapp.shared.server.ServerAPI
import net.underdesk.circolapp.shared.server.pojo.Post
import kotlin.coroutines.cancellation.CancellationException
abstract class GenericPreverServer(ktorClient: HttpClient) : Server(ktorClient) {
abstract val categoryId: Int
override val idsAreHumanReadable = false
override suspend fun getCircularsFromServer()
: Pair<List<Circular>, ServerAPI.Companion.Result> {
return try {
val list = arrayListOf<Circular>()
var page = 1
var newCircularsInPage: List<Circular>
do {
newCircularsInPage = parsePage(page)
list.addAll(newCircularsInPage)
page++
} while (newCircularsInPage.size >= 99)
list.sortByDescending { it.id }
Pair(list, ServerAPI.Companion.Result.SUCCESS)
} catch (exception: IOException) {
Pair(emptyList(), ServerAPI.Companion.Result.NETWORK_ERROR)
} catch (exception: Exception) {
Pair(emptyList(), ServerAPI.Companion.Result.GENERIC_ERROR)
}
}
@OptIn(ExperimentalStdlibApi::class)
@Throws(IOException::class, CancellationException::class)
private suspend fun parsePage(page: Int): List<Circular> {
val posts = retrievePageFromServer(page)
return withContext(Dispatchers.Default) {
val list = arrayListOf<Circular>()
for (post in posts) {
list.add(generateFromString(post.id, post.title.rendered, post.date, post.link))
}
list
}
}
override suspend fun getRealUrl(rawUrl: String): Pair<String, ServerAPI.Companion.Result> {
return Pair(rawUrl, ServerAPI.Companion.Result.SUCCESS)
}
override suspend fun newCircularsAvailable(): Pair<Boolean, ServerAPI.Companion.Result> {
return Pair(true, ServerAPI.Companion.Result.SUCCESS)
}
@OptIn(ExperimentalStdlibApi::class)
@Throws(IOException::class, CancellationException::class)
private suspend fun retrievePageFromServer(page: Int): List<Post> {
return try {
ktorClient.get(getEndpointUrl(page))
} catch (ex: ClientRequestException) {
emptyList()
}
}
private fun generateFromString(id: Long, string: String, date: String, url: String): Circular {
val title = string.replace("&#8211;", "-")
val dateList = date.split("T")[0].split("-")
val realDate = dateList[2] + "/" + dateList[1] + "/" + dateList[0]
return Circular(id, serverID, title, url, url, realDate)
}
private fun getEndpointUrl(page: Int) =
"https://www.prever.edu.it/wp-json/wp/v2/posts?_fields=id,date,link,title&categories=${categoryId}&per_page=100&page=${page}&after=2020-09-01T00:00:00Z"
}

View File

@@ -0,0 +1,27 @@
/*
* Circolapp
* Copyright (C) 2019-2021 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.shared.server.prever
import io.ktor.client.*
import net.underdesk.circolapp.shared.server.ServerAPI
class PreverAgrarioServer(ktorClient: HttpClient) : GenericPreverServer(ktorClient) {
override val categoryId = 476
override val serverID = ServerAPI.getServerId(ServerAPI.Companion.Servers.PREVER_AGRARIO)
}

View File

@@ -0,0 +1,27 @@
/*
* Circolapp
* Copyright (C) 2019-2021 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.shared.server.prever
import io.ktor.client.*
import net.underdesk.circolapp.shared.server.ServerAPI
class PreverAlberghieroServer(ktorClient: HttpClient) : GenericPreverServer(ktorClient) {
override val categoryId = 94
override val serverID = ServerAPI.getServerId(ServerAPI.Companion.Servers.PREVER_ALBERGHIERO)
}