Add DM create button

This commit is contained in:
Matthieu 2024-08-28 19:28:43 +02:00
parent 5a10a2bae4
commit 8a9cb4f4d3
8 changed files with 128 additions and 4 deletions

View File

@ -80,7 +80,7 @@ class ConversationActivity : BaseActivity() {
}
}
private suspend fun sendMessage(
suspend fun sendMessage(
api: PixelfedAPI,
pid: String,
) {

View File

@ -1,13 +1,24 @@
package org.pixeldroid.app.directmessages
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.launch
import org.pixeldroid.app.R
import org.pixeldroid.app.databinding.ActivityConversationBinding
import org.pixeldroid.app.databinding.ActivityFollowersBinding
import org.pixeldroid.app.profile.FollowsActivity
import org.pixeldroid.app.databinding.NewDmDialogBinding
import org.pixeldroid.app.utils.BaseActivity
class DirectMessagesActivity : BaseActivity() {
lateinit var binding: ActivityFollowersBinding
@ -25,6 +36,74 @@ class DirectMessagesActivity : BaseActivity() {
supportActionBar?.setTitle(R.string.direct_messages)
initConversationFragment(savedInstanceState)
addFab()
}
private fun addFab() {
// Create a Floating Action Button
val fab = FloatingActionButton(this).apply {
id = View.generateViewId()
setImageResource(android.R.drawable.ic_dialog_email) // Example icon
ViewCompat.setElevation(this, 8f) // Set elevation if needed
}
// Set LayoutParams for the FAB
val fabParams = CoordinatorLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT).apply {
gravity = android.view.Gravity.END or android.view.Gravity.BOTTOM
marginEnd = 16
bottomMargin = 16
}
// Add the FAB to the CoordinatorLayout
binding.coordinatorFollowers.addView(fab, fabParams)
fab.setOnClickListener {
newDirectMessage()
}
}
private fun newDirectMessage() {
val newDmDialogBinding =
NewDmDialogBinding.inflate(LayoutInflater.from(this))
MaterialAlertDialogBuilder(this)
.setTitle(R.string.new_dm_conversation)
.setMessage(R.string.dm_instruction)
.setView(newDmDialogBinding.root)
.setPositiveButton(android.R.string.ok) { _, _ ->
val name = newDmDialogBinding.dmTarget.text.toString()
val text = newDmDialogBinding.dmText.text.toString()
lifecycleScope.launch {
try {
apiHolder.api?.let {
val pid = it.lookupUser(name, remote = name.count { it == '@' } == 2)
.firstOrNull {
it.name == name
}?.id ?: return@launch errorSending()
it.sendDirectMessage(pid, text)
}
} catch (e: Exception) {
Log.e("DirectMessagesActivity", e.toString())
errorSending()
}
}
}
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.show()
//
// conversation?.accounts?.firstOrNull()?.let {
// val intent = Intent(itemView.context, ConversationActivity::class.java).apply {
// putExtra(PROFILE_ID, it.id)
// putExtra(CONVERSATION_ID, conversation?.id)
// putExtra(USERNAME, it.getDisplayName())
// }
// startActivity(intent)
// }
}
private fun errorSending() {
Snackbar.make(binding.root, R.string.new_dm_error, Snackbar.LENGTH_LONG).show()
}
private fun initConversationFragment(savedInstanceState: Bundle?) {

View File

@ -138,6 +138,12 @@ interface PixelfedAPI {
@Field("reblogs") reblogs : Boolean = true
) : Relationship
@POST("/api/v1.1/direct/lookup")
suspend fun lookupUser(
@Query("q") q: String,
@Query("remote") remote : Boolean = false
) : List<LookupUser>
@POST("/api/v1/accounts/{id}/unfollow")
suspend fun unfollow(
@Path("id") statusId: String,

View File

@ -0,0 +1,6 @@
package org.pixeldroid.app.utils.api.objects
data class LookupUser(
val name: String,
val id: String
)

View File

@ -3,6 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/coordinatorFollowers"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout

View File

@ -34,6 +34,7 @@
app:layout_constraintTop_toBottomOf="@+id/imageView4" />
<TextView
android:id="@id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/panda_pull_to_refresh_to_try_again"

View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/dm_target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:inputType="text"
android:hint="@string/dm_target"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="@+id/dm_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no"
android:inputType="text"
android:hint="@string/new_dm"
app:layout_constraintTop_toBottomOf="@id/dm_target"/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -354,4 +354,9 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
<string name="dm_title">DM to %1$s</string>
<string name="direct_messages">Direct Messages</string>
<string name="new_dm_conversation">Create new DM conversation</string>
<string name="dm_instruction">Fill in the target username and the message to send</string>
<string name="dm_target">Target username</string>
<string name="new_dm">Message you want to write. Say hello!</string>
<string name="new_dm_error">Error sending your message! Check the username</string>
</resources>