From 04d81adc2bdbd302d1b6b2ed52379e43591ae25d Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Tue, 6 Dec 2022 22:17:31 -0400 Subject: [PATCH] Add compressed adapter --- .../com/example/myapplication/MainActivity.kt | 11 ++- .../ui/completed/CompletedAdapter.kt | 67 ++++++++++++++++++ .../ui/completed/CompletedFragment.kt | 18 +++-- .../ui/completed/CompletedItem.kt | 7 ++ .../ui/compressing/CompressingAdapter.kt | 69 ++++++++++--------- .../ui/compressing/CompressingFragment.kt | 8 +-- app/src/main/res/drawable/ic_share.xml | 5 ++ .../main/res/layout/fragment_completed.xml | 13 ++-- .../main/res/layout/fragment_compressing.xml | 4 +- app/src/main/res/layout/item_compressed.xml | 31 +++++++++ app/src/main/res/layout/item_compressing.xml | 53 +++++++------- app/src/main/res/values/strings.xml | 1 + 12 files changed, 206 insertions(+), 81 deletions(-) create mode 100644 app/src/main/java/com/example/myapplication/ui/completed/CompletedAdapter.kt create mode 100644 app/src/main/java/com/example/myapplication/ui/completed/CompletedItem.kt create mode 100644 app/src/main/res/drawable/ic_share.xml create mode 100644 app/src/main/res/layout/item_compressed.xml diff --git a/app/src/main/java/com/example/myapplication/MainActivity.kt b/app/src/main/java/com/example/myapplication/MainActivity.kt index c1d5577..3653378 100644 --- a/app/src/main/java/com/example/myapplication/MainActivity.kt +++ b/app/src/main/java/com/example/myapplication/MainActivity.kt @@ -23,6 +23,8 @@ import androidx.navigation.ui.setupWithNavController import com.arthenica.ffmpegkit.FFmpegKit import com.arthenica.ffmpegkit.FFmpegKitConfig 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.CompressingItem 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 session = FFmpegKit.executeAsync(command) { compressingItems.remove(item) + completedAdapter.refreshList(this) handler.post { 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 { val compressingItems: MutableList = mutableListOf() + val compressingAdapter = CompressingAdapter(compressingItems) - val adapter = CompressingAdapter(compressingItems) + val completedItems: MutableList = mutableListOf() + val completedAdapter = CompletedAdapter(completedItems) } } \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/ui/completed/CompletedAdapter.kt b/app/src/main/java/com/example/myapplication/ui/completed/CompletedAdapter.kt new file mode 100644 index 0000000..8bd35c7 --- /dev/null +++ b/app/src/main/java/com/example/myapplication/ui/completed/CompletedAdapter.kt @@ -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): RecyclerView.Adapter() { + 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() + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/ui/completed/CompletedFragment.kt b/app/src/main/java/com/example/myapplication/ui/completed/CompletedFragment.kt index d182c71..28ac761 100644 --- a/app/src/main/java/com/example/myapplication/ui/completed/CompletedFragment.kt +++ b/app/src/main/java/com/example/myapplication/ui/completed/CompletedFragment.kt @@ -7,6 +7,10 @@ import android.view.ViewGroup import android.widget.TextView import androidx.fragment.app.Fragment 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 class CompletedFragment : Fragment() { @@ -22,17 +26,21 @@ class CompletedFragment : Fragment() { container: ViewGroup?, savedInstanceState: Bundle? ): View { - val dashboardViewModel = - ViewModelProvider(this).get(CompletedViewModel::class.java) + val dashboardViewModel = ViewModelProvider(this).get(CompletedViewModel::class.java) _binding = FragmentCompletedBinding.inflate(inflater, container, false) val root: View = binding.root - val textView: TextView = binding.textCompleted + val completedRecycler = binding.root.findViewById(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) { - textView.text = it + //do nothing } - return root + return binding.root } override fun onDestroyView() { diff --git a/app/src/main/java/com/example/myapplication/ui/completed/CompletedItem.kt b/app/src/main/java/com/example/myapplication/ui/completed/CompletedItem.kt new file mode 100644 index 0000000..d1c5cef --- /dev/null +++ b/app/src/main/java/com/example/myapplication/ui/completed/CompletedItem.kt @@ -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) \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/ui/compressing/CompressingAdapter.kt b/app/src/main/java/com/example/myapplication/ui/compressing/CompressingAdapter.kt index 343ce7b..923f92e 100644 --- a/app/src/main/java/com/example/myapplication/ui/compressing/CompressingAdapter.kt +++ b/app/src/main/java/com/example/myapplication/ui/compressing/CompressingAdapter.kt @@ -1,34 +1,37 @@ -package com.example.myapplication.ui.compressing - -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.widget.ProgressBar -import android.widget.TextView -import androidx.recyclerview.widget.RecyclerView -import com.example.myapplication.R - -class CompressingAdapter(private val mCompressingItems: MutableList): RecyclerView.Adapter() { - inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { - val filename: TextView = itemView.findViewById(R.id.compressing_filename) - val date: TextView = itemView.findViewById(R.id.compressing_date) - val progress: ProgressBar = itemView.findViewById(R.id.compressing_progress) - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressing, parent, false) - return ViewHolder(compressingItemView) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val compressingItem: CompressingItem = mCompressingItems[position] - holder.filename.text = compressingItem.filename - holder.date.text = compressingItem.date.toString() - holder.progress.progress = compressingItem.progress.toInt() - - } - - override fun getItemCount(): Int { - return mCompressingItems.size - } +package com.example.myapplication.ui.compressing + +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.ProgressBar +import android.widget.TextView +import androidx.recyclerview.widget.RecyclerView +import com.example.myapplication.R +import com.example.myapplication.ui.completed.CompletedItem +import java.util.* + +class CompressingAdapter(private val mCompressingItems: MutableList): RecyclerView.Adapter() { + inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { + val filename: TextView = itemView.findViewById(R.id.compressing_filename) + val date: TextView = itemView.findViewById(R.id.compressing_date) + val progress: ProgressBar = itemView.findViewById(R.id.compressing_progress) + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressing, parent, false) + return ViewHolder(compressingItemView) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + val compressingItem: CompressingItem = mCompressingItems[position] + holder.filename.text = compressingItem.filename + holder.date.text = compressingItem.date.toString() + holder.progress.progress = compressingItem.progress.toInt() + + } + + override fun getItemCount(): Int { + return mCompressingItems.size + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/myapplication/ui/compressing/CompressingFragment.kt b/app/src/main/java/com/example/myapplication/ui/compressing/CompressingFragment.kt index 30b2c95..a082d6c 100644 --- a/app/src/main/java/com/example/myapplication/ui/compressing/CompressingFragment.kt +++ b/app/src/main/java/com/example/myapplication/ui/compressing/CompressingFragment.kt @@ -9,7 +9,6 @@ import androidx.lifecycle.ViewModelProvider import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.example.myapplication.MainActivity -import com.example.myapplication.MainActivity.Companion.compressingItems import com.example.myapplication.R import com.example.myapplication.databinding.FragmentCompressingBinding @@ -30,12 +29,13 @@ class CompressingFragment : Fragment() { val compressingRecycler = binding.root.findViewById(R.id.compressing_recycler_view) as? RecyclerView - compressingRecycler?.adapter = MainActivity.adapter + compressingRecycler?.adapter = MainActivity.compressingAdapter compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context) + + + return binding.root - - } override fun onDestroyView() { diff --git a/app/src/main/res/drawable/ic_share.xml b/app/src/main/res/drawable/ic_share.xml new file mode 100644 index 0000000..87cea78 --- /dev/null +++ b/app/src/main/res/drawable/ic_share.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/fragment_completed.xml b/app/src/main/res/layout/fragment_completed.xml index 67b24d9..c634c73 100644 --- a/app/src/main/res/layout/fragment_completed.xml +++ b/app/src/main/res/layout/fragment_completed.xml @@ -6,15 +6,14 @@ android:layout_height="match_parent" tools:context=".ui.completed.CompletedFragment"> - + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> \ No newline at end of file diff --git a/app/src/main/res/layout/item_compressed.xml b/app/src/main/res/layout/item_compressed.xml new file mode 100644 index 0000000..a353f85 --- /dev/null +++ b/app/src/main/res/layout/item_compressed.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_compressing.xml b/app/src/main/res/layout/item_compressing.xml index 736978f..90d6d68 100644 --- a/app/src/main/res/layout/item_compressing.xml +++ b/app/src/main/res/layout/item_compressing.xml @@ -1,29 +1,26 @@ - - - - - - - - - - + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 98c3fb5..8610134 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -12,4 +12,5 @@ Enter Size in MB Files Youtube + Share Button \ No newline at end of file