[Messages] Fix Librus attachment downloading. Add option to force (re)download an attachment.

This commit is contained in:
Kuba Szczodrzyński 2020-03-30 19:48:56 +02:00
parent b004ec048e
commit 328c07eaf4
5 changed files with 42 additions and 21 deletions

View File

@ -729,6 +729,13 @@ inline fun <T : View> T.onClick(crossinline onClickListener: (v: T) -> Unit) {
}
}
@Suppress("UNCHECKED_CAST")
inline fun <T : View> T.onLongClick(crossinline onLongClickListener: (v: T) -> Boolean) {
setOnLongClickListener { v: View ->
onLongClickListener(v as T)
}
}
@Suppress("UNCHECKED_CAST")
inline fun <T : CompoundButton> T.onChange(crossinline onChangeListener: (v: T, isChecked: Boolean) -> Unit) {
setOnCheckedChangeListener { buttonView, isChecked ->

View File

@ -54,7 +54,7 @@ class LibrusMessagesGetAttachment(override val data: DataLibrus,
downloadAttachment("${LIBRUS_SANDBOX_URL}CSDownload&singleUseKey=$attachmentKey", method = POST)
}
} else {
downloadAttachment(downloadLink, method = GET)
downloadAttachment("$downloadLink/get", method = GET)
}
}
}

View File

@ -15,6 +15,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ProgressBar
import androidx.appcompat.widget.PopupMenu
import androidx.fragment.app.Fragment
import com.google.android.material.chip.Chip
import com.mikepenz.iconics.IconicsColor
@ -62,7 +63,7 @@ class MessageFragment : Fragment(), CoroutineScope {
private lateinit var activity: MainActivity
private lateinit var b: MessageFragmentBinding
private lateinit var job: Job
private val job: Job = Job()
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.Main
@ -73,16 +74,12 @@ class MessageFragment : Fragment(), CoroutineScope {
activity = (getActivity() as MainActivity?) ?: return null
context ?: return null
app = activity.application as App
context!!.theme.applyStyle(Themes.appTheme, true)
b = MessageFragmentBinding.inflate(inflater)
job = Job()
return b.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// TODO check if app, activity, b can be null
if (app.profile == null || !isAdded)
return
if (!isAdded) return
b.closeButton.setImageDrawable(
IconicsDrawable(activity, CommunityMaterial.Icon2.cmd_window_close)
@ -260,16 +257,35 @@ class MessageFragment : Fragment(), CoroutineScope {
MessagesFragment.pageSelection = min(message.type, 1)
}
private val attachmentOnClick = { v: View ->
if (v.tag is Int) {
downloadAttachment(v.tag as Int)
}
}
private val attachmentOnLongClick = { v: View ->
(v.tag as? Int)?.let { tag ->
val popupMenu = PopupMenu(v.context, v)
popupMenu.menu.add(0, tag, 0, R.string.messages_attachment_download_again)
popupMenu.setOnMenuItemClickListener {
downloadAttachment(it.itemId, forceDownload = true)
true
}
popupMenu.show()
}
true
}
private fun showAttachments() {
if (message.attachmentIds != null) {
val insertPoint = b.attachments
insertPoint.removeAllViews()
val chipLayoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
chipLayoutParams.setMargins(0, Utils.dpToPx(8), 0, Utils.dpToPx(8))
chipLayoutParams.setMargins(0, 8.dp, 0, 0)
val progressLayoutParams = FrameLayout.LayoutParams(Utils.dpToPx(18), Utils.dpToPx(18))
progressLayoutParams.setMargins(Utils.dpToPx(8), 0, Utils.dpToPx(8), 0)
val progressLayoutParams = FrameLayout.LayoutParams(18.dp, 18.dp)
progressLayoutParams.setMargins(8.dp, 0, 8.dp, 0)
progressLayoutParams.gravity = END or CENTER_VERTICAL
// CREATE VIEWS AND AN OBJECT FOR EVERY ATTACHMENT
@ -280,12 +296,13 @@ class MessageFragment : Fragment(), CoroutineScope {
val size = message.attachmentSizes[index]
// create the parent
val attachmentLayout = FrameLayout(b.root.context)
attachmentLayout.setPadding(Utils.dpToPx(16), 0, Utils.dpToPx(16), 0)
attachmentLayout.setPadding(16.dp, 0, 16.dp, 0)
val attachmentChip = Chip(attachmentLayout.context)
//attachmentChip.setChipBackgroundColorResource(ThemeUtils.getChipColorRes());
attachmentChip.layoutParams = chipLayoutParams
attachmentChip.height = Utils.dpToPx(40)
attachmentChip.chipMinHeight = 40.dp.toFloat()
//attachmentChip.height = Utils.dpToPx(40)
// show the file size or not
if (size == -1L)
@ -312,11 +329,8 @@ class MessageFragment : Fragment(), CoroutineScope {
attachmentChip.isCloseIconVisible = false
// set the object's index in the attachmentList as the tag
attachmentChip.tag = index
attachmentChip.setOnClickListener { v ->
if (v.tag is Int) {
downloadAttachment(v.tag as Int)
}
}
attachmentChip.onClick(attachmentOnClick)
attachmentChip.onLongClick(attachmentOnLongClick)
attachmentLayout.addView(attachmentChip)
val attachmentProgress = ProgressBar(attachmentLayout.context)
@ -338,10 +352,10 @@ class MessageFragment : Fragment(), CoroutineScope {
}
}
private fun downloadAttachment(index: Int) {
private fun downloadAttachment(index: Int, forceDownload: Boolean = false) {
val attachment = attachmentList[index]
if (attachment.downloaded != null) {
if (!forceDownload && attachment.downloaded != null) {
Utils.openFile(activity, File(attachment.downloaded))
return
}

View File

@ -231,8 +231,7 @@
android:ellipsize="middle"
android:text="Wyniki sprawdzianu z matematyki.pdf"
android:visibility="visible"
app:chipIcon="@drawable/googleg_standard_color_18"
app:chipMinHeight="36dp" />
app:chipIcon="@drawable/googleg_standard_color_18" />
<ProgressBar
style="?android:attr/progressBarStyle"

View File

@ -1279,4 +1279,5 @@
<string name="registration_enable_dialog_text">Rejestracja jest automatyczna, jeśli ta opcja jest włączona. Pozwala na tworzenie i odbieranie wydarzeń udostępnionych innym uczniom z Twojej klasy. Dzięki temu, można dodawać do dziennika pozycje nie zapisane przez nauczyciela.\n\nUpewnij się, że zapoznałeś się z warunkami <a href="http://szkolny.eu/privacy-policy">Polityki prywatności</a> i akceptujesz jej postanowienia.</string>
<string name="menu_add_remove_cards">Dodaj lub usuń karty</string>
<string name="menu_template">Template</string>
<string name="messages_attachment_download_again">Pobierz ponownie</string>
</resources>