[App] Share Lesson and Event notes to specific team only.

This commit is contained in:
kuba2k2 2022-10-26 11:14:32 +02:00
parent 742bd03e9e
commit 9fde97bef0
No known key found for this signature in database
GPG Key ID: 7919598F25F38819
8 changed files with 35 additions and 8 deletions

View File

@ -324,11 +324,14 @@ class SzkolnyApi(val app: App) : CoroutineScope {
} }
@Throws(Exception::class) @Throws(Exception::class)
fun shareNote(note: Note) { fun shareNote(note: Note, teamId: Long? = null) {
val profile = app.db.profileDao().getByIdNow(note.profileId) val profile = app.db.profileDao().getByIdNow(note.profileId)
?: throw NullPointerException("Profile is not found") ?: throw NullPointerException("Profile is not found")
val team = app.db.teamDao().getClassNow(note.profileId) val team = if (teamId == null)
?: throw NullPointerException("TeamClass is not found") 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( val response = api.shareNote(NoteShareRequest(
deviceId = app.deviceId, deviceId = app.deviceId,

View File

@ -9,6 +9,7 @@ interface Noteable {
fun getNoteType(): Note.OwnerType fun getNoteType(): Note.OwnerType
fun getNoteOwnerProfileId(): Int fun getNoteOwnerProfileId(): Int
fun getNoteOwnerId(): Long fun getNoteOwnerId(): Long
fun getNoteShareTeamId(): Long? = null
var notes: MutableList<Note> var notes: MutableList<Note>

View File

@ -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.Metadata
import pl.szczodrzynski.edziennik.data.db.entity.Note import pl.szczodrzynski.edziennik.data.db.entity.Note
import pl.szczodrzynski.edziennik.data.db.entity.Noteable 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.ui.search.Searchable
import pl.szczodrzynski.edziennik.utils.html.BetterHtml import pl.szczodrzynski.edziennik.utils.html.BetterHtml
import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Date
@ -118,4 +119,5 @@ class EventFull(
override fun getNoteType() = Note.OwnerType.EVENT override fun getNoteType() = Note.OwnerType.EVENT
override fun getNoteOwnerProfileId() = profileId override fun getNoteOwnerProfileId() = profileId
override fun getNoteOwnerId() = id override fun getNoteOwnerId() = id
override fun getNoteShareTeamId() = teamId.takePositive()
} }

View File

@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.R
import pl.szczodrzynski.edziennik.data.db.entity.Lesson import pl.szczodrzynski.edziennik.data.db.entity.Lesson
import pl.szczodrzynski.edziennik.data.db.entity.Note import pl.szczodrzynski.edziennik.data.db.entity.Note
import pl.szczodrzynski.edziennik.data.db.entity.Noteable import pl.szczodrzynski.edziennik.data.db.entity.Noteable
import pl.szczodrzynski.edziennik.ext.takePositive
import pl.szczodrzynski.edziennik.utils.models.Time import pl.szczodrzynski.edziennik.utils.models.Time
class LessonFull( class LessonFull(
@ -142,4 +143,5 @@ class LessonFull(
override fun getNoteType() = Note.OwnerType.LESSON override fun getNoteType() = Note.OwnerType.LESSON
override fun getNoteOwnerProfileId() = profileId override fun getNoteOwnerProfileId() = profileId
override fun getNoteOwnerId() = ownerId override fun getNoteOwnerId() = ownerId
override fun getNoteShareTeamId() = teamId.takePositive()
} }

View File

@ -76,6 +76,9 @@ fun pendingIntentFlag(): Int {
fun Int?.takeValue() = if (this == -1) null else this fun Int?.takeValue() = if (this == -1) null else this
fun Int?.takePositive() = if (this == -1 || this == 0) 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 String?.takeValue() = if (this.isNullOrBlank()) null else this
fun Any?.ignore() = Unit fun Any?.ignore() = Unit

View File

@ -87,7 +87,12 @@ class NoteEditorDialog(
.show() .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() progressDialog?.dismiss()
return success return success
} }

View File

@ -93,10 +93,15 @@ class NoteManager(private val app: App) {
return getOwner(note) != null 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 { val success = when {
!note.isShared && wasShared -> unshareNote(activity, note) !note.isShared && wasShared -> unshareNote(activity, note)
note.isShared -> shareNote(activity, note) note.isShared -> shareNote(activity, note, teamId)
else -> true else -> true
} }
@ -124,9 +129,9 @@ class NoteManager(private val app: App) {
return true 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) { return app.api.runCatching(activity) {
shareNote(note) shareNote(note, teamId)
} != null } != null
} }

View File

@ -426,4 +426,10 @@ public class Date implements Comparable<Date>, Noteable {
public boolean hasReplacingNotes() { public boolean hasReplacingNotes() {
return Noteable.DefaultImpls.hasReplacingNotes(this); return Noteable.DefaultImpls.hasReplacingNotes(this);
} }
@Nullable
@Override
public Long getNoteShareTeamId() {
return Noteable.DefaultImpls.getNoteShareTeamId(this);
}
} }