forked from github/szkolny
[API/Events] Update Event Manual dialog sharing.
This commit is contained in:
parent
2cf204ff79
commit
f242c30476
@ -641,3 +641,8 @@ fun Drawable.setTintColor(color: Int): Drawable {
|
||||
)
|
||||
return this
|
||||
}
|
||||
|
||||
inline fun <T> List<T>.ifNotEmpty(block: (List<T>) -> Unit) {
|
||||
if (!isEmpty())
|
||||
block(this)
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ class Szkolny(val app: App, val callback: EdziennikCallback) {
|
||||
completed()
|
||||
}
|
||||
|
||||
fun shareEvent(event: EventFull) {
|
||||
/*fun shareEvent(event: EventFull) {
|
||||
api.shareEvent(event)
|
||||
completed()
|
||||
}
|
||||
@ -44,7 +44,7 @@ class Szkolny(val app: App, val callback: EdziennikCallback) {
|
||||
fun unshareEvent(event: EventFull) {
|
||||
api.unshareEvent(event)
|
||||
completed()
|
||||
}
|
||||
}*/
|
||||
|
||||
private fun completed() {
|
||||
callback.onCompleted()
|
||||
|
@ -15,7 +15,9 @@ import pl.szczodrzynski.edziennik.data.api.szkolny.interceptor.SignatureIntercep
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.request.EventShareRequest
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.request.ServerSyncRequest
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.request.WebPushRequest
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.response.ApiResponse
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.response.WebPushResponse
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.profiles.ProfileFull
|
||||
import pl.szczodrzynski.edziennik.utils.models.Date
|
||||
@ -102,29 +104,33 @@ class SzkolnyApi(val app: App) {
|
||||
return events
|
||||
}
|
||||
|
||||
fun shareEvent(event: EventFull) {
|
||||
fun shareEvent(event: EventFull): ApiResponse<Nothing>? {
|
||||
val team = app.db.teamDao().getByIdNow(event.profileId, event.teamId)
|
||||
|
||||
api.shareEvent(EventShareRequest(
|
||||
return api.shareEvent(EventShareRequest(
|
||||
deviceId = app.deviceId,
|
||||
sharedByName = event.sharedByName,
|
||||
shareTeamCode = team.code,
|
||||
event = event
|
||||
)).execute()
|
||||
)).execute().body()
|
||||
}
|
||||
|
||||
fun unshareEvent(event: EventFull) {
|
||||
fun unshareEvent(event: Event): ApiResponse<Nothing>? {
|
||||
val team = app.db.teamDao().getByIdNow(event.profileId, event.teamId)
|
||||
|
||||
api.shareEvent(EventShareRequest(
|
||||
return api.shareEvent(EventShareRequest(
|
||||
deviceId = app.deviceId,
|
||||
sharedByName = event.sharedByName,
|
||||
unshareTeamCode = team.code,
|
||||
eventId = event.id
|
||||
)).execute()
|
||||
)).execute().body()
|
||||
}
|
||||
|
||||
fun pairBrowser(browserId: String?, pairToken: String?): List<WebPushResponse.Browser> {
|
||||
/*fun eventEditRequest(requesterName: String, event: Event): ApiResponse<Nothing>? {
|
||||
|
||||
}*/
|
||||
|
||||
fun pairBrowser(browserId: String?, pairToken: String?, onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> {
|
||||
val response = api.webPush(WebPushRequest(
|
||||
action = "pairBrowser",
|
||||
deviceId = app.deviceId,
|
||||
@ -132,10 +138,15 @@ class SzkolnyApi(val app: App) {
|
||||
pairToken = pairToken
|
||||
)).execute().body()
|
||||
|
||||
response?.errors?.let {
|
||||
onError?.invoke(it)
|
||||
return emptyList()
|
||||
}
|
||||
|
||||
return response?.data?.browsers ?: emptyList()
|
||||
}
|
||||
|
||||
fun listBrowsers(): List<WebPushResponse.Browser> {
|
||||
fun listBrowsers(onError: ((List<ApiResponse.Error>) -> Unit)? = null): List<WebPushResponse.Browser> {
|
||||
val response = api.webPush(WebPushRequest(
|
||||
action = "listBrowsers",
|
||||
deviceId = app.deviceId
|
||||
|
@ -14,6 +14,7 @@ data class EventShareRequest (
|
||||
val sharedByName: String,
|
||||
val shareTeamCode: String? = null,
|
||||
val unshareTeamCode: String? = null,
|
||||
val requesterName: String? = null,
|
||||
|
||||
val eventId: Long? = null,
|
||||
val event: EventFull? = null
|
||||
|
@ -16,8 +16,8 @@ class SzkolnyTask(val request: Any) : IApiTask(-1) {
|
||||
private const val TAG = "SzkolnyTask"
|
||||
|
||||
fun sync(profiles: List<ProfileFull>) = SzkolnyTask(SyncRequest(profiles))
|
||||
fun shareEvent(event: EventFull) = SzkolnyTask(ShareEventRequest(event))
|
||||
fun unshareEvent(event: EventFull) = SzkolnyTask(UnshareEventRequest(event))
|
||||
/*fun shareEvent(event: EventFull) = SzkolnyTask(ShareEventRequest(event))
|
||||
fun unshareEvent(event: EventFull) = SzkolnyTask(UnshareEventRequest(event))*/
|
||||
}
|
||||
|
||||
private lateinit var szkolny: Szkolny
|
||||
@ -35,12 +35,12 @@ class SzkolnyTask(val request: Any) : IApiTask(-1) {
|
||||
|
||||
when (request) {
|
||||
is SyncRequest -> szkolny.sync(request.profiles)
|
||||
is ShareEventRequest -> szkolny.shareEvent(request.event)
|
||||
is UnshareEventRequest -> szkolny.unshareEvent(request.event)
|
||||
/*is ShareEventRequest -> szkolny.shareEvent(request.event)
|
||||
is UnshareEventRequest -> szkolny.unshareEvent(request.event)*/
|
||||
}
|
||||
}
|
||||
|
||||
data class SyncRequest(val profiles: List<ProfileFull>)
|
||||
data class ShareEventRequest(val event: EventFull)
|
||||
data class UnshareEventRequest(val event: EventFull)
|
||||
/*data class ShareEventRequest(val event: EventFull)
|
||||
data class UnshareEventRequest(val event: EventFull)*/
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ import androidx.appcompat.app.AppCompatActivity
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import kotlinx.coroutines.*
|
||||
import pl.szczodrzynski.edziennik.*
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
|
||||
import pl.szczodrzynski.edziennik.data.api.task.SzkolnyTask
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
import pl.szczodrzynski.edziennik.databinding.DialogEventDetailsBinding
|
||||
@ -39,6 +40,10 @@ class EventDetailsDialog(
|
||||
|
||||
private lateinit var adapter: EventListAdapter
|
||||
|
||||
private val api by lazy {
|
||||
SzkolnyApi(app)
|
||||
}
|
||||
|
||||
init { run {
|
||||
if (activity.isFinishing)
|
||||
return@run
|
||||
@ -126,19 +131,27 @@ class EventDetailsDialog(
|
||||
}
|
||||
|
||||
private fun removeEvent() {
|
||||
if (eventShared && eventOwn) {
|
||||
Toast.makeText(activity, "Unshare + remove own event", Toast.LENGTH_SHORT).show()
|
||||
launch {
|
||||
if (eventShared && eventOwn) {
|
||||
Toast.makeText(activity, "Unshare + remove own event", Toast.LENGTH_SHORT).show()
|
||||
|
||||
SzkolnyTask.unshareEvent(event).enqueue(activity)
|
||||
finishRemoving()
|
||||
}
|
||||
else if (eventShared && !eventOwn) {
|
||||
Toast.makeText(activity, "Remove + blacklist somebody's event", Toast.LENGTH_SHORT).show()
|
||||
// TODO
|
||||
}
|
||||
else {
|
||||
Toast.makeText(activity, "Remove event", Toast.LENGTH_SHORT).show()
|
||||
finishRemoving()
|
||||
val response = withContext(Dispatchers.Default) {
|
||||
api.unshareEvent(event!!)
|
||||
}
|
||||
|
||||
response?.errors?.ifNotEmpty {
|
||||
Toast.makeText(activity, "Error: "+it[0].reason, Toast.LENGTH_SHORT).show()
|
||||
return@launch
|
||||
}
|
||||
|
||||
finishRemoving()
|
||||
} else if (eventShared && !eventOwn) {
|
||||
Toast.makeText(activity, "Remove + blacklist somebody's event", Toast.LENGTH_SHORT).show()
|
||||
// TODO
|
||||
} else {
|
||||
Toast.makeText(activity, "Remove event", Toast.LENGTH_SHORT).show()
|
||||
finishRemoving()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ import com.jaredrummler.android.colorpicker.ColorPickerDialogListener
|
||||
import kotlinx.coroutines.*
|
||||
import pl.szczodrzynski.edziennik.*
|
||||
import pl.szczodrzynski.edziennik.MainActivity.Companion.DRAWER_ITEM_AGENDA
|
||||
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
|
||||
import pl.szczodrzynski.edziennik.data.api.task.SzkolnyTask
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.Event
|
||||
import pl.szczodrzynski.edziennik.data.db.modules.events.EventFull
|
||||
@ -60,13 +61,17 @@ class EventManualDialog(
|
||||
private val app by lazy { activity.application as App }
|
||||
private lateinit var b: DialogEventManualV2Binding
|
||||
private lateinit var dialog: AlertDialog
|
||||
private var removeEventDialog: AlertDialog? = null
|
||||
private var defaultLoaded = false
|
||||
|
||||
private lateinit var event: Event
|
||||
private var customColor: Int? = null
|
||||
private val editingShared = editingEvent?.sharedBy != null
|
||||
private val editingOwn = editingEvent?.sharedBy == "self"
|
||||
private var removeEventDialog: AlertDialog? = null
|
||||
private var defaultLoaded = false
|
||||
|
||||
private val api by lazy {
|
||||
SzkolnyApi(app)
|
||||
}
|
||||
|
||||
init { run {
|
||||
if (activity.isFinishing)
|
||||
@ -617,7 +622,21 @@ class EventManualDialog(
|
||||
else if (!share && editingShared) {
|
||||
Toast.makeText(activity, "Unshare own event", Toast.LENGTH_SHORT).show()
|
||||
|
||||
SzkolnyTask.unshareEvent(eventObject.withMetadata(metadataObject)).enqueue(activity)
|
||||
eventObject.apply {
|
||||
sharedBy = null
|
||||
sharedByName = profile?.studentNameLong
|
||||
}
|
||||
|
||||
val response = withContext(Dispatchers.Default) {
|
||||
api.unshareEvent(eventObject)
|
||||
}
|
||||
|
||||
response?.errors?.ifNotEmpty {
|
||||
Toast.makeText(activity, "Error: "+it[0].reason, Toast.LENGTH_SHORT).show()
|
||||
return@launch
|
||||
}
|
||||
|
||||
eventObject.sharedByName = null
|
||||
finishAdding(eventObject, metadataObject)
|
||||
}
|
||||
else if (share) {
|
||||
@ -630,7 +649,14 @@ class EventManualDialog(
|
||||
|
||||
metadataObject.addedDate = System.currentTimeMillis()
|
||||
|
||||
SzkolnyTask.shareEvent(eventObject.withMetadata(metadataObject)).enqueue(activity)
|
||||
val response = withContext(Dispatchers.Default) {
|
||||
api.shareEvent(eventObject.withMetadata(metadataObject))
|
||||
}
|
||||
|
||||
response?.errors?.ifNotEmpty {
|
||||
Toast.makeText(activity, "Error: "+it[0].reason, Toast.LENGTH_SHORT).show()
|
||||
return@launch
|
||||
}
|
||||
|
||||
eventObject.sharedBy = "self"
|
||||
finishAdding(eventObject, metadataObject)
|
||||
@ -642,19 +668,27 @@ class EventManualDialog(
|
||||
}
|
||||
|
||||
private fun removeEvent() {
|
||||
if (editingShared && editingOwn) {
|
||||
Toast.makeText(activity, "Unshare + remove own event", Toast.LENGTH_SHORT).show()
|
||||
launch {
|
||||
if (editingShared && editingOwn) {
|
||||
Toast.makeText(activity, "Unshare + remove own event", Toast.LENGTH_SHORT).show()
|
||||
|
||||
editingEvent?.let { SzkolnyTask.unshareEvent(it).enqueue(activity) }
|
||||
finishRemoving()
|
||||
}
|
||||
else if (editingShared && !editingOwn) {
|
||||
Toast.makeText(activity, "Remove + blacklist somebody's event", Toast.LENGTH_SHORT).show()
|
||||
// TODO
|
||||
}
|
||||
else {
|
||||
Toast.makeText(activity, "Remove event", Toast.LENGTH_SHORT).show()
|
||||
finishRemoving()
|
||||
val response = withContext(Dispatchers.Default) {
|
||||
api.unshareEvent(editingEvent!!)
|
||||
}
|
||||
|
||||
response?.errors?.ifNotEmpty {
|
||||
Toast.makeText(activity, "Error: "+it[0].reason, Toast.LENGTH_SHORT).show()
|
||||
return@launch
|
||||
}
|
||||
|
||||
finishRemoving()
|
||||
} else if (editingShared && !editingOwn) {
|
||||
Toast.makeText(activity, "Remove + blacklist somebody's event", Toast.LENGTH_SHORT).show()
|
||||
// TODO
|
||||
} else {
|
||||
Toast.makeText(activity, "Remove event", Toast.LENGTH_SHORT).show()
|
||||
finishRemoving()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user