Skip to content
This repository was archived by the owner on Oct 10, 2024. It is now read-only.
Open
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
132 changes: 61 additions & 71 deletions app/src/main/java/com/abedelazizshe/lightcompressor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ class MainActivity : AppCompatActivity() {
const val REQUEST_CAPTURE_VIDEO = 1
}

private val uris = mutableListOf<Uri>()
private val data = mutableListOf<VideoDetailsModel>()
private lateinit var adapter: RecyclerViewAdapter

private val compressors: MutableList<VideoCompressor> = mutableListOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

Expand All @@ -62,12 +60,14 @@ class MainActivity : AppCompatActivity() {
}

binding.cancel.setOnClickListener {
VideoCompressor.cancel()
compressors.forEach {
it.cancel()
}
}

val recyclerview = findViewById<RecyclerView>(R.id.recyclerview)
recyclerview.layoutManager = LinearLayoutManager(this)
adapter = RecyclerViewAdapter(applicationContext, data)
adapter = RecyclerViewAdapter(applicationContext, mutableListOf())
recyclerview.adapter = adapter
}

Expand Down Expand Up @@ -108,24 +108,22 @@ class MainActivity : AppCompatActivity() {

private fun handleResult(data: Intent?) {
val clipData: ClipData? = data?.clipData
val uris = mutableListOf<Uri>()
if (clipData != null) {
for (i in 0 until clipData.itemCount) {
val videoItem = clipData.getItemAt(i)
uris.add(videoItem.uri)
}
processVideo()
processVideo(uris)
} else if (data != null && data.data != null) {
val uri = data.data
uris.add(uri!!)
processVideo()
processVideo(uris)
}
}

private fun reset() {
uris.clear()
binding.mainContents.visibility = View.GONE
data.clear()
adapter.notifyDataSetChanged()
}

private fun setReadStoragePermission() {
Expand Down Expand Up @@ -171,69 +169,61 @@ class MainActivity : AppCompatActivity() {
}

@SuppressLint("SetTextI18n")
private fun processVideo() {
private fun processVideo(uris: List<Uri>) {
binding.mainContents.visibility = View.VISIBLE

lifecycleScope.launch {
VideoCompressor.start(
context = applicationContext,
uris,
isStreamable = false,
sharedStorageConfiguration = SharedStorageConfiguration(
saveAt = SaveLocation.movies,
subFolderName = "my-demo-videos"
),
// appSpecificStorageConfiguration = AppSpecificStorageConfiguration(
//
// ),
configureWith = Configuration(
quality = VideoQuality.LOW,
videoNames = uris.map { uri -> uri.pathSegments.last() },
isMinBitrateCheckEnabled = true,
),
listener = object : CompressionListener {
override fun onProgress(index: Int, percent: Float) {
//Update UI
if (percent <= 100)
runOnUiThread {
data[index] = VideoDetailsModel(
"",
uris[index],
"",
percent
)
adapter.notifyDataSetChanged()
}
}

override fun onStart(index: Int) {
data.add(
index,
VideoDetailsModel("", uris[index], "")
)
adapter.notifyDataSetChanged()
}

override fun onSuccess(index: Int, size: Long, path: String?) {
data[index] = VideoDetailsModel(
path,
uris[index],
getFileSize(size),
100F
)
adapter.notifyDataSetChanged()
}

override fun onFailure(index: Int, failureMessage: String) {
Log.wtf("failureMessage", failureMessage)
}

override fun onCancelled(index: Int) {
Log.wtf("TAG", "compression has been cancelled")
// make UI changes, cleanup, etc
}
},
)
uris.forEach { uri ->
lifecycleScope.launch {
val compressor = VideoCompressor.createInstance(uri = uri)
compressors.add(compressor)
compressor.start(
context = applicationContext,
isStreamable = false,
sharedStorageConfiguration = SharedStorageConfiguration(
saveAt = SaveLocation.movies,
subFolderName = "my-demo-videos"
),
/*appSpecificStorageConfiguration = AppSpecificStorageConfiguration(
subFolderName = "temp-videos"
),*/
configureWith = Configuration(
quality = VideoQuality.LOW,
videoName = uri.pathSegments.last(),
isMinBitrateCheckEnabled = true,
),
listener = object : CompressionListener {
override fun onProgress(percent: Float) {
//Update UI
if (percent <= 100)
runOnUiThread {
adapter.updateProgressForUri(uri, percent)
}
}

override fun onStart() {
adapter.addData(VideoDetailsModel("", uri, ""))
}

override fun onSuccess(size: Long, path: String?) {
adapter.refreshItem(VideoDetailsModel(
path,
uri,
getFileSize(size),
100F
))
}

override fun onFailure(failureMessage: String) {
Log.wtf("failureMessage", failureMessage)
}

override fun onCancelled() {
Log.wtf("TAG", "compression has been cancelled")
// make UI changes, cleanup, etc
}
},
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.abedelazizshe.lightcompressor

import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -10,7 +11,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide

class RecyclerViewAdapter(private val context: Context, private val list: List<VideoDetailsModel>) :
class RecyclerViewAdapter(private val context: Context, private val list: MutableList<VideoDetailsModel>) :
RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand All @@ -20,6 +21,28 @@ class RecyclerViewAdapter(private val context: Context, private val list: List<V
return ViewHolder(view)
}

fun addData(videoDetailsModel: VideoDetailsModel) {
if (list.find { it.uri == videoDetailsModel.uri } == null) {
list.add(videoDetailsModel)
notifyDataSetChanged()
}
}

fun refreshItem(videoDetailsModel: VideoDetailsModel) {
val index = list.indexOfFirst { it.uri == videoDetailsModel.uri }
if (index != -1) {
list.removeAt(index)
list.add(index, videoDetailsModel)
}
}

fun updateProgressForUri(uri: Uri, progress: Float) {
list.find { it.uri == uri }?.let { model ->
model.progress = progress
notifyDataSetChanged()
}
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

val itemsViewModel = list[position]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ data class VideoDetailsModel(
val playableVideoPath: String?,
val uri: Uri,
val newSize: String,
val progress: Float = 0F
var progress: Float = 0F
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import androidx.annotation.WorkerThread
*/
interface CompressionListener {
@MainThread
fun onStart(index: Int)
fun onStart()

@MainThread
fun onSuccess(index: Int, size: Long, path: String?)
fun onSuccess(size: Long, path: String?)

@MainThread
fun onFailure(index: Int, failureMessage: String)
fun onFailure(failureMessage: String)

@WorkerThread
fun onProgress(index: Int, percent: Float)
fun onProgress(percent: Float)

@WorkerThread
fun onCancelled(index: Int)
fun onCancelled()
}

interface CompressionProgressListener {
fun onProgressChanged(index: Int, percent: Float)
fun onProgressCancelled(index: Int)
fun onProgressChanged(percent: Float)
fun onProgressCancelled()
}
Loading