Skip to content

Commit

Permalink
WIP file captions - impl + theming
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Oct 25, 2023
1 parent efc7021 commit 8aff7bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 49 deletions.
58 changes: 15 additions & 43 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ import android.widget.RelativeLayout.LayoutParams
import android.widget.SeekBar
import android.widget.TextView
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.view.ContextThemeWrapper
import androidx.core.content.ContextCompat
import androidx.core.content.FileProvider
Expand Down Expand Up @@ -113,7 +112,6 @@ import coil.target.Target
import coil.transform.CircleCropTransformation
import com.google.android.flexbox.FlexboxLayout
import com.google.android.material.button.MaterialButton
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.nextcloud.android.common.ui.theme.utils.ColorRole
import com.nextcloud.talk.BuildConfig
Expand Down Expand Up @@ -188,6 +186,7 @@ import com.nextcloud.talk.ui.StatusDrawable
import com.nextcloud.talk.ui.bottom.sheet.ProfileBottomSheet
import com.nextcloud.talk.ui.dialog.AttachmentDialog
import com.nextcloud.talk.ui.dialog.DateTimePickerFragment
import com.nextcloud.talk.ui.dialog.FileAttachmentPreviewFragment
import com.nextcloud.talk.ui.dialog.MessageActionsDialog
import com.nextcloud.talk.ui.dialog.ShowReactionsDialog
import com.nextcloud.talk.ui.recyclerview.MessageSwipeActions
Expand Down Expand Up @@ -2411,43 +2410,12 @@ class ChatActivity :
filenamesWithLineBreaks.append(filename).append("\n")
}

val confirmationQuestion = when (filesToUpload.size) {
1 -> context.resources?.getString(R.string.nc_upload_confirm_send_single)?.let {
String.format(it, title.trim())
}

else -> context.resources?.getString(R.string.nc_upload_confirm_send_multiple)?.let {
String.format(it, title.trim())
}
}

binding.messageInputView.context?.let {
// TODO replace this function body with a custom dialog fragment
// This will be useful later on as well for previewing photos and using the photopicker
val materialAlertDialogBuilder = MaterialAlertDialogBuilder(it)
.setTitle(confirmationQuestion)
.setMessage(filenamesWithLineBreaks.toString())
// .setView(inflate(context, R.layout.dialog_file_attachment_preview, null))
.setPositiveButton(R.string.nc_yes) { _, _ ->
if (permissionUtil.isFilesPermissionGranted()) {
uploadFiles(filesToUpload)
} else {
UploadAndShareFilesWorker.requestStoragePermission(this)
}
}
.setNegativeButton(R.string.nc_no) { _, _ ->
// unused atm
}

viewThemeUtils.dialog.colorMaterialAlertDialogBackground(it, materialAlertDialogBuilder)

val dialog = materialAlertDialogBuilder.show()

viewThemeUtils.platform.colorTextButtons(
dialog.getButton(AlertDialog.BUTTON_POSITIVE),
dialog.getButton(AlertDialog.BUTTON_NEGATIVE)
)
}
val newFragment: DialogFragment = FileAttachmentPreviewFragment.newInstance(
filenamesWithLineBreaks.toString(),
filesToUpload,
this::uploadFiles
)
newFragment.show(supportFragmentManager, FileAttachmentPreviewFragment.TAG)
} catch (e: IllegalStateException) {
context.resources?.getString(R.string.nc_upload_failed)?.let {
Snackbar.make(
Expand Down Expand Up @@ -2633,13 +2601,13 @@ class ChatActivity :
}
}

private fun uploadFiles(files: MutableList<String>) {
private fun uploadFiles(files: MutableList<String>, caption: String = "") {
for (file in files) {
uploadFile(file, false)
}
uploadFile(file, false, caption)
} // TODO when rendering I can group attachments by checking their caption
}

private fun uploadFile(fileUri: String, isVoiceMessage: Boolean) {
private fun uploadFile(fileUri: String, isVoiceMessage: Boolean, caption: String = "") {
var metaData = ""

if (!participantPermissions.hasChatPermission()) {
Expand All @@ -2651,6 +2619,10 @@ class ChatActivity :
metaData = VOICE_MESSAGE_META_DATA
}

if (caption != "") {
metaData = "{\"caption\":\"$caption\"}"
}

try {
require(fileUri.isNotEmpty())
UploadAndShareFilesWorker.upload(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,76 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import autodagger.AutoInjector
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.nextcloud.talk.R
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.databinding.DialogFileAttachmentPreviewBinding
import com.nextcloud.talk.jobs.UploadAndShareFilesWorker
import com.nextcloud.talk.ui.theme.ViewThemeUtils
import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil
import javax.inject.Inject

class FileAttachmentPreviewFragment : DialogFragment() {
@AutoInjector(NextcloudTalkApplication::class)
class FileAttachmentPreviewFragment(
filenames: String,
filesToUpload: MutableList<String>,
functionToCall: (files: MutableList<String>, caption: String) -> Unit
) : DialogFragment() {
private val files = filenames
private val filesList = filesToUpload
private val uploadFiles = functionToCall
lateinit var binding: DialogFileAttachmentPreviewBinding

@Inject
lateinit var permissionUtil: PlatformPermissionUtil

@Inject
lateinit var viewThemeUtils: ViewThemeUtils
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// todo
return super.onCreateDialog(savedInstanceState)
binding = DialogFileAttachmentPreviewBinding.inflate(LayoutInflater.from(context))
return MaterialAlertDialogBuilder(requireContext()).setView(binding.root).create()
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// todo
return super.onCreateView(inflater, container, savedInstanceState)
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
setUpViews()
setUpListeners()
return inflater.inflate(R.layout.dialog_file_attachment_preview, container, false)
}

private fun setUpViews() {
binding.dialogFileAttachmentPreviewFilenames.text = files
viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(binding.buttonClose)
viewThemeUtils.material.colorMaterialButtonPrimaryBorderless(binding.buttonSend)
viewThemeUtils.platform.colorViewBackground(binding.root)
viewThemeUtils.material.colorTextInputLayout(binding.dialogFileAttachmentPreviewLayout)
}

private fun setUpListeners() {
binding.buttonClose.setOnClickListener {
dismiss()
}

binding.buttonSend.setOnClickListener {
if (permissionUtil.isFilesPermissionGranted()) {
val caption: String = binding.dialogFileAttachmentPreviewCaption.text.toString()
uploadFiles(filesList, caption)
} else {
UploadAndShareFilesWorker.requestStoragePermission(requireActivity())
}
dismiss()
}
}

companion object {
@JvmStatic
fun newInstance() = FileAttachmentPreviewFragment()
fun newInstance(
filenames: String,
filesToUpload: MutableList<String>,
functionToCall: (files: MutableList<String>, caption: String) -> Unit
) =
FileAttachmentPreviewFragment(filenames, filesToUpload, functionToCall)
val TAG: String = FilterConversationFragment::class.java.simpleName
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/dialog_file_attachment_preview.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
tools:text="a.png\nb.png\nc.png"/>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/dialog_file_attachment_preview_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down

0 comments on commit 8aff7bb

Please sign in to comment.