mirror of
https://gitlab.shinice.net/pixeldroid/PixelDroid
synced 2025-01-28 22:39:22 +01:00
Check if size of image isn't too big, and show dialog if it is
This commit is contained in:
parent
50957ea228
commit
71714a5f3c
@ -42,6 +42,7 @@ import java.io.OutputStream
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
import kotlin.math.ceil
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
private const val TAG = "Post Creation Activity"
|
||||
@ -49,9 +50,10 @@ private const val MORE_PICTURES_REQUEST_CODE = 0xffff
|
||||
|
||||
data class PhotoData(
|
||||
var imageUri: Uri,
|
||||
var size: Long,
|
||||
var uploadId: String? = null,
|
||||
var progress: Int? = null,
|
||||
var imageDescription: String? = null
|
||||
var imageDescription: String? = null,
|
||||
)
|
||||
|
||||
class PostCreationActivity : BaseActivity() {
|
||||
@ -59,10 +61,9 @@ class PostCreationActivity : BaseActivity() {
|
||||
private lateinit var accessToken: String
|
||||
private lateinit var pixelfedAPI: PixelfedAPI
|
||||
|
||||
private var albumLimit by Delegates.notNull<Int>()
|
||||
|
||||
private var positionResult = 0
|
||||
private var user: UserDatabaseEntity? = null
|
||||
private lateinit var instance: InstanceDatabaseEntity
|
||||
|
||||
private val photoData: ArrayList<PhotoData> = ArrayList()
|
||||
|
||||
@ -75,7 +76,7 @@ class PostCreationActivity : BaseActivity() {
|
||||
|
||||
user = db.userDao().getActiveUser()
|
||||
|
||||
val instance: InstanceDatabaseEntity = user?.run {
|
||||
instance = user?.run {
|
||||
db.instanceDao().getAll().first { instanceDatabaseEntity ->
|
||||
instanceDatabaseEntity.uri.contains(instance_uri)
|
||||
}
|
||||
@ -83,8 +84,6 @@ class PostCreationActivity : BaseActivity() {
|
||||
|
||||
binding.postTextInputLayout.counterMaxLength = instance.maxStatusChars
|
||||
|
||||
albumLimit = instance.albumLimit
|
||||
|
||||
// get image URIs
|
||||
intent.clipData?.let { addPossibleImages(it) }
|
||||
|
||||
@ -96,13 +95,13 @@ class PostCreationActivity : BaseActivity() {
|
||||
carousel.layoutCarouselCallback = {
|
||||
if(it){
|
||||
// Became a carousel
|
||||
binding.toolbar3.visibility = VISIBLE
|
||||
binding.toolbarPostCreation.visibility = VISIBLE
|
||||
} else {
|
||||
// Became a grid
|
||||
binding.toolbar3.visibility = INVISIBLE
|
||||
binding.toolbarPostCreation.visibility = INVISIBLE
|
||||
}
|
||||
}
|
||||
carousel.maxEntries = albumLimit
|
||||
carousel.maxEntries = instance.albumLimit
|
||||
carousel.addPhotoButtonCallback = {
|
||||
addPhoto(applicationContext)
|
||||
}
|
||||
@ -158,20 +157,51 @@ class PostCreationActivity : BaseActivity() {
|
||||
*/
|
||||
private fun addPossibleImages(clipData: ClipData){
|
||||
var count = clipData.itemCount
|
||||
if(count + photoData.size > albumLimit){
|
||||
if(count + photoData.size > instance.albumLimit){
|
||||
AlertDialog.Builder(this).apply {
|
||||
setMessage(getString(R.string.total_exceeds_album_limit).format(albumLimit))
|
||||
setMessage(getString(R.string.total_exceeds_album_limit).format(instance.albumLimit))
|
||||
setNegativeButton(android.R.string.ok) { _, _ -> }
|
||||
}.show()
|
||||
count = count.coerceAtMost(albumLimit - photoData.size)
|
||||
count = count.coerceAtMost(instance.albumLimit - photoData.size)
|
||||
}
|
||||
if (count + photoData.size >= albumLimit) {
|
||||
if (count + photoData.size >= instance.albumLimit) {
|
||||
// Disable buttons to add more images
|
||||
binding.addPhotoButton.isEnabled = false
|
||||
}
|
||||
for (i in 0 until count) {
|
||||
clipData.getItemAt(i).uri.let {
|
||||
photoData.add(PhotoData(it))
|
||||
val size: Long =
|
||||
if (it.toString().startsWith("content")) {
|
||||
contentResolver.query(it, null, null, null, null)
|
||||
?.use { cursor ->
|
||||
/* Get the column indexes of the data in the Cursor,
|
||||
* move to the first row in the Cursor, get the data,
|
||||
* and display it.
|
||||
*/
|
||||
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
|
||||
cursor.moveToFirst()
|
||||
cursor.getLong(sizeIndex)
|
||||
} ?: 0
|
||||
} else {
|
||||
it.toFile().length()
|
||||
}
|
||||
val sizeInkBytes = ceil(size.toDouble() / 1000).toLong()
|
||||
if(sizeInkBytes > instance.maxPhotoSize || sizeInkBytes > instance.maxVideoSize){
|
||||
val maxSize = when {
|
||||
instance.maxPhotoSize != instance.maxVideoSize -> {
|
||||
val type = contentResolver.getType(it)
|
||||
if(type?.startsWith("video/") == true){
|
||||
instance.maxVideoSize
|
||||
} else instance.maxPhotoSize
|
||||
}
|
||||
else -> instance.maxPhotoSize
|
||||
}
|
||||
AlertDialog.Builder(this).apply {
|
||||
setMessage(getString(R.string.size_exceeds_instance_limit).format(photoData.size + 1, sizeInkBytes, maxSize))
|
||||
setNegativeButton(android.R.string.ok) { _, _ -> }
|
||||
}.show()
|
||||
}
|
||||
photoData.add(PhotoData(imageUri = it, size = size))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -196,21 +226,21 @@ class PostCreationActivity : BaseActivity() {
|
||||
|
||||
if(path.startsWith("file")) {
|
||||
MediaScannerConnection.scanFile(
|
||||
this,
|
||||
arrayOf(path.toUri().toFile().absolutePath),
|
||||
null
|
||||
this,
|
||||
arrayOf(path.toUri().toFile().absolutePath),
|
||||
null
|
||||
) { path, uri ->
|
||||
if (uri == null) {
|
||||
Log.e(
|
||||
"NEW IMAGE SCAN FAILED",
|
||||
"Tried to scan $path, but it failed"
|
||||
"NEW IMAGE SCAN FAILED",
|
||||
"Tried to scan $path, but it failed"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Snackbar.make(
|
||||
button, getString(R.string.save_image_success),
|
||||
Snackbar.LENGTH_LONG
|
||||
button, getString(R.string.save_image_success),
|
||||
Snackbar.LENGTH_LONG
|
||||
).show()
|
||||
}
|
||||
|
||||
@ -223,8 +253,8 @@ class PostCreationActivity : BaseActivity() {
|
||||
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name)
|
||||
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png")
|
||||
contentValues.put(
|
||||
MediaStore.MediaColumns.RELATIVE_PATH,
|
||||
Environment.DIRECTORY_PICTURES
|
||||
MediaStore.MediaColumns.RELATIVE_PATH,
|
||||
Environment.DIRECTORY_PICTURES
|
||||
)
|
||||
val imageUri: Uri =
|
||||
resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)!!
|
||||
@ -271,23 +301,7 @@ class PostCreationActivity : BaseActivity() {
|
||||
val imageUri = data.imageUri
|
||||
val imageInputStream = contentResolver.openInputStream(imageUri)!!
|
||||
|
||||
val size =
|
||||
if (imageUri.toString().startsWith("content")) {
|
||||
contentResolver.query(imageUri, null, null, null, null)
|
||||
?.use { cursor ->
|
||||
/* Get the column indexes of the data in the Cursor,
|
||||
* move to the first row in the Cursor, get the data,
|
||||
* and display it.
|
||||
*/
|
||||
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
|
||||
cursor.moveToFirst()
|
||||
cursor.getLong(sizeIndex)
|
||||
} ?: 0
|
||||
} else {
|
||||
imageUri.toFile().length()
|
||||
}
|
||||
|
||||
val imagePart = ProgressRequestBody(imageInputStream, size)
|
||||
val imagePart = ProgressRequestBody(imageInputStream, data.size)
|
||||
val requestBody = MultipartBody.Builder()
|
||||
.setType(MultipartBody.FORM)
|
||||
.addFormDataPart("file", System.currentTimeMillis().toString(), imagePart)
|
||||
@ -312,26 +326,33 @@ class PostCreationActivity : BaseActivity() {
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
{ attachment: Attachment ->
|
||||
data.progress = 0
|
||||
data.uploadId = attachment.id!!
|
||||
},
|
||||
{ e ->
|
||||
binding.uploadError.visibility = View.VISIBLE
|
||||
e.printStackTrace()
|
||||
postSub?.dispose()
|
||||
sub.dispose()
|
||||
},
|
||||
{
|
||||
data.progress = 100
|
||||
if(photoData.all{it.progress == 100 && it.uploadId != null}){
|
||||
binding.uploadProgressBar.visibility = View.GONE
|
||||
binding.uploadCompletedTextview.visibility = View.VISIBLE
|
||||
post()
|
||||
{ attachment: Attachment ->
|
||||
data.progress = 0
|
||||
data.uploadId = attachment.id!!
|
||||
},
|
||||
{ e: Throwable ->
|
||||
binding.uploadError.visibility = View.VISIBLE
|
||||
if(e is HttpException){
|
||||
binding.uploadErrorTextExplanation.text =
|
||||
getString(R.string.upload_error).format(e.code())
|
||||
binding.uploadErrorTextExplanation.visibility= VISIBLE
|
||||
} else {
|
||||
binding.uploadErrorTextExplanation.visibility= View.GONE
|
||||
}
|
||||
e.printStackTrace()
|
||||
postSub?.dispose()
|
||||
sub.dispose()
|
||||
},
|
||||
{
|
||||
data.progress = 100
|
||||
if (photoData.all { it.progress == 100 && it.uploadId != null }) {
|
||||
binding.uploadProgressBar.visibility = View.GONE
|
||||
binding.uploadCompletedTextview.visibility = View.VISIBLE
|
||||
post()
|
||||
}
|
||||
postSub?.dispose()
|
||||
sub.dispose()
|
||||
}
|
||||
postSub?.dispose()
|
||||
sub.dispose()
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -342,23 +363,23 @@ class PostCreationActivity : BaseActivity() {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
try {
|
||||
pixelfedAPI.postStatus(
|
||||
authorization = "Bearer $accessToken",
|
||||
statusText = description,
|
||||
media_ids = photoData.mapNotNull { it.uploadId }.toList()
|
||||
authorization = "Bearer $accessToken",
|
||||
statusText = description,
|
||||
media_ids = photoData.mapNotNull { it.uploadId }.toList()
|
||||
)
|
||||
Toast.makeText(applicationContext,getString(R.string.upload_post_success),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(applicationContext, getString(R.string.upload_post_success),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
val intent = Intent(this@PostCreationActivity, MainActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
startActivity(intent)
|
||||
} catch (exception: IOException) {
|
||||
Toast.makeText(applicationContext,getString(R.string.upload_post_error),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(applicationContext, getString(R.string.upload_post_error),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
Log.e(TAG, exception.toString())
|
||||
enableButton(true)
|
||||
} catch (exception: HttpException) {
|
||||
Toast.makeText(applicationContext,getString(R.string.upload_post_failed),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(applicationContext, getString(R.string.upload_post_failed),
|
||||
Toast.LENGTH_SHORT).show()
|
||||
Log.e(TAG, exception.response().toString() + exception.message().toString())
|
||||
enableButton(true)
|
||||
}
|
||||
|
@ -8,14 +8,15 @@
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/upload_error"
|
||||
android:visibility="gone"
|
||||
android:layout_width="match_parent"
|
||||
android:elevation="2dp"
|
||||
android:layout_height="match_parent"
|
||||
android:elevation="2dp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent">
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:visibility="visible">
|
||||
|
||||
<com.mikepenz.iconics.view.IconicsTextView
|
||||
android:id="@+id/upload_error_text_view"
|
||||
@ -30,15 +31,29 @@
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<com.mikepenz.iconics.view.IconicsTextView
|
||||
android:id="@+id/upload_error_text_explanation"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#90000000"
|
||||
tools:text="Error code returned by server: 413"
|
||||
android:textColor="@color/colorPrimaryError"
|
||||
android:textSize="20sp"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/upload_error_text_view"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/retry_upload_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:text="@string/retry"
|
||||
app:layout_constraintEnd_toEndOf="@id/upload_error_text_view"
|
||||
app:layout_constraintHorizontal_bias="0.498"
|
||||
app:layout_constraintStart_toStartOf="@id/upload_error_text_view"
|
||||
app:layout_constraintTop_toBottomOf="@id/upload_error_text_view" />
|
||||
app:layout_constraintTop_toBottomOf="@+id/upload_error_text_explanation" />
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
@ -135,7 +150,7 @@
|
||||
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
android:id="@+id/toolbar3"
|
||||
android:id="@+id/toolbarPostCreation"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="#40000000"
|
||||
|
@ -38,10 +38,11 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
|
||||
<string name="domain_of_your_instance">Domain of your instance</string>
|
||||
<string name="connect_to_pixelfed">Connect to Pixelfed</string>
|
||||
<string name="login_connection_required_once">You need to be online to be able to add the first account and use PixelDroid :(</string>
|
||||
<string name="add_account_name">Add Account</string>
|
||||
<string name="add_account_description">Add another Pixelfed Account</string>
|
||||
<string name="api_not_enabled_dialog">The API is not activated on this instance. Contact your administrator to ask them to activate it.</string>
|
||||
<!-- Drawer -->
|
||||
<string name="logout">Log out</string>
|
||||
<string name="add_account_name">Add Account</string>
|
||||
<string name="add_account_description">Add another Pixelfed Account</string>
|
||||
<!-- Post creation -->
|
||||
<string name="permission_denied">Permission denied</string>
|
||||
<string name="save_image_failed">Unable to save image</string>
|
||||
@ -64,6 +65,10 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
|
||||
<string name="switch_to_carousel">Switch to carousel</string>
|
||||
<string name="save_image_description">Save image description</string>
|
||||
<string name="no_media_description">Add a media description here…</string>
|
||||
<string name="total_exceeds_album_limit">You chose more images than the maximum your server allows (%1$s). Images beyond the limit have been ignored.</string>
|
||||
<string name="size_exceeds_instance_limit">Size of image number %1$s in the album exceeds the maximum size allowed by the instance (%2$s kB but the limit is %3$s kB). You might not be able to upload it.</string>
|
||||
<string name="upload_error">Error code returned by server: %1$s</string>
|
||||
|
||||
|
||||
<!-- Post editing -->
|
||||
<string name="lbl_brightness">BRIGHTNESS</string>
|
||||
@ -78,6 +83,8 @@ For more info about Pixelfed, you can check here: https://pixelfed.org"</string>
|
||||
<string name="crop_result_error">"Couldn't retrieve image after crop"</string>
|
||||
<string name="image_preview">Preview of the image being edited</string>
|
||||
<string name="crop_button">Button to crop or rotate the image</string>
|
||||
<string name="save_before_returning">Save your edits?</string>
|
||||
<string name="no_cancel_edit">No, cancel edit</string>
|
||||
|
||||
<!-- Camera -->
|
||||
<string name="capture_button_alt">Capture</string>
|
||||
@ -193,8 +200,4 @@ Following"</item>
|
||||
<string name="help_translate">Help translate PixelDroid to your language:</string>
|
||||
<string name="issues_contribute">Report issues or contribute to the application:</string>
|
||||
<string name="mascot_description">Image showing a red panda, Pixelfed\'s mascot, using a phone</string>
|
||||
<string name="save_before_returning">Save your edits?</string>
|
||||
<string name="no_cancel_edit">No, cancel edit</string>
|
||||
<string name="api_not_enabled_dialog">The API is not activated on this instance. Contact your administrator to ask them to activate it.</string>
|
||||
<string name="total_exceeds_album_limit">You chose more images than the maximum your server allows (%1$s). Images beyond the limit have been ignored.</string>
|
||||
</resources>
|
Loading…
x
Reference in New Issue
Block a user