From 9fde97bef089988eb62bc33026fb81dcd4a9f229 Mon Sep 17 00:00:00 2001 From: kuba2k2 Date: Wed, 26 Oct 2022 11:14:32 +0200 Subject: [PATCH] [App] Share Lesson and Event notes to specific team only. --- .../edziennik/data/api/szkolny/SzkolnyApi.kt | 9 ++++++--- .../edziennik/data/db/entity/Noteable.kt | 1 + .../edziennik/data/db/full/EventFull.kt | 2 ++ .../edziennik/data/db/full/LessonFull.kt | 2 ++ .../szczodrzynski/edziennik/ext/MiscExtensions.kt | 3 +++ .../edziennik/ui/notes/NoteEditorDialog.kt | 7 ++++++- .../edziennik/utils/managers/NoteManager.kt | 13 +++++++++---- .../szczodrzynski/edziennik/utils/models/Date.java | 6 ++++++ 8 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt index 922cb6dd..954d6bb1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/szkolny/SzkolnyApi.kt @@ -324,11 +324,14 @@ class SzkolnyApi(val app: App) : CoroutineScope { } @Throws(Exception::class) - fun shareNote(note: Note) { + fun shareNote(note: Note, teamId: Long? = null) { val profile = app.db.profileDao().getByIdNow(note.profileId) ?: throw NullPointerException("Profile is not found") - val team = app.db.teamDao().getClassNow(note.profileId) - ?: throw NullPointerException("TeamClass is not found") + val team = if (teamId == null) + app.db.teamDao().getClassNow(note.profileId) + else + app.db.teamDao().getByIdNow(note.profileId, teamId) + team ?: throw NullPointerException("TeamClass is not found") val response = api.shareNote(NoteShareRequest( deviceId = app.deviceId, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Noteable.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Noteable.kt index 052596b3..8881c1b4 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Noteable.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Noteable.kt @@ -9,6 +9,7 @@ interface Noteable { fun getNoteType(): Note.OwnerType fun getNoteOwnerProfileId(): Int fun getNoteOwnerId(): Long + fun getNoteShareTeamId(): Long? = null var notes: MutableList diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt index 3eb681f2..332b7ce5 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/EventFull.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.Event import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.entity.Note import pl.szczodrzynski.edziennik.data.db.entity.Noteable +import pl.szczodrzynski.edziennik.ext.takePositive import pl.szczodrzynski.edziennik.ui.search.Searchable import pl.szczodrzynski.edziennik.utils.html.BetterHtml import pl.szczodrzynski.edziennik.utils.models.Date @@ -118,4 +119,5 @@ class EventFull( override fun getNoteType() = Note.OwnerType.EVENT override fun getNoteOwnerProfileId() = profileId override fun getNoteOwnerId() = id + override fun getNoteShareTeamId() = teamId.takePositive() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/LessonFull.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/LessonFull.kt index cb149ce9..b61bba30 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/LessonFull.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/full/LessonFull.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.R import pl.szczodrzynski.edziennik.data.db.entity.Lesson import pl.szczodrzynski.edziennik.data.db.entity.Note import pl.szczodrzynski.edziennik.data.db.entity.Noteable +import pl.szczodrzynski.edziennik.ext.takePositive import pl.szczodrzynski.edziennik.utils.models.Time class LessonFull( @@ -142,4 +143,5 @@ class LessonFull( override fun getNoteType() = Note.OwnerType.LESSON override fun getNoteOwnerProfileId() = profileId override fun getNoteOwnerId() = ownerId + override fun getNoteShareTeamId() = teamId.takePositive() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ext/MiscExtensions.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ext/MiscExtensions.kt index 5ddb5dcb..e53950f9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ext/MiscExtensions.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ext/MiscExtensions.kt @@ -76,6 +76,9 @@ fun pendingIntentFlag(): Int { fun Int?.takeValue() = if (this == -1) null else this fun Int?.takePositive() = if (this == -1 || this == 0) null else this +fun Long?.takeValue() = if (this == -1L) null else this +fun Long?.takePositive() = if (this == -1L || this == 0L) null else this + fun String?.takeValue() = if (this.isNullOrBlank()) null else this fun Any?.ignore() = Unit diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/notes/NoteEditorDialog.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/notes/NoteEditorDialog.kt index df101849..8114a6db 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/notes/NoteEditorDialog.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/notes/NoteEditorDialog.kt @@ -87,7 +87,12 @@ class NoteEditorDialog( .show() } - val success = manager.saveNote(activity, note, wasShared = editingNote?.isShared ?: false) + val success = manager.saveNote( + activity = activity, + note = note, + teamId = owner?.getNoteShareTeamId(), + wasShared = editingNote?.isShared ?: false, + ) progressDialog?.dismiss() return success } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/NoteManager.kt b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/NoteManager.kt index 67ac306b..866555bd 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/NoteManager.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/managers/NoteManager.kt @@ -93,10 +93,15 @@ class NoteManager(private val app: App) { return getOwner(note) != null } - suspend fun saveNote(activity: AppCompatActivity, note: Note, wasShared: Boolean): Boolean { + suspend fun saveNote( + activity: AppCompatActivity, + note: Note, + teamId: Long?, + wasShared: Boolean, + ): Boolean { val success = when { !note.isShared && wasShared -> unshareNote(activity, note) - note.isShared -> shareNote(activity, note) + note.isShared -> shareNote(activity, note, teamId) else -> true } @@ -124,9 +129,9 @@ class NoteManager(private val app: App) { return true } - private suspend fun shareNote(activity: AppCompatActivity, note: Note): Boolean { + private suspend fun shareNote(activity: AppCompatActivity, note: Note, teamId: Long?): Boolean { return app.api.runCatching(activity) { - shareNote(note) + shareNote(note, teamId) } != null } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java index 30dc4cc2..9a07ae2c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/Date.java @@ -426,4 +426,10 @@ public class Date implements Comparable, Noteable { public boolean hasReplacingNotes() { return Noteable.DefaultImpls.hasReplacingNotes(this); } + + @Nullable + @Override + public Long getNoteShareTeamId() { + return Noteable.DefaultImpls.getNoteShareTeamId(this); + } }