Make controller layout more preferences like

This commit is contained in:
Willi Ye 2021-03-29 17:58:54 +02:00 committed by ◱ Mark
parent 4b7cb176f6
commit e50f5a5d72
8 changed files with 147 additions and 104 deletions

View File

@ -282,7 +282,7 @@ class MainActivity : AppCompatActivity() {
} }
private fun loadRoms(loadFromFile : Boolean) { private fun loadRoms(loadFromFile : Boolean) {
viewModel.loadRoms(this, loadFromFile, Uri.parse(settings.searchLocation)) viewModel.loadRoms(loadFromFile, Uri.parse(settings.searchLocation))
settings.refreshRequired = false settings.refreshRequired = false
} }

View File

@ -1,13 +1,12 @@
package emu.skyline package emu.skyline
import android.app.Application
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import androidx.lifecycle.LiveData import androidx.lifecycle.*
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import emu.skyline.loader.AppEntry import emu.skyline.loader.AppEntry
import emu.skyline.loader.RomFormat import emu.skyline.loader.RomFormat
import emu.skyline.utils.fromFile import emu.skyline.utils.fromFile
@ -26,7 +25,7 @@ sealed class MainState {
} }
@HiltViewModel @HiltViewModel
class MainViewModel @Inject constructor(private val romProvider : RomProvider) : ViewModel() { class MainViewModel @Inject constructor(@ApplicationContext context : Context, private val romProvider : RomProvider) : AndroidViewModel(context as Application) {
companion object { companion object {
private val TAG = MainViewModel::class.java.simpleName private val TAG = MainViewModel::class.java.simpleName
} }
@ -44,11 +43,11 @@ class MainViewModel @Inject constructor(private val romProvider : RomProvider) :
* *
* @param loadFromFile If this is false then trying to load cached adapter data is skipped entirely * @param loadFromFile If this is false then trying to load cached adapter data is skipped entirely
*/ */
fun loadRoms(context : Context, loadFromFile : Boolean, searchLocation : Uri) { fun loadRoms(loadFromFile : Boolean, searchLocation : Uri) {
if (state == MainState.Loading) return if (state == MainState.Loading) return
state = MainState.Loading state = MainState.Loading
val romsFile = File(context.filesDir.canonicalPath + "/roms.bin") val romsFile = File(getApplication<SkylineApplication>().filesDir.canonicalPath + "/roms.bin")
viewModelScope.launch(Dispatchers.IO) { viewModelScope.launch(Dispatchers.IO) {
if (loadFromFile && romsFile.exists()) { if (loadFromFile && romsFile.exists()) {

View File

@ -0,0 +1,24 @@
package emu.skyline.adapter.controller
import android.view.ViewGroup
import emu.skyline.adapter.GenericListItem
import emu.skyline.adapter.GenericViewHolder
import emu.skyline.adapter.ViewBindingFactory
import emu.skyline.adapter.inflater
import emu.skyline.databinding.ControllerHeaderBinding
object ControllerHeaderBindingFactory : ViewBindingFactory {
override fun createBinding(parent : ViewGroup) = ControllerHeaderBinding.inflate(parent.inflater(), parent, false)
}
class ControllerHeaderItem(private val text : String) : GenericListItem<ControllerHeaderBinding>() {
override fun getViewBindingFactory() = ControllerHeaderBindingFactory
override fun bind(holder : GenericViewHolder<ControllerHeaderBinding>, position : Int) {
holder.binding.root.text = text
}
override fun areItemsTheSame(other : GenericListItem<ControllerHeaderBinding>) = other is ControllerHeaderItem
override fun areContentsTheSame(other : GenericListItem<ControllerHeaderBinding>) = other is ControllerHeaderItem && text == other.text
}

View File

@ -16,7 +16,6 @@ import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.R import emu.skyline.R
import emu.skyline.adapter.GenericAdapter import emu.skyline.adapter.GenericAdapter
import emu.skyline.adapter.GenericListItem import emu.skyline.adapter.GenericListItem
import emu.skyline.adapter.HeaderViewItem
import emu.skyline.adapter.controller.* import emu.skyline.adapter.controller.*
import emu.skyline.databinding.ControllerActivityBinding import emu.skyline.databinding.ControllerActivityBinding
import emu.skyline.input.dialog.ButtonDialog import emu.skyline.input.dialog.ButtonDialog
@ -38,9 +37,6 @@ class ControllerActivity : AppCompatActivity() {
*/ */
val id by lazy { intent.getIntExtra("index", 0) } val id by lazy { intent.getIntExtra("index", 0) }
/**
* The adapter used by [controller_list] to hold all the items
*/
private val adapter = GenericAdapter() private val adapter = GenericAdapter()
/** /**
@ -74,7 +70,7 @@ class ControllerActivity : AppCompatActivity() {
return return
if (id == 0) { if (id == 0) {
items.add(HeaderViewItem(getString(R.string.osc))) items.add(ControllerHeaderItem(getString(R.string.osc)))
val oscSummary = { checked : Boolean -> getString(if (checked) R.string.osc_shown else R.string.osc_not_shown) } val oscSummary = { checked : Boolean -> getString(if (checked) R.string.osc_shown else R.string.osc_not_shown) }
items.add(ControllerCheckBoxViewItem(getString(R.string.osc_enable), oscSummary.invoke(settings.onScreenControl), settings.onScreenControl) { item, position -> items.add(ControllerCheckBoxViewItem(getString(R.string.osc_enable), oscSummary.invoke(settings.onScreenControl), settings.onScreenControl) { item, position ->
@ -98,7 +94,7 @@ class ControllerActivity : AppCompatActivity() {
for (item in GeneralType.values()) { for (item in GeneralType.values()) {
if (item.compatibleControllers == null || item.compatibleControllers.contains(controller.type)) { if (item.compatibleControllers == null || item.compatibleControllers.contains(controller.type)) {
if (!wroteTitle) { if (!wroteTitle) {
items.add(HeaderViewItem(getString(R.string.general))) items.add(ControllerHeaderItem(getString(R.string.general)))
wroteTitle = true wroteTitle = true
} }
@ -110,7 +106,7 @@ class ControllerActivity : AppCompatActivity() {
for (stick in controller.type.sticks) { for (stick in controller.type.sticks) {
if (!wroteTitle) { if (!wroteTitle) {
items.add(HeaderViewItem(getString(R.string.sticks))) items.add(ControllerHeaderItem(getString(R.string.sticks)))
wroteTitle = true wroteTitle = true
} }
@ -134,7 +130,7 @@ class ControllerActivity : AppCompatActivity() {
for (button in controller.type.buttons.filter { it in buttonArray.second }) { for (button in controller.type.buttons.filter { it in buttonArray.second }) {
if (!wroteTitle) { if (!wroteTitle) {
items.add(HeaderViewItem(getString(buttonArray.first))) items.add(ControllerHeaderItem(getString(buttonArray.first)))
wroteTitle = true wroteTitle = true
} }
@ -149,7 +145,7 @@ class ControllerActivity : AppCompatActivity() {
for (button in controller.type.buttons.filterNot { item -> buttonArrays.any { item in it.second } }.plus(ButtonId.Menu)) { for (button in controller.type.buttons.filterNot { item -> buttonArrays.any { item in it.second } }.plus(ButtonId.Menu)) {
if (!wroteTitle) { if (!wroteTitle) {
items.add(HeaderViewItem(getString(R.string.misc_buttons))) items.add(ControllerHeaderItem(getString(R.string.misc_buttons)))
wroteTitle = true wroteTitle = true
} }

View File

@ -14,5 +14,6 @@
android:id="@+id/controller_list" android:id="@+id/controller_list"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior" /> app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>

View File

@ -6,12 +6,15 @@
android:background="?attr/selectableItemBackground" android:background="?attr/selectableItemBackground"
android:clickable="true" android:clickable="true"
android:focusable="true" android:focusable="true"
android:orientation="horizontal"> android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:orientation="horizontal"
android:paddingStart="72dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">
<LinearLayout <LinearLayout
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:layout_weight="1" android:layout_weight="1"
@ -21,6 +24,8 @@
android:id="@+id/text_title" android:id="@+id/text_title"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem" android:textAppearance="?android:attr/textAppearanceListItem"
tools:text="Title" /> tools:text="Title" />
@ -28,8 +33,9 @@
android:id="@+id/text_subtitle" android:id="@+id/text_subtitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:maxLines="10"
android:textAppearance="?android:attr/textAppearanceListItemSecondary" android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:textColor="@android:color/tertiary_text_light" android:textColor="?android:attr/textColorSecondary"
tools:text="Summary" /> tools:text="Summary" />
</LinearLayout> </LinearLayout>
@ -37,6 +43,8 @@
android:id="@+id/checkbox" android:id="@+id/checkbox"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:clickable="false"
android:clickable="false" /> android:gravity="end|center_vertical"
android:paddingStart="16dp"
android:paddingEnd="0dp" />
</LinearLayout> </LinearLayout>

View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:paddingStart="72dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="?attr/colorAccent"
tools:text="Header" />

View File

@ -1,31 +1,34 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/controller_item" android:id="@+id/controller_item"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" android:background="?android:attr/selectableItemBackground"
android:baselineAligned="false"
android:clickable="true" android:clickable="true"
android:clipToPadding="false"
android:focusable="true" android:focusable="true"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:orientation="vertical" android:orientation="vertical"
android:paddingVertical="8dp" android:paddingStart="72dp"
android:paddingHorizontal="16dp"> android:paddingTop="16dp"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:paddingBottom="16dp">
<TextView <TextView
android:id="@+id/text_title" android:id="@+id/text_title"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentStart="true" android:ellipsize="marquee"
android:layout_alignParentTop="false" android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceListItem" android:textAppearance="?android:attr/textAppearanceListItem" />
tools:ignore="RelativeOverlap" />
<TextView <TextView
android:id="@+id/text_subtitle" android:id="@+id/text_subtitle"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/text_title" android:maxLines="10"
android:layout_alignStart="@id/text_title"
android:textAppearance="?android:attr/textAppearanceListItemSecondary" android:textAppearance="?android:attr/textAppearanceListItemSecondary"
android:textColor="@android:color/tertiary_text_light" /> android:textColor="?android:attr/textColorSecondary" />
</RelativeLayout> </LinearLayout>