Add compressed adapter

This commit is contained in:
Isaac Shoebottom 2022-12-06 22:17:31 -04:00
parent 43132f9cdf
commit 04d81adc2b
12 changed files with 206 additions and 81 deletions

View File

@ -23,6 +23,8 @@ import androidx.navigation.ui.setupWithNavController
import com.arthenica.ffmpegkit.FFmpegKit import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.FFmpegKitConfig import com.arthenica.ffmpegkit.FFmpegKitConfig
import com.example.myapplication.databinding.ActivityMainBinding import com.example.myapplication.databinding.ActivityMainBinding
import com.example.myapplication.ui.completed.CompletedAdapter
import com.example.myapplication.ui.completed.CompletedItem
import com.example.myapplication.ui.compressing.CompressingAdapter import com.example.myapplication.ui.compressing.CompressingAdapter
import com.example.myapplication.ui.compressing.CompressingItem import com.example.myapplication.ui.compressing.CompressingItem
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
@ -128,10 +130,11 @@ class MainActivity : AppCompatActivity() {
val command = "-i $inUri -c:v mpeg4 ${outputFile.absolutePath} -y" val command = "-i $inUri -c:v mpeg4 ${outputFile.absolutePath} -y"
val session = FFmpegKit.executeAsync(command) { val session = FFmpegKit.executeAsync(command) {
compressingItems.remove(item) compressingItems.remove(item)
completedAdapter.refreshList(this)
handler.post { handler.post {
Toast.makeText(this, "Finished converting $fileName", Toast.LENGTH_SHORT).show() Toast.makeText(this, "Finished converting $fileName", Toast.LENGTH_SHORT).show()
adapter.notifyDataSetChanged() compressingAdapter.notifyDataSetChanged()
} }
} }
@ -144,13 +147,15 @@ class MainActivity : AppCompatActivity() {
adapter.notifyDataSetChanged() compressingAdapter.notifyDataSetChanged()
} }
} }
companion object { companion object {
val compressingItems: MutableList<CompressingItem> = mutableListOf() val compressingItems: MutableList<CompressingItem> = mutableListOf()
val compressingAdapter = CompressingAdapter(compressingItems)
val adapter = CompressingAdapter(compressingItems) val completedItems: MutableList<CompletedItem> = mutableListOf()
val completedAdapter = CompletedAdapter(completedItems)
} }
} }

View File

@ -0,0 +1,67 @@
package com.example.myapplication.ui.completed
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import java.util.*
class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>): RecyclerView.Adapter<CompletedAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val filename: TextView = itemView.findViewById(R.id.compressed_filename)
val date: TextView = itemView.findViewById(R.id.compressed_date)
var shareButton: ImageButton = itemView.findViewById(R.id.compressed_share)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressed, parent, false)
return ViewHolder(compressingItemView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val compressingItem: CompletedItem = mCompletedList[position]
holder.filename.text = compressingItem.filename
holder.date.text = compressingItem.date.toString()
holder.shareButton.setOnClickListener { shareFile(holder.itemView.context, compressingItem.uri) }
}
private fun shareFile(context: Context?, uri: Uri) {
val builder = VmPolicy.Builder()
StrictMode.setVmPolicy(builder.build()) // this is a hack to allow sharing files from the app
val intent = Intent(Intent.ACTION_SEND)
intent.type = "video/mp4"
intent.putExtra(Intent.EXTRA_STREAM, uri)
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
context?.startActivity(Intent.createChooser(intent, "Share Video"))
}
override fun getItemCount(): Int {
return mCompletedList.size
}
@SuppressLint("NotifyDataSetChanged") //this is fine because it is a custom adapter
fun refreshList(context: Context) {
mCompletedList.clear()
context.getExternalFilesDir(null)?.listFiles()?.forEach {
if (it.name.endsWith(".mp4")) {
val completedItem = CompletedItem(it.name, Date(it.lastModified()), ImageButton(context), Uri.fromFile(it))
mCompletedList.add(completedItem)
}
}
notifyDataSetChanged()
}
}

View File

@ -7,6 +7,10 @@ import android.view.ViewGroup
import android.widget.TextView import android.widget.TextView
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.MainActivity
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentCompletedBinding import com.example.myapplication.databinding.FragmentCompletedBinding
class CompletedFragment : Fragment() { class CompletedFragment : Fragment() {
@ -22,17 +26,21 @@ class CompletedFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val dashboardViewModel = val dashboardViewModel = ViewModelProvider(this).get(CompletedViewModel::class.java)
ViewModelProvider(this).get(CompletedViewModel::class.java)
_binding = FragmentCompletedBinding.inflate(inflater, container, false) _binding = FragmentCompletedBinding.inflate(inflater, container, false)
val root: View = binding.root val root: View = binding.root
val textView: TextView = binding.textCompleted val completedRecycler = binding.root.findViewById<View>(R.id.compressed_recycler_view) as? RecyclerView
completedRecycler?.adapter = MainActivity.completedAdapter
completedRecycler?.layoutManager = LinearLayoutManager(binding.root.context)
MainActivity.completedAdapter.refreshList(binding.root.context)
dashboardViewModel.text.observe(viewLifecycleOwner) { dashboardViewModel.text.observe(viewLifecycleOwner) {
textView.text = it //do nothing
} }
return root return binding.root
} }
override fun onDestroyView() { override fun onDestroyView() {

View File

@ -0,0 +1,7 @@
package com.example.myapplication.ui.completed
import android.net.Uri
import android.widget.ImageButton
import java.util.*
data class CompletedItem(val filename: String, val date: Date, val button: ImageButton, val uri: Uri)

View File

@ -1,5 +1,6 @@
package com.example.myapplication.ui.compressing package com.example.myapplication.ui.compressing
import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -7,6 +8,8 @@ import android.widget.ProgressBar
import android.widget.TextView import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R import com.example.myapplication.R
import com.example.myapplication.ui.completed.CompletedItem
import java.util.*
class CompressingAdapter(private val mCompressingItems: MutableList<CompressingItem>): RecyclerView.Adapter<CompressingAdapter.ViewHolder>() { class CompressingAdapter(private val mCompressingItems: MutableList<CompressingItem>): RecyclerView.Adapter<CompressingAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {

View File

@ -9,7 +9,6 @@ import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.MainActivity import com.example.myapplication.MainActivity
import com.example.myapplication.MainActivity.Companion.compressingItems
import com.example.myapplication.R import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentCompressingBinding import com.example.myapplication.databinding.FragmentCompressingBinding
@ -30,12 +29,13 @@ class CompressingFragment : Fragment() {
val compressingRecycler = binding.root.findViewById<View>(R.id.compressing_recycler_view) as? RecyclerView val compressingRecycler = binding.root.findViewById<View>(R.id.compressing_recycler_view) as? RecyclerView
compressingRecycler?.adapter = MainActivity.adapter compressingRecycler?.adapter = MainActivity.compressingAdapter
compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context) compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context)
return binding.root return binding.root
} }
override fun onDestroyView() { override fun onDestroyView() {

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -2.92,-2.92z"/>
</vector>

View File

@ -6,15 +6,14 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.completed.CompletedFragment"> tools:context=".ui.completed.CompletedFragment">
<TextView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/text_completed" android:id="@+id/compressed_recycler_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_marginStart="8dp" android:layout_marginStart="16dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="16dp"
android:textAlignment="center" android:layout_marginBottom="8dp"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"

View File

@ -12,7 +12,9 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_marginStart="16dp" android:layout_marginStart="16dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="@+id/compressed_filename"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/compressed_date"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
<ImageButton
android:id="@+id/compressed_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/ic_share"
android:contentDescription="@string/share_button" />
</LinearLayout>

View File

@ -1,29 +1,26 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:paddingTop="10dp" android:paddingTop="10dp"
android:paddingBottom="10dp"> android:paddingBottom="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView <TextView
android:id="@+id/compressing_filename" android:id="@+id/compressing_filename"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
<TextView <TextView
android:id="@+id/compressing_date" android:id="@+id/compressing_date"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</LinearLayout>
<ProgressBar <ProgressBar
android:id="@+id/compressing_progress" android:id="@+id/compressing_progress"
android:layout_width="wrap_content" style="@android:style/Widget.Holo.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> android:layout_height="wrap_content" />
</LinearLayout> </LinearLayout>

View File

@ -12,4 +12,5 @@
<string name="settings_custom_size_hint">Enter Size in MB</string> <string name="settings_custom_size_hint">Enter Size in MB</string>
<string name="files">Files</string> <string name="files">Files</string>
<string name="youtube">Youtube</string> <string name="youtube">Youtube</string>
<string name="share_button">Share Button</string>
</resources> </resources>