Clean up code

This commit is contained in:
Isaac Shoebottom 2022-12-06 22:52:17 -04:00
parent e68a984411
commit 9d4da6a410
9 changed files with 120 additions and 84 deletions

View File

@ -46,7 +46,8 @@ class MainActivity : AppCompatActivity() {
// VERIFY PERMISSIONS
val permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
val permission =
ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
@ -63,8 +64,11 @@ class MainActivity : AppCompatActivity() {
val navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_compressing, R.id.navigation_completed,R.id.navigation_settings))
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_compressing, R.id.navigation_completed, R.id.navigation_settings
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
@ -81,7 +85,6 @@ class MainActivity : AppCompatActivity() {
return when (item.itemId) {
//runs when pressing "Files"
R.id.addFile -> {
val intent = Intent()
@ -93,12 +96,14 @@ class MainActivity : AppCompatActivity() {
resultLauncher.launch(intent)
//Toast.makeText(applicationContext, "Files", Toast.LENGTH_LONG).show()
return true
}
R.id.addYoutube -> {
Toast.makeText(applicationContext, "Youtube downloading is currently not available", Toast.LENGTH_LONG).show()
Toast.makeText(
applicationContext,
"Youtube downloading is currently not available",
Toast.LENGTH_LONG
).show()
return true
}
else -> super.onOptionsItemSelected(item)
@ -107,7 +112,8 @@ class MainActivity : AppCompatActivity() {
//grabs output from pressing files, used for grabbing URI
@SuppressLint("NotifyDataSetChanged") // Needed because of custom adapter
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
private var resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
// There are no request codes
@ -131,7 +137,8 @@ class MainActivity : AppCompatActivity() {
val mmr = MediaMetadataRetriever()
mmr.setDataSource(this, data)
val duration =
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toDouble()
mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
?.toDouble()
Log.i("ethan", duration.toString())
Log.i("ethan", settingsViewModel.getSize().toString())
@ -153,7 +160,11 @@ class MainActivity : AppCompatActivity() {
completedAdapter.refreshList(this)
handler.post {
Toast.makeText(this, "Finished converting $fileName", Toast.LENGTH_SHORT).show()
Toast.makeText(
this,
"Finished converting $fileName",
Toast.LENGTH_SHORT
).show()
compressingAdapter.notifyDataSetChanged()
}
}
@ -165,7 +176,11 @@ class MainActivity : AppCompatActivity() {
compressingAdapter.notifyDataSetChanged()
} 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

@ -17,7 +17,8 @@ import com.example.myapplication.R
import java.util.*
class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>): RecyclerView.Adapter<CompletedAdapter.ViewHolder>() {
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)
@ -26,7 +27,8 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressed, parent, false)
val compressingItemView =
LayoutInflater.from(parent.context).inflate(R.layout.item_compressed, parent, false)
return ViewHolder(compressingItemView)
}
@ -34,8 +36,18 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
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) }
holder.deleteButton.setOnClickListener { deleteFile(holder.itemView.context, compressingItem.uri) }
holder.shareButton.setOnClickListener {
shareFile(
holder.itemView.context,
compressingItem.uri
)
}
holder.deleteButton.setOnClickListener {
deleteFile(
holder.itemView.context,
compressingItem.uri
)
}
}
@ -77,7 +89,13 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
mCompletedList.clear()
context.getExternalFilesDir(null)?.listFiles()?.forEach {
if (it.name.endsWith(".mp4")) {
val completedItem = CompletedItem(it.name, Date(it.lastModified()), ImageButton(context), ImageButton(context), Uri.fromFile(it))
val completedItem = CompletedItem(
it.name,
Date(it.lastModified()),
ImageButton(context),
ImageButton(context),
Uri.fromFile(it)
)
mCompletedList.add(completedItem)
}
}

View File

@ -4,9 +4,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
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
@ -29,7 +27,8 @@ class CompletedFragment : Fragment() {
_binding = FragmentCompletedBinding.inflate(inflater, container, false)
val root: View = binding.root
val completedRecycler = binding.root.findViewById<View>(R.id.compressed_recycler_view) as? RecyclerView
val completedRecycler =
binding.root.findViewById<View>(R.id.compressed_recycler_view) as? RecyclerView
completedRecycler?.adapter = MainActivity.completedAdapter
completedRecycler?.layoutManager = LinearLayoutManager(binding.root.context)

View File

@ -4,4 +4,10 @@ import android.net.Uri
import android.widget.ImageButton
import java.util.*
data class CompletedItem(val filename: String, val date: Date, val shareButton: ImageButton, val deleteButton: 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,6 +1,5 @@
package com.example.myapplication.ui.compressing
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -8,10 +7,9 @@ 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<CompressingItem>): RecyclerView.Adapter<CompressingAdapter.ViewHolder>() {
class CompressingAdapter(private val mCompressingItems: MutableList<CompressingItem>) :
RecyclerView.Adapter<CompressingAdapter.ViewHolder>() {
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)
@ -19,7 +17,8 @@ class CompressingAdapter(private val mCompressingItems: MutableList<CompressingI
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val compressingItemView = LayoutInflater.from(parent.context).inflate(R.layout.item_compressing, parent, false)
val compressingItemView =
LayoutInflater.from(parent.context).inflate(R.layout.item_compressing, parent, false)
return ViewHolder(compressingItemView)
}

View File

@ -5,7 +5,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.MainActivity
@ -17,16 +16,20 @@ class CompressingFragment : Fragment() {
private var _binding: FragmentCompressingBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
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 {
_binding = FragmentCompressingBinding.inflate(inflater, container, false)
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.compressingAdapter
compressingRecycler?.layoutManager = LinearLayoutManager(binding.root.context)

View File

@ -1,5 +1,5 @@
package com.example.myapplication.ui.compressing
import java.util.Date
import java.util.*
data class CompressingItem(val filename: String, val progress: Double, val date: Date)

View File

@ -56,9 +56,6 @@ class SettingsFragment : Fragment() {
settingsViewModel.size.value = 0.0
}
}
Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show()
}
//updates size when the custom value changes
@ -77,7 +74,6 @@ class SettingsFragment : Fragment() {
}catch (e: NumberFormatException){
settingsViewModel.size.value = 0.0
}
Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show()
}
}
)