Align with code style and remove unnecessary code

This commit is contained in:
Willi Ye 2020-07-21 20:09:59 +02:00 committed by ◱ PixelyIon
parent c3e54d1abf
commit b86aac26d7
4 changed files with 24 additions and 51 deletions

View File

@ -51,14 +51,14 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
/** /**
* This adds all files in [directory] with [extension] as an entry in [adapter] using [loader] to load metadata * This adds all files in [directory] with [extension] as an entry in [adapter] using [loader] to load metadata
*/ */
private fun addEntries(romFormat : RomFormat, directory : DocumentFile, found : Boolean = false) : Boolean { private fun addEntries(extension : String, romFormat : RomFormat, directory : DocumentFile, found : Boolean = false) : Boolean {
var foundCurrent = found var foundCurrent = found
directory.listFiles().forEach { file -> directory.listFiles().forEach { file ->
if (file.isDirectory) { if (file.isDirectory) {
foundCurrent = addEntries(romFormat, file, foundCurrent) foundCurrent = addEntries(extension, romFormat, file, foundCurrent)
} else { } else {
if (romFormat.extension.equals(file.name?.substringAfterLast("."), ignoreCase = true)) { if (extension.equals(file.name?.substringAfterLast("."), ignoreCase = true)) {
val romFd = contentResolver.openFileDescriptor(file.uri, "r")!! val romFd = contentResolver.openFileDescriptor(file.uri, "r")!!
val romFile = RomFile(this, romFormat, romFd) val romFile = RomFile(this, romFormat, romFd)
@ -113,15 +113,13 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
val searchLocation = DocumentFile.fromTreeUri(this, Uri.parse(sharedPreferences.getString("search_location", "")))!! val searchLocation = DocumentFile.fromTreeUri(this, Uri.parse(sharedPreferences.getString("search_location", "")))!!
var foundRoms = addEntries(RomFormat.NRO, searchLocation) var foundRoms = addEntries("nro", RomFormat.NRO, searchLocation)
foundRoms = foundRoms or addEntries(RomFormat.NSO, searchLocation) foundRoms = foundRoms or addEntries("nso", RomFormat.NSO, searchLocation)
foundRoms = foundRoms or addEntries(RomFormat.NCA, searchLocation) foundRoms = foundRoms or addEntries("nca", RomFormat.NCA, searchLocation)
foundRoms = foundRoms or addEntries(RomFormat.NSP, searchLocation) foundRoms = foundRoms or addEntries("nsp", RomFormat.NSP, searchLocation)
runOnUiThread { runOnUiThread {
if (!foundRoms) { if (!foundRoms) adapter.addHeader(getString(R.string.no_rom))
adapter.addHeader(getString(R.string.no_rom))
}
try { try {
adapter.save(File("${applicationInfo.dataDir}/roms.bin")) adapter.save(File("${applicationInfo.dataDir}/roms.bin"))
@ -204,11 +202,10 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
app_list.layoutManager = when (layoutType) { app_list.layoutManager = when (layoutType) {
LayoutType.List -> LinearLayoutManager(this).also { app_list.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL)) } LayoutType.List -> LinearLayoutManager(this).also { app_list.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL)) }
LayoutType.Grid, LayoutType.GridCompact -> GridLayoutManager(this, gridSpan).apply { LayoutType.Grid, LayoutType.GridCompact -> GridLayoutManager(this, gridSpan).apply {
spanSizeLookup = GridLayoutSpan(adapter, gridSpan).also { spanSizeLookup = GridLayoutSpan(adapter, gridSpan).also {
if (app_list.itemDecorationCount > 0) { if (app_list.itemDecorationCount > 0) app_list.removeItemDecorationAt(0)
app_list.removeItemDecorationAt(0)
}
} }
} }
} }

View File

@ -9,7 +9,6 @@ import android.app.Dialog
import android.content.Context import android.content.Context
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@ -33,13 +32,12 @@ enum class LayoutType(val layoutRes : Int) {
GridCompact(R.layout.app_item_grid_compact) GridCompact(R.layout.app_item_grid_compact)
} }
private typealias AppInteractionFunc = (appItem : AppItem) -> Unit private typealias InteractionFunction = (appItem : AppItem) -> Unit
/** /**
* This adapter is used to display all found applications using their metadata * This adapter is used to display all found applications using their metadata
*/ */
internal class AppAdapter(val layoutType : LayoutType, private val gridSpan : Int, private val onClick : AppInteractionFunc, private val onLongClick : AppInteractionFunc) : HeaderAdapter<AppItem, BaseHeader, RecyclerView.ViewHolder>() { internal class AppAdapter(val layoutType : LayoutType, private val gridSpan : Int, private val onClick : InteractionFunction, private val onLongClick : InteractionFunction) : HeaderAdapter<AppItem, BaseHeader, RecyclerView.ViewHolder>() {
private lateinit var context : Context private lateinit var context : Context
private val missingIcon by lazy { ContextCompat.getDrawable(context, R.drawable.default_icon)!!.toBitmap(256, 256) } private val missingIcon by lazy { ContextCompat.getDrawable(context, R.drawable.default_icon)!!.toBitmap(256, 256) }
private val missingString by lazy { context.getString(R.string.metadata_missing) } private val missingString by lazy { context.getString(R.string.metadata_missing) }
@ -78,38 +76,20 @@ internal class AppAdapter(val layoutType : LayoutType, private val gridSpan : In
val inflater = LayoutInflater.from(context) val inflater = LayoutInflater.from(context)
val view = when (ElementType.values()[viewType]) { val view = when (ElementType.values()[viewType]) {
ElementType.Item -> inflater.inflate(layoutType.layoutRes, parent, false) ElementType.Item -> inflater.inflate(layoutType.layoutRes, parent, false)
ElementType.Header -> inflater.inflate(R.layout.section_item, parent, false) ElementType.Header -> inflater.inflate(R.layout.section_item, parent, false)
} }
Log.i("blaa", "onCreateViewHolder")
return when (ElementType.values()[viewType]) { return when (ElementType.values()[viewType]) {
ElementType.Item -> { ElementType.Item -> {
ItemViewHolder(view, view.findViewById(R.id.icon), view.findViewById(R.id.text_title), view.findViewById(R.id.text_subtitle)).apply { ItemViewHolder(view, view.findViewById(R.id.icon), view.findViewById(R.id.text_title), view.findViewById(R.id.text_subtitle)).apply {
if (layoutType == LayoutType.List) { if (layoutType == LayoutType.Grid || layoutType == LayoutType.GridCompact) {
view.apply {
if (context is View.OnClickListener) {
setOnClickListener(context as View.OnClickListener)
}
if (context is View.OnLongClickListener) {
setOnLongClickListener(context as View.OnLongClickListener)
}
}
} else {
card = view.findViewById(R.id.app_item_grid) card = view.findViewById(R.id.app_item_grid)
card!!.apply {
if (context is View.OnClickListener) {
setOnClickListener(context as View.OnClickListener)
}
if (context is View.OnLongClickListener) {
setOnLongClickListener(context as View.OnLongClickListener)
}
}
title.isSelected = true title.isSelected = true
} }
} }
} }
ElementType.Header -> { ElementType.Header -> {
HeaderViewHolder(view).apply { HeaderViewHolder(view).apply {
header = view.findViewById(R.id.text_title) header = view.findViewById(R.id.text_title)
@ -146,7 +126,6 @@ internal class AppAdapter(val layoutType : LayoutType, private val gridSpan : In
// Increase margin of edges to avoid huge gap in between items // Increase margin of edges to avoid huge gap in between items
if (layoutType == LayoutType.Grid || layoutType == LayoutType.GridCompact) { if (layoutType == LayoutType.Grid || layoutType == LayoutType.GridCompact) {
holder.itemView.layoutParams = LinearLayout.LayoutParams(holder.itemView.layoutParams.width, holder.itemView.layoutParams.height).apply { holder.itemView.layoutParams = LinearLayout.LayoutParams(holder.itemView.layoutParams.width, holder.itemView.layoutParams.height).apply {
if (position % gridSpan == 0) { if (position % gridSpan == 0) {
marginStart = holder.itemView.resources.getDimensionPixelSize(R.dimen.app_card_margin) * 2 marginStart = holder.itemView.resources.getDimensionPixelSize(R.dimen.app_card_margin) * 2

View File

@ -79,12 +79,9 @@ internal class LogAdapter internal constructor(val context : Context, val compac
val inflater = LayoutInflater.from(context) val inflater = LayoutInflater.from(context)
val view = when (ElementType.values()[viewType]) { val view = when (ElementType.values()[viewType]) {
ElementType.Item -> { ElementType.Item -> inflater.inflate(if (compact) R.layout.log_item_compact else R.layout.log_item, parent, false)
inflater.inflate(if (compact) R.layout.log_item_compact else R.layout.log_item, parent, false)
} ElementType.Header -> inflater.inflate(R.layout.log_item, parent, false)
ElementType.Header -> {
inflater.inflate(R.layout.log_item, parent, false)
}
} }
return when (ElementType.values()[viewType]) { return when (ElementType.values()[viewType]) {

View File

@ -22,12 +22,12 @@ import java.util.*
/** /**
* An enumeration of all supported ROM formats * An enumeration of all supported ROM formats
*/ */
enum class RomFormat(val extension : String) { enum class RomFormat(val format : Int) {
NRO("nro"), NRO(0),
NSO("nso"), NSO(1),
NCA("nca"), NCA(2),
XCI("xci"), XCI(3),
NSP("nsp"), NSP(4),
} }
/** /**