Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,14 @@ class MediaAdapter(
it, it.getString(R.string.upload_unsuccessful_description),
R.string.upload_unsuccessful, R.drawable.ic_error, listOf(
AlertHelper.positiveButton(R.string.retry) { _, _ ->
media[pos].sStatus = Media.Status.Queued
media[pos].statusMessage = ""
media[pos].save()

updateItem(media[pos].id)
media[pos].apply {
sStatus = Media.Status.Queued
statusMessage = ""
save()

BroadcastManager.postChange(it, collectionId, id)
}

UploadService.startUploadService(it)
},
Expand Down Expand Up @@ -136,14 +139,16 @@ class MediaAdapter(
holder.handle?.toggle(isEditMode)
}

fun updateItem(mediaId: Long): Boolean {
fun updateItem(mediaId: Long, progress: Long): Boolean {
val idx = media.indexOfFirst { it.id == mediaId }
if (idx < 0) return false

val item = Media.get(mediaId) ?: return false

media[idx] = item

if (progress >= 0) {
media[idx].progress = progress
} else {
val item = Media.get(mediaId) ?: return false
media[idx] = item
}
notifyItemChanged(idx)

return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import net.opendasharchive.openarchive.fragments.VideoRequestHandler
import net.opendasharchive.openarchive.util.extensions.hide
import net.opendasharchive.openarchive.util.extensions.show
import timber.log.Timber
import java.io.InputStream
import kotlin.math.roundToInt

abstract class MediaViewHolder(protected val binding: ViewBinding): RecyclerView.ViewHolder(binding.root) {
Expand Down Expand Up @@ -303,18 +304,19 @@ abstract class MediaViewHolder(protected val binding: ViewBinding): RecyclerView
fileInfo?.text = Formatter.formatShortFileSize(mContext, file.length())
} else {
if (media.contentLength == -1L) {
var iStream: InputStream? = null
try {
val iStream = mContext.contentResolver.openInputStream(media.fileUri)
iStream = mContext.contentResolver.openInputStream(media.fileUri)

if (iStream != null) {
media.contentLength = iStream.available().toLong()
media.save()

iStream.close()
}
}
catch (e: Throwable) {
Timber.e(e)
} finally {
iStream?.close()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
Expand Down Expand Up @@ -42,25 +44,26 @@ class MainMediaFragment : Fragment() {
private var mAdapters = HashMap<Long, MediaAdapter>()
private var mSection = HashMap<Long, SectionViewHolder>()
private var mProjectId = -1L
private var mCollections = ArrayList<Collection>()
private var mCollections = mutableMapOf<Long, Collection>()

private lateinit var mBinding: FragmentMainMediaBinding

private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() {

private val handler = Handler(Looper.getMainLooper())
override fun onReceive(context: Context, intent: Intent) {
val action = BroadcastManager.getAction(intent)
val mediaId = action?.mediaId ?: return

if (mediaId < 0) return
val action = BroadcastManager.getAction(intent) ?: return

when (action) {
BroadcastManager.Action.Change -> {
updateItem(mediaId)
handler.post {
updateItem(action.collectionId, action.mediaId, action.progress)
}
}

BroadcastManager.Action.Delete -> {
refresh()
handler.post {
refresh()
}
}
}
}
Expand Down Expand Up @@ -109,36 +112,43 @@ class MainMediaFragment : Fragment() {

mBinding = FragmentMainMediaBinding.inflate(inflater, container, false)

refresh()

return mBinding.root
}

fun updateItem(mediaId: Long) {
for (adapter in mAdapters.values) {
if (adapter.updateItem(mediaId)) break
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

refresh()
}

fun updateItem(collectionId: Long, mediaId: Long, progress: Long) {
mAdapters[collectionId]?.apply {
updateItem(mediaId, progress)
mCollections[collectionId]?.let { collection ->
mSection[collectionId]?.setHeader(collection, media)
}
}
}

fun refresh() {
mCollections = ArrayList(Collection.getByProject(mProjectId))
mCollections = Collection.getByProject(mProjectId).associateBy { it.id }.toMutableMap()

// Remove all sections, which' collections don't exist anymore.
val toDelete = mAdapters.keys.filter { id ->
mCollections.firstOrNull { it.id == id } == null
mCollections.containsKey(id).not()
}.toMutableList()

mCollections.forEach { collection ->
mCollections.forEach { (id, collection) ->
val media = collection.media

// Also remove all empty collections.
if (media.isEmpty()) {
toDelete.add(collection.id)
toDelete.add(id)
return@forEach
}

val adapter = mAdapters[collection.id]
val holder = mSection[collection.id]
val adapter = mAdapters[id]
val holder = mSection[id]

if (adapter != null) {
adapter.updateData(media)
Expand All @@ -154,22 +164,20 @@ class MainMediaFragment : Fragment() {
// while adding images.
deleteCollections(toDelete, false)

if (::mBinding.isInitialized) {
mBinding.addMediaHint.toggle(mCollections.isEmpty())
}
mBinding.addMediaHint.toggle(mCollections.isEmpty())
}

fun deleteSelected() {
val toDelete = ArrayList<Long>()

mCollections.forEach { collection ->
if (mAdapters[collection.id]?.deleteSelected() == true) {
mCollections.forEach { (id, collection) ->
if (mAdapters[id]?.deleteSelected() == true) {
val media = collection.media

if (media.isEmpty()) {
toDelete.add(collection.id)
} else {
mSection[collection.id]?.setHeader(collection, media)
mSection[id]?.setHeader(collection, media)
}
}
}
Expand Down Expand Up @@ -208,12 +216,11 @@ class MainMediaFragment : Fragment() {
val holder = mSection.remove(collectionId)
(holder?.root?.parent as? ViewGroup)?.removeView(holder.root)

val idx = mCollections.indexOfFirst { it.id == collectionId }

if (idx > -1 && idx < mCollections.size) {
val collection = mCollections.removeAt(idx)

if (cleanup) collection.delete()
mCollections[collectionId]?.let {
mCollections.remove(collectionId)
if (cleanup) {
it.delete()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class SectionViewHolder(
collection: Collection,
media: List<Media>
) {
if (media.firstOrNull { it.isUploading } != null)
if (media.any { it.isUploading })
{
timestamp.setText(R.string.uploading)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ abstract class Conduit(
* result is a site specific unique id that we can use to fetch the data,
* build an embed tag, etc. for some sites this might be a URL
*/
fun jobSucceeded() {
mMedia.progress = 100
fun jobSucceeded() {
mMedia.progress = mMedia.contentLength
mMedia.sStatus = Media.Status.Uploaded
mMedia.save()

BroadcastManager.postChange(mContext, mMedia.id)
BroadcastManager.postChange(mContext, mMedia.collectionId, mMedia.id)
}

fun jobFailed(exception: Throwable) {
fun jobFailed(exception: Throwable) {
// If an upload was cancelled, ignore the error.
if (mCancelled) return

Expand All @@ -109,25 +109,14 @@ abstract class Conduit(

Timber.d(exception)

BroadcastManager.postChange(mContext, mMedia.id)
BroadcastManager.postChange(mContext, mMedia.collectionId, mMedia.id)
}

// track when the last progress broadcast was sent, timestamp
// we use this to limit the rate of sending out these broadcasts
private var lastProgressBroadcast = 0L
fun jobProgress(uploadedBytes: Long) {
mMedia.progress = uploadedBytes

fun jobProgress(uploadedBytes: Long) {
// making sure we're not writing to the database more often than (1000/150=)~7 times a second.
// jobProgress is getting called up to several hundred times a second.
if (System.currentTimeMillis() > lastProgressBroadcast + 150) {
lastProgressBroadcast = System.currentTimeMillis()

mMedia.progress = uploadedBytes
mMedia.save()

BroadcastManager.postChange(mContext, mMedia.id)
}
}
BroadcastManager.postProgress(mContext, mMedia.collectionId, mMedia.id, uploadedBytes)
}

/**
* workaround to deal with some quirks in our data model?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class SaveClient(context: Context) : StrongBuilderBase<SaveClient, OkHttpClient>

okBuilder = OkHttpClient.Builder()
.addInterceptor(cacheInterceptor)
.connectTimeout(20L, TimeUnit.SECONDS)
.writeTimeout(20L, TimeUnit.SECONDS)
.readTimeout(20L, TimeUnit.SECONDS)
.connectTimeout(40L, TimeUnit.SECONDS)
.writeTimeout(40L, TimeUnit.SECONDS)
.readTimeout(40L, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.protocols(arrayListOf(Protocol.HTTP_1_1))
}
Expand Down
Loading