Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Feb 18, 2025
1 parent bac0630 commit fd945b0
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 282 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,31 @@ package com.wirelessalien.zipxtract.activity

import android.content.ClipData
import android.content.ClipboardManager
import android.content.DialogInterface
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.databinding.ActivityMainBinding
import com.wirelessalien.zipxtract.databinding.DialogCrashLogBinding
import com.wirelessalien.zipxtract.fragment.ArchiveFragment
import com.wirelessalien.zipxtract.fragment.MainFragment
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.IOException


class MainActivity : AppCompatActivity() {

private lateinit var bottomNavigationView: BottomNavigationView
private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val fileName = "Crash_Log.txt"
val crashLogFile = File(cacheDir, fileName)
Expand All @@ -61,14 +62,13 @@ class MainActivity : AppCompatActivity() {
e.printStackTrace()
}

val dialogView = layoutInflater.inflate(R.layout.dialog_crash_log, null)
val textView = dialogView.findViewById<TextView>(R.id.crash_log_text)
textView.text = crashLog.toString()
val dialogBinding = DialogCrashLogBinding.inflate(layoutInflater)
dialogBinding.crashLogText.text = crashLog.toString()

MaterialAlertDialogBuilder(this, R.style.MaterialDialog)
.setTitle(getString(R.string.crash_log))
.setView(dialogView)
.setPositiveButton(getString(R.string.copy_text)) { _: DialogInterface?, _: Int ->
.setView(dialogBinding.root)
.setPositiveButton(getString(R.string.copy_text)) { _, _ ->
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("ZipXtract Crash Log", crashLog.toString())
clipboard.setPrimaryClip(clip)
Expand All @@ -79,8 +79,7 @@ class MainActivity : AppCompatActivity() {
crashLogFile.delete()
}

bottomNavigationView = findViewById(R.id.bottomNav)
bottomNavigationView.setOnItemSelectedListener { item ->
binding.bottomNav.setOnItemSelectedListener { item ->
when (item.itemId) {
R.id.home -> {
loadFragment(MainFragment())
Expand All @@ -95,7 +94,7 @@ class MainActivity : AppCompatActivity() {
}

if (savedInstanceState == null) {
bottomNavigationView.selectedItemId = R.id.home
binding.bottomNav.selectedItemId = R.id.home
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ package com.wirelessalien.zipxtract.activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.OpenableColumns
import android.util.Log
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.textfield.TextInputEditText
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.databinding.DialogCrashLogBinding
import com.wirelessalien.zipxtract.databinding.PasswordInputOpenWithBinding
import com.wirelessalien.zipxtract.service.ExtractArchiveService
import com.wirelessalien.zipxtract.service.ExtractCsArchiveService
import com.wirelessalien.zipxtract.service.ExtractRarService
Expand All @@ -53,6 +51,7 @@ class OpenWithActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val fileName = "Crash_Log.txt"
val crashLogFile = File(cacheDir, fileName)
if (crashLogFile.exists()) {
Expand All @@ -69,14 +68,13 @@ class OpenWithActivity : AppCompatActivity() {
e.printStackTrace()
}

val dialogView = layoutInflater.inflate(R.layout.dialog_crash_log, null)
val textView = dialogView.findViewById<TextView>(R.id.crash_log_text)
textView.text = crashLog.toString()
val dialogBinding = DialogCrashLogBinding.inflate(layoutInflater)
dialogBinding.crashLogText.text = crashLog.toString()

MaterialAlertDialogBuilder(this, R.style.MaterialDialog)
.setTitle(getString(R.string.crash_log))
.setView(dialogView)
.setPositiveButton(getString(R.string.copy_text)) { _: DialogInterface?, _: Int ->
.setView(dialogBinding.root)
.setPositiveButton(getString(R.string.copy_text)) { _, _ ->
val clipboard = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("ZipXtract Crash Log", crashLog.toString())
clipboard.setPrimaryClip(clip)
Expand All @@ -97,13 +95,13 @@ class OpenWithActivity : AppCompatActivity() {
}

private fun showPasswordInputDialog(uri: Uri) {
val dialogView = layoutInflater.inflate(R.layout.password_input_open_with, null)
val passwordEditText = dialogView.findViewById<TextInputEditText>(R.id.passwordInput)
val progressBar = dialogView.findViewById<ProgressBar>(R.id.progressIndicator)
val dialogBinding = PasswordInputOpenWithBinding.inflate(layoutInflater)
val passwordEditText = dialogBinding.passwordInput
val progressBar = dialogBinding.progressIndicator

MaterialAlertDialogBuilder(this, R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
.setView(dialogView)
.setView(dialogBinding.root)
.setPositiveButton(getString(R.string.ok)) { _, _ ->
val password = passwordEditText.text.toString()
progressBar.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,38 @@ package com.wirelessalien.zipxtract.activity
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.OnBackPressedCallback
import androidx.activity.OnBackPressedDispatcher
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.appbar.MaterialToolbar
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.databinding.ActivitySettingsBinding
import com.wirelessalien.zipxtract.fragment.SettingsFragment


class SettingsActivity : AppCompatActivity() {

private lateinit var binding: ActivitySettingsBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)

// Set up the toolbar
val toolbar: MaterialToolbar = findViewById(R.id.toolbar)
toolbar.title = getString(R.string.action_settings)
setSupportActionBar(toolbar)
binding.toolbar.title = getString(R.string.action_settings)
setSupportActionBar(binding.toolbar)

// Display the fragment as the main content.
supportFragmentManager.beginTransaction()
.replace(R.id.fragmentContainer, SettingsFragment())
.replace(binding.fragmentContainer.id, SettingsFragment())
.commit()

// Add back button to the activity
val actionBar = supportActionBar
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true)
actionBar.setHomeButtonEnabled(true)
supportActionBar?.apply {
setDisplayHomeAsUpEnabled(true)
setHomeButtonEnabled(true)
}

// Handle back button press
OnBackPressedDispatcher().addCallback(this, object : OnBackPressedCallback(true) {
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
finishActivity()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.android.material.card.MaterialCardView
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.databinding.ItemFileBinding
import com.wirelessalien.zipxtract.fragment.MainFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -98,22 +95,13 @@ class FileAdapter(private val context: Context, private val mainFragment: MainFr
innerPaths
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnLongClickListener, View.OnClickListener {
val fileIcon: ImageView = itemView.findViewById(R.id.file_icon)
val fileName: TextView = itemView.findViewById(R.id.file_name)
val fileSize: TextView = itemView.findViewById(R.id.file_size)
val fileDate: TextView = itemView.findViewById(R.id.file_date)
val fileExtension: TextView = itemView.findViewById(R.id.file_extension)
val fileCheckIcon: ImageView = itemView.findViewById(R.id.check_icon)
private val fileIconCv: MaterialCardView = itemView.findViewById(R.id.card_view)
val constLayout: LinearLayout = itemView.findViewById(R.id.linear_layout)

inner class ViewHolder(val binding: ItemFileBinding) : RecyclerView.ViewHolder(binding.root), View.OnLongClickListener, View.OnClickListener {
init {
itemView.isClickable = true
itemView.isFocusable = true
itemView.setOnClickListener(this)
itemView.setOnLongClickListener(this)
fileIconCv.setOnClickListener(this)
binding.cardView.setOnClickListener(this)
}

override fun onClick(v: View?) {
Expand All @@ -138,26 +126,27 @@ class FileAdapter(private val context: Context, private val mainFragment: MainFr
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_file, parent, false)
return ViewHolder(view)
val binding = ItemFileBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val file = filteredFiles[position]
val binding = holder.binding

holder.fileName.text = file.name
binding.fileName.text = file.name

val dateFormat =
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault())
holder.fileDate.text = dateFormat.format(Date(getFileTimeOfCreation(file)))
binding.fileDate.text = dateFormat.format(Date(getFileTimeOfCreation(file)))

if (file.isDirectory) {
holder.fileIcon.setImageResource(R.drawable.ic_folder)
holder.fileSize.text = context.getString(R.string.folder)
holder.fileIcon.visibility = View.VISIBLE
holder.fileExtension.visibility = View.GONE
binding.fileIcon.setImageResource(R.drawable.ic_folder)
binding.fileSize.text = context.getString(R.string.folder)
binding.fileIcon.visibility = View.VISIBLE
binding.fileExtension.visibility = View.GONE
} else {
holder.fileSize.text = bytesToString(file.length())
binding.fileSize.text = bytesToString(file.length())

when (file.extension.lowercase(Locale.getDefault())) {
"png", "jpg", "bmp", "jpeg", "gif", "webp" -> {
Expand All @@ -168,21 +157,21 @@ class FileAdapter(private val context: Context, private val mainFragment: MainFr
Glide.with(context)
.load(file)
.thumbnail(requestBuilder)
.into(holder.fileIcon)
holder.fileIcon.visibility = View.VISIBLE
holder.fileExtension.visibility = View.GONE
.into(binding.fileIcon)
binding.fileIcon.visibility = View.VISIBLE
binding.fileExtension.visibility = View.GONE
}
else -> {
holder.fileIcon.visibility = View.GONE
holder.fileExtension.visibility = View.VISIBLE
holder.fileExtension.text = if (file.extension.isNotEmpty()) {
binding.fileIcon.visibility = View.GONE
binding.fileExtension.visibility = View.VISIBLE
binding.fileExtension.text = if (file.extension.isNotEmpty()) {
if (file.extension.length > 4) {
"FILE"
} else {
if (file.extension.length == 4) {
holder.fileExtension.textSize = 16f
binding.fileExtension.textSize = 16f
} else {
holder.fileExtension.textSize = 18f
binding.fileExtension.textSize = 18f
}
file.extension.uppercase(Locale.getDefault())
}
Expand All @@ -194,12 +183,11 @@ class FileAdapter(private val context: Context, private val mainFragment: MainFr
}

if (selectedItems.get(position, false)) {
holder.fileCheckIcon.visibility = View.VISIBLE
holder.constLayout.setBackgroundColor(context.getColor(R.color.md_theme_primary_90))
binding.checkIcon.visibility = View.VISIBLE
binding.linearLayout.setBackgroundColor(context.getColor(R.color.md_theme_primary_90))
} else {
holder.fileCheckIcon.visibility = View.GONE
holder.constLayout.setBackgroundColor(context.getColor(R.color.md_theme_surface))

binding.checkIcon.visibility = View.GONE
binding.linearLayout.setBackgroundColor(context.getColor(R.color.md_theme_surface))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,45 @@ package com.wirelessalien.zipxtract.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.wirelessalien.zipxtract.R
import com.wirelessalien.zipxtract.databinding.ListItemFileBinding
import java.io.File

class FilePathAdapter(private val filePaths: MutableList<String>, private val onDeleteClick: (String) -> Unit) :
RecyclerView.Adapter<FilePathAdapter.ViewHolder>() {

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val filePathText: TextView = itemView.findViewById(R.id.textFileName)
val deleteButton: Button = itemView.findViewById(R.id.deleteBtn)
inner class ViewHolder(private val binding: ListItemFileBinding) : RecyclerView.ViewHolder(binding.root) {

init {
deleteButton.setOnClickListener {
binding.deleteBtn.setOnClickListener {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
val filePath = filePaths[position]
onDeleteClick(filePath)
}
}
}

fun bind(filePath: String) {
binding.textFileName.text = filePath

val file = File(filePath)
if (file.isDirectory) {
binding.deleteBtn.visibility = View.GONE
} else {
binding.deleteBtn.visibility = View.VISIBLE
}
}
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item_file, parent, false)
return ViewHolder(view)
val binding = ListItemFileBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val filePath = filePaths[position]
holder.filePathText.text = filePath

val file = File(filePath)
if (file.isDirectory) {
holder.deleteButton.visibility = View.GONE
} else {
holder.deleteButton.visibility = View.VISIBLE
}
holder.bind(filePath)
}

override fun getItemCount(): Int {
Expand Down
Loading

0 comments on commit fd945b0

Please sign in to comment.