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

@ -17,7 +17,8 @@ import com.example.myapplication.R
import java.util.* 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) { 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)
@ -26,7 +27,8 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
} }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { 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) return ViewHolder(compressingItemView)
} }
@ -34,8 +36,18 @@ class CompletedAdapter(private val mCompletedList: MutableList<CompletedItem>):
val compressingItem: CompletedItem = mCompletedList[position] val compressingItem: CompletedItem = mCompletedList[position]
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 {
holder.deleteButton.setOnClickListener { deleteFile(holder.itemView.context, compressingItem.uri) } 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() 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), 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

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

View File

@ -4,4 +4,10 @@ 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 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 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
@ -8,10 +7,9 @@ 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) {
val filename: TextView = itemView.findViewById(R.id.compressing_filename) val filename: TextView = itemView.findViewById(R.id.compressing_filename)
val date: TextView = itemView.findViewById(R.id.compressing_date) 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 { 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) return ViewHolder(compressingItemView)
} }

View File

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

View File

@ -1,5 +1,5 @@
package com.example.myapplication.ui.compressing 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) 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 settingsViewModel.size.value = 0.0
} }
} }
Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show()
} }
//updates size when the custom value changes //updates size when the custom value changes
@ -77,7 +74,6 @@ class SettingsFragment : Fragment() {
}catch (e: NumberFormatException){ }catch (e: NumberFormatException){
settingsViewModel.size.value = 0.0 settingsViewModel.size.value = 0.0
} }
Toast.makeText(root.context,"" + settingsViewModel.size.value,Toast.LENGTH_SHORT).show()
} }
} }
) )