LAFChargingLookActivity.kt raw
1 package io.github.domi04151309.alwayson.activities
2
3 import android.os.Bundle
4 import android.widget.ImageView
5 import androidx.core.content.edit
6 import androidx.recyclerview.widget.LinearLayoutManager
7 import androidx.recyclerview.widget.RecyclerView
8 import io.github.domi04151309.alwayson.R
9 import io.github.domi04151309.alwayson.adapters.LayoutListAdapter
10 import io.github.domi04151309.alwayson.helpers.P
11
12 class LAFChargingLookActivity : BaseActivity(), LayoutListAdapter.OnItemClickListener {
13 internal var value: String = P.CHARGING_STYLE_DEFAULT
14 private lateinit var preview: ImageView
15 private lateinit var layoutList: RecyclerView
16
17 private val drawables =
18 arrayOf(
19 R.drawable.charging_circle,
20 R.drawable.charging_flash,
21 R.drawable.charging_ios,
22 )
23
24 private fun positionToString(position: Int): String =
25 when (position) {
26 ITEM_CIRCLE -> P.CHARGING_STYLE_CIRCLE
27 ITEM_FLASH -> P.CHARGING_STYLE_FLASH
28 ITEM_IOS -> P.CHARGING_STYLE_IOS
29 else -> P.CHARGING_STYLE_DEFAULT
30 }
31
32 private fun stringToPosition(string: String): Int =
33 when (string) {
34 P.CHARGING_STYLE_CIRCLE -> ITEM_CIRCLE
35 P.CHARGING_STYLE_FLASH -> ITEM_FLASH
36 P.CHARGING_STYLE_IOS -> ITEM_IOS
37 else -> ITEM_CIRCLE
38 }
39
40 override fun onCreate(savedInstanceState: Bundle?) {
41 super.onCreate(savedInstanceState)
42 setContentView(R.layout.activity_layout_list)
43
44 preview = findViewById(R.id.preview)
45 layoutList = findViewById(R.id.layout_list)
46
47 layoutList.layoutManager =
48 LinearLayoutManager(this).apply {
49 orientation = LinearLayoutManager.HORIZONTAL
50 }
51 layoutList.adapter =
52 LayoutListAdapter(
53 drawables,
54 resources.getStringArray(R.array.pref_look_and_feel_charging_array_display),
55 this,
56 )
57 }
58
59 override fun onItemClick(position: Int) {
60 preview.setImageResource(drawables[position])
61 value = positionToString(position)
62 }
63
64 override fun onStart() {
65 super.onStart()
66 value = P.getPreferences(this).getString(P.CHARGING_STYLE, P.CHARGING_STYLE_DEFAULT)
67 ?: P.CHARGING_STYLE_DEFAULT
68 setSelectedItem(layoutList.adapter as LayoutListAdapter, stringToPosition(value))
69 }
70
71 override fun onStop() {
72 super.onStop()
73 P.getPreferences(this).edit { putString(P.CHARGING_STYLE, value) }
74 }
75
76 private fun setSelectedItem(
77 adapter: LayoutListAdapter,
78 position: Int,
79 ) {
80 preview.setImageResource(drawables[position])
81 adapter.setSelectedItem(position)
82 }
83
84 companion object {
85 private const val ITEM_CIRCLE = 0
86 private const val ITEM_FLASH = 1
87 private const val ITEM_IOS = 2
88 }
89 }
90