Clean app and add delete

This commit is contained in:
Isaac Shoebottom 2022-12-06 22:47:10 -04:00
parent 62ea78dc3c
commit e68a984411
10 changed files with 47 additions and 52 deletions

View File

@ -6,7 +6,6 @@ import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.media.MediaMetadataRetriever import android.media.MediaMetadataRetriever
import android.net.Uri import android.net.Uri
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
@ -30,7 +29,6 @@ import com.example.myapplication.ui.completed.CompletedAdapter
import com.example.myapplication.ui.completed.CompletedItem 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.example.myapplication.ui.settings.SettingsFragment
import com.example.myapplication.ui.settings.SettingsViewModel import com.example.myapplication.ui.settings.SettingsViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
import java.io.File import java.io.File
@ -152,11 +150,11 @@ class MainActivity : AppCompatActivity() {
Log.i("ethan",command) Log.i("ethan",command)
val session = FFmpegKit.executeAsync(command) { val session = FFmpegKit.executeAsync(command) {
compressingItems.remove(item) compressingItems.remove(item)
adapter.refreshList(this) 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()
} }
} }
@ -165,7 +163,7 @@ class MainActivity : AppCompatActivity() {
Log.i("Tag", session.output) Log.i("Tag", session.output)
adapter.notifyDataSetChanged() compressingAdapter.notifyDataSetChanged()
}else{ }else{
Toast.makeText(applicationContext,"File is less than target size",Toast.LENGTH_LONG).show() Toast.makeText(applicationContext,"File is less than target size",Toast.LENGTH_LONG).show()
} }

View File

@ -1,6 +1,7 @@
package com.example.myapplication.ui.completed package com.example.myapplication.ui.completed
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.AlertDialog
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
@ -20,7 +21,8 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
val filename: TextView = itemView.findViewById(R.id.compressed_filename) val filename: TextView = itemView.findViewById(R.id.compressed_filename)
val date: TextView = itemView.findViewById(R.id.compressed_date) val date: TextView = itemView.findViewById(R.id.compressed_date)
var shareButton: ImageButton = itemView.findViewById(R.id.compressed_share) val shareButton: ImageButton = itemView.findViewById(R.id.compressed_share)
val deleteButton: ImageButton = itemView.findViewById(R.id.compressed_delete)
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
@ -33,12 +35,30 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
holder.filename.text = compressingItem.filename holder.filename.text = compressingItem.filename
holder.date.text = compressingItem.date.toString() holder.date.text = compressingItem.date.toString()
holder.shareButton.setOnClickListener { shareFile(holder.itemView.context, compressingItem.uri) } holder.shareButton.setOnClickListener { shareFile(holder.itemView.context, compressingItem.uri) }
holder.deleteButton.setOnClickListener { deleteFile(holder.itemView.context, compressingItem.uri) }
} }
@SuppressLint("NotifyDataSetChanged")
private fun deleteFile(context: Context?, uri: Uri) {
val policy = VmPolicy.Builder().build()
StrictMode.setVmPolicy(policy) // this is a hack to allow the file to be deleted
AlertDialog.Builder(context)
.setTitle("Delete file")
.setMessage("Are you sure you want to delete this file?")
.setPositiveButton("Yes") { _, _ ->
val file = java.io.File(uri.path!!)
file.delete()
refreshList(context!!)
}
.setNegativeButton("No") { _, _ -> }
.show()
}
private fun shareFile(context: Context?, uri: Uri) { private fun shareFile(context: Context?, uri: Uri) {
val builder = VmPolicy.Builder() val policy = VmPolicy.Builder().build()
StrictMode.setVmPolicy(builder.build()) // this is a hack to allow sharing files from the app StrictMode.setVmPolicy(policy) // this is a hack to allow sharing files from the app
val intent = Intent(Intent.ACTION_SEND) val intent = Intent(Intent.ACTION_SEND)
@ -57,7 +77,7 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
mCompletedList.clear() mCompletedList.clear()
context.getExternalFilesDir(null)?.listFiles()?.forEach { context.getExternalFilesDir(null)?.listFiles()?.forEach {
if (it.name.endsWith(".mp4")) { if (it.name.endsWith(".mp4")) {
val completedItem = CompletedItem(it.name, Date(it.lastModified()), ImageButton(context), Uri.fromFile(it)) val completedItem = CompletedItem(it.name, Date(it.lastModified()), ImageButton(context), ImageButton(context), Uri.fromFile(it))
mCompletedList.add(completedItem) mCompletedList.add(completedItem)
} }
} }

View File

@ -26,8 +26,6 @@ class CompletedFragment : Fragment() {
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View { ): View {
val dashboardViewModel = 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
@ -36,10 +34,6 @@ class CompletedFragment : Fragment() {
completedRecycler?.layoutManager = LinearLayoutManager(binding.root.context) completedRecycler?.layoutManager = LinearLayoutManager(binding.root.context)
MainActivity.completedAdapter.refreshList(binding.root.context) MainActivity.completedAdapter.refreshList(binding.root.context)
dashboardViewModel.text.observe(viewLifecycleOwner) {
//do nothing
}
return binding.root return binding.root
} }

View File

@ -4,4 +4,4 @@ import android.net.Uri
import android.widget.ImageButton import android.widget.ImageButton
import java.util.* import java.util.*
data class CompletedItem(val filename: String, val date: Date, val button: ImageButton, val uri: Uri) data class CompletedItem(val filename: String, val date: Date, val shareButton: ImageButton, val deleteButton: ImageButton, val uri: Uri)

View File

@ -1,13 +0,0 @@
package com.example.myapplication.ui.completed
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class CompletedViewModel : ViewModel() {
private val _text = MutableLiveData<String>().apply {
value = "This is completed Fragment"
}
val text: LiveData<String> = _text
}

View File

@ -23,8 +23,6 @@ class CompressingFragment : Fragment() {
private val binding get() = _binding!! private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
ViewModelProvider(this)[CompressingViewModel::class.java]
_binding = FragmentCompressingBinding.inflate(inflater, container, false) _binding = FragmentCompressingBinding.inflate(inflater, container, false)
@ -32,9 +30,6 @@ class CompressingFragment : Fragment() {
compressingRecycler?.adapter = MainActivity.compressingAdapter compressingRecycler?.adapter = MainActivity.compressingAdapter
compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context) compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context)
return binding.root return binding.root
} }

View File

@ -1,13 +0,0 @@
package com.example.myapplication.ui.compressing
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class CompressingViewModel : ViewModel() {
private val _text = MutableLiveData<String>().apply {
value = "This is compressing Fragment"
}
val text: LiveData<String> = _text
}

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="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2L18,7L6,7v12zM8,9h8v10L8,19L8,9zM15.5,4l-1,-1h-5l-1,1L5,4v2h14L19,4z"/>
</vector>

View File

@ -5,9 +5,9 @@
android:layout_height="wrap_content"> android:layout_height="wrap_content">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="3" android:layout_weight="5"
android:orientation="vertical"> android:orientation="vertical">
<TextView <TextView
@ -26,6 +26,14 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
app:srcCompat="@drawable/ic_share" android:contentDescription="@string/share_button"
android:contentDescription="@string/share_button" /> app:srcCompat="@drawable/ic_share" />
<ImageButton
android:id="@+id/compressed_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="@string/delete"
app:srcCompat="@drawable/ic_delete" />
</LinearLayout> </LinearLayout>

View File

@ -12,5 +12,6 @@
<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> <string name="share_button">Share</string>
<string name="delete">Delete</string>
</resources> </resources>