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
*/
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
directory.listFiles().forEach { file ->
if (file.isDirectory) {
foundCurrent = addEntries(romFormat, file, foundCurrent)
foundCurrent = addEntries(extension, romFormat, file, foundCurrent)
} 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 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", "")))!!
var foundRoms = addEntries(RomFormat.NRO, searchLocation)
foundRoms = foundRoms or addEntries(RomFormat.NSO, searchLocation)
foundRoms = foundRoms or addEntries(RomFormat.NCA, searchLocation)
foundRoms = foundRoms or addEntries(RomFormat.NSP, searchLocation)
var foundRoms = addEntries("nro", RomFormat.NRO, searchLocation)
foundRoms = foundRoms or addEntries("nso", RomFormat.NSO, searchLocation)
foundRoms = foundRoms or addEntries("nca", RomFormat.NCA, searchLocation)
foundRoms = foundRoms or addEntries("nsp", RomFormat.NSP, searchLocation)
runOnUiThread {
if (!foundRoms) {
adapter.addHeader(getString(R.string.no_rom))
}
if (!foundRoms) adapter.addHeader(getString(R.string.no_rom))
try {
adapter.save(File("${applicationInfo.dataDir}/roms.bin"))
@ -204,11 +202,10 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
app_list.layoutManager = when (layoutType) {
LayoutType.List -> LinearLayoutManager(this).also { app_list.addItemDecoration(DividerItemDecoration(this, RecyclerView.VERTICAL)) }
LayoutType.Grid, LayoutType.GridCompact -> GridLayoutManager(this, gridSpan).apply {
spanSizeLookup = GridLayoutSpan(adapter, gridSpan).also {
if (app_list.itemDecorationCount > 0) {
app_list.removeItemDecorationAt(0)
}
if (app_list.itemDecorationCount > 0) app_list.removeItemDecorationAt(0)
}
}
}

View File

@ -9,7 +9,6 @@ import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@ -33,13 +32,12 @@ enum class LayoutType(val layoutRes : Int) {
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
*/
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 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) }
@ -78,38 +76,20 @@ internal class AppAdapter(val layoutType : LayoutType, private val gridSpan : In
val inflater = LayoutInflater.from(context)
val view = when (ElementType.values()[viewType]) {
ElementType.Item -> inflater.inflate(layoutType.layoutRes, parent, false)
ElementType.Header -> inflater.inflate(R.layout.section_item, parent, false)
}
Log.i("blaa", "onCreateViewHolder")
return when (ElementType.values()[viewType]) {
ElementType.Item -> {
ItemViewHolder(view, view.findViewById(R.id.icon), view.findViewById(R.id.text_title), view.findViewById(R.id.text_subtitle)).apply {
if (layoutType == LayoutType.List) {
view.apply {
if (context is View.OnClickListener) {
setOnClickListener(context as View.OnClickListener)
}
if (context is View.OnLongClickListener) {
setOnLongClickListener(context as View.OnLongClickListener)
}
}
} else {
if (layoutType == LayoutType.Grid || layoutType == LayoutType.GridCompact) {
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
}
}
}
ElementType.Header -> {
HeaderViewHolder(view).apply {
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
if (layoutType == LayoutType.Grid || layoutType == LayoutType.GridCompact) {
holder.itemView.layoutParams = LinearLayout.LayoutParams(holder.itemView.layoutParams.width, holder.itemView.layoutParams.height).apply {
if (position % gridSpan == 0) {
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 view = when (ElementType.values()[viewType]) {
ElementType.Item -> {
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.Item -> 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)
}
return when (ElementType.values()[viewType]) {

View File

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