From d731616349f59c83e88f5ca4937251dab10ef9de Mon Sep 17 00:00:00 2001 From: SrMouraSilva Date: Sun, 18 Feb 2018 23:22:26 -0300 Subject: [PATCH 01/41] Add KeyboardEvent support --- .../io/github/pedalpi/pedalpi_display/MainActivity.kt | 10 +++++++++- .../communication/message/response/ResponseVerb.kt | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt index 79d0f32..22cc3be 100644 --- a/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt @@ -1,5 +1,6 @@ package io.github.pedalpi.pedalpi_display +import android.app.Instrumentation import android.content.Context import android.os.Bundle import android.support.v7.app.AppCompatActivity @@ -57,7 +58,14 @@ class MainActivity : AppCompatActivity() { private fun onMessage(message : ResponseMessage) { Log.i("OnMSG", message.toString()) - if (message.type == ResponseVerb.RESPONSE && message.request == Messages.CURRENT_PEDALBOARD_DATA) { + // FIXME: Converter ResponseMessage.KEYBOARD_EVENT in key event + if (message.type == ResponseVerb.KEYBOARD_EVENT && message.content["code"].string == "DOWN") { + Log.i("KEY", message.content["number"].int.toString()) + + val inst = Instrumentation() + inst.sendKeyDownUpSync(message.content["number"].int) + + } else if (message.type == ResponseVerb.RESPONSE && message.request == Messages.CURRENT_PEDALBOARD_DATA) { runOnUiThread({ val id = message.content["pedalboard"].int diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/ResponseVerb.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/ResponseVerb.kt index 73a4f7f..6834d35 100644 --- a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/ResponseVerb.kt +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/ResponseVerb.kt @@ -3,6 +3,7 @@ package io.github.pedalpi.pedalpi_display.communication.message.response enum class ResponseVerb(private val type: String) { RESPONSE("RESPONSE"), EVENT("EVENT"), + KEYBOARD_EVENT("KEYBOARD_EVENT"), ERROR("ERROR"); From def51066c1fb8a64d2ed58c5e045c39704b92f61 Mon Sep 17 00:00:00 2001 From: SrMouraSilva Date: Tue, 20 Feb 2018 13:06:49 -0300 Subject: [PATCH 02/41] #6 Create view effects. Tests #5 button support --- app/src/main/AndroidManifest.xml | 3 +- .../github/pedalpi/pedalpi_display/Data.java | 34 ++++ .../pedalpi/pedalpi_display/MainActivity.kt | 55 +++--- .../communication/MessageProcessor.java | 57 ------ .../communication/ResponseMessageProcessor.kt | 46 +++++ .../message/response/EventMessage.kt | 32 ++++ .../{ServerApplication.java => App.java} | 2 +- .../communication/server/Server.java | 8 +- .../effects/EffectsActivity.kt | 162 ++++++++++++++++++ .../effects/EffectsListItemAdapter.kt | 74 ++++++++ .../effects/EffectsListItemDTO.kt | 9 + app/src/main/res/layout/activity_effects.xml | 9 + app/src/main/res/layout/activity_main.xml | 28 ++- app/src/main/res/layout/effects_list_item.xml | 36 ++++ app/src/main/res/values/dimen.xml | 6 + 15 files changed, 463 insertions(+), 98 deletions(-) create mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/Data.java delete mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/communication/MessageProcessor.java create mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/communication/ResponseMessageProcessor.kt create mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/EventMessage.kt rename app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/{ServerApplication.java => App.java} (91%) create mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsActivity.kt create mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemAdapter.kt create mode 100644 app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemDTO.kt create mode 100644 app/src/main/res/layout/activity_effects.xml create mode 100644 app/src/main/res/layout/effects_list_item.xml create mode 100644 app/src/main/res/values/dimen.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b40d68a..519e8ca 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,7 +11,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" - android:name=".communication.server.ServerApplication"> + android:name=".communication.server.App"> @@ -19,6 +19,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/Data.java b/app/src/main/java/io/github/pedalpi/pedalpi_display/Data.java new file mode 100644 index 0000000..d181f14 --- /dev/null +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/Data.java @@ -0,0 +1,34 @@ +package io.github.pedalpi.pedalpi_display; + +import com.google.gson.JsonElement; + +public class Data { + private static Data instance; + private JsonElement currentPedalboard; + private int number; + + public static synchronized Data getInstance() { + if (instance != null) + return instance; + + Data.instance = new Data(); + + return instance; + } + + public JsonElement getCurrentPedalboard() { + return currentPedalboard; + } + + public void setCurrentPedalboard(JsonElement currentPedalboard) { + this.currentPedalboard = currentPedalboard; + } + + public int getCurrentPedalboardPosition() { + return number; + } + + public void setCurrentPedalboardPosition(int number) { + this.number = number; + } +} diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt index 22cc3be..99fdbf9 100644 --- a/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/MainActivity.kt @@ -1,19 +1,19 @@ package io.github.pedalpi.pedalpi_display -import android.app.Instrumentation import android.content.Context +import android.content.Intent import android.os.Bundle import android.support.v7.app.AppCompatActivity -import android.util.Log import android.view.WindowManager +import android.widget.Button import android.widget.TextView import com.github.salomonbrys.kotson.get -import com.github.salomonbrys.kotson.int import com.github.salomonbrys.kotson.string import io.github.pedalpi.pedalpi_display.communication.message.request.Messages import io.github.pedalpi.pedalpi_display.communication.message.response.ResponseMessage import io.github.pedalpi.pedalpi_display.communication.message.response.ResponseVerb import io.github.pedalpi.pedalpi_display.communication.server.Server +import io.github.pedalpi.pedalpi_display.effects.EffectsActivity import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper @@ -26,21 +26,10 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - /* - CalligraphyConfig.initDefault(CalligraphyConfig.Builder() - .setDefaultFontPath("fonts/BlackOpsOne-Regular.ttf") - .setFontAttrId(R.attr.fontPath) - .build() - ) - */ - window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) supportActionBar?.hide() - //this.number = findViewById(R.id.pedalboard_number) - //this.name = findViewById(R.id.pedalboard_name) - this.number = findViewById(R.id.pedalboard_number) as TextView this.name = findViewById(R.id.pedalboard_name) as TextView @@ -49,6 +38,9 @@ class MainActivity : AppCompatActivity() { Server.getInstance().setListener({ onMessage(it) }) Server.getInstance().sendBroadcast(Messages.CURRENT_PEDALBOARD_DATA) + + val button = findViewById(R.id.button) as Button + button.setOnClickListener({goToEffectsList()}) } override fun attachBaseContext(newBase: Context) { @@ -56,29 +48,26 @@ class MainActivity : AppCompatActivity() { } private fun onMessage(message : ResponseMessage) { - Log.i("OnMSG", message.toString()) - - // FIXME: Converter ResponseMessage.KEYBOARD_EVENT in key event - if (message.type == ResponseVerb.KEYBOARD_EVENT && message.content["code"].string == "DOWN") { - Log.i("KEY", message.content["number"].int.toString()) - - val inst = Instrumentation() - inst.sendKeyDownUpSync(message.content["number"].int) - - } else if (message.type == ResponseVerb.RESPONSE && message.request == Messages.CURRENT_PEDALBOARD_DATA) { - runOnUiThread({ - val id = message.content["pedalboard"].int - - number.text = if (id < 10) "0"+id else id.toString() - name.text = message.content["bank"]["pedalboards"][id]["name"].string - }) - } else if (message.type == ResponseVerb.EVENT && message.content["type"].string == "CURRENT") { + if (message.type == ResponseVerb.RESPONSE && message.request == Messages.CURRENT_PEDALBOARD_DATA + || message.type == ResponseVerb.EVENT && message.content["type"].string == "CURRENT") { runOnUiThread({ - val id = message.content["pedalboard"].int + val id = Data.getInstance().currentPedalboardPosition + val pedalboard = Data.getInstance().currentPedalboard number.text = if (id < 10) "0"+id else id.toString() - name.text = message.content["value"]["name"].string + name.text = pedalboard["name"].string }) } } + + private fun goToEffectsList() { + val intent = Intent(baseContext, EffectsActivity::class.java) + startActivity(intent) + } + + public override fun onResume() { + super.onResume() + + Server.getInstance().setListener({ onMessage(it) }) + } } diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/MessageProcessor.java b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/MessageProcessor.java deleted file mode 100644 index 1d1ad98..0000000 --- a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/MessageProcessor.java +++ /dev/null @@ -1,57 +0,0 @@ -package io.github.pedalpi.pedalpi_display.communication; - -public class MessageProcessor { - //https://github.com/SalomonBrys/Kotson#accessing-object-fields-via-property-delegates -} - -/* -public class MessageProcessor { - - public static Patch process(RequestMessage message, Patch patch) throws JSONException { - - if (message.getType() == ResponseVerb.PATCH) { - patch = new Patch(message.getContent()); - - } else if(message.getType() == ResponseVerb.EFFECT) { - int indexEffect = message.getContent().getInt("index"); - patch.getEffects().get(indexEffect).toggleStatus(); - - } else if(message.getType() == ResponseVerb.PARAM) { - JSONObject data = message.getContent(); - - int effectIndex = data.getInt("effect"); - int paramIndex = data.getInt("param"); - Double value = Parameter.prepareDoubleValue(data, "value"); - - Parameter param = patch.getEffects().get(effectIndex).getParameters().get(paramIndex); - param.setValue(value); - } - - return patch; - } - - public static RequestMessage generateEffectStatusToggled(Effect effect) { - try { - JSONObject indexNumber = new JSONObject(); - indexNumber.put("index",effect.getIndex()); - - return new RequestMessage(ResponseVerb.EFFECT, indexNumber); - } catch (JSONException e) { - return new RequestMessage(ResponseVerb.ERROR); - } - } - - public static RequestMessage generateUpdateParamValue(Effect effect, Parameter parameter){ - try { - JSONObject data = new JSONObject(); - data.put("effect", effect.getIndex()); - data.put("param", parameter.getIndex()); - data.put("value", parameter.getValue()); - - return new RequestMessage(ResponseVerb.PARAM, data); - } catch (JSONException e) { - return new RequestMessage(ResponseVerb.ERROR); - } - } -} -*/ \ No newline at end of file diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/ResponseMessageProcessor.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/ResponseMessageProcessor.kt new file mode 100644 index 0000000..aeee163 --- /dev/null +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/ResponseMessageProcessor.kt @@ -0,0 +1,46 @@ +package io.github.pedalpi.pedalpi_display.communication + +import android.app.Instrumentation +import android.util.Log +import com.github.salomonbrys.kotson.get +import com.github.salomonbrys.kotson.int +import com.github.salomonbrys.kotson.string +import io.github.pedalpi.pedalpi_display.Data +import io.github.pedalpi.pedalpi_display.communication.message.request.Messages +import io.github.pedalpi.pedalpi_display.communication.message.response.EventMessage +import io.github.pedalpi.pedalpi_display.communication.message.response.EventType +import io.github.pedalpi.pedalpi_display.communication.message.response.ResponseMessage +import io.github.pedalpi.pedalpi_display.communication.message.response.ResponseVerb + +class ResponseMessageProcessor : Client.OnMessageListener { + var listener : Client.OnMessageListener = Client.OnMessageListener {} + + override fun onMessage(message: ResponseMessage) { + Log.i("OnMSG", message.toString()) + + if (message.type == ResponseVerb.KEYBOARD_EVENT && message.content["code"].string == "DOWN") { + Log.i("KEY", message.content["number"].int.toString()) + + val inst = Instrumentation() + inst.sendKeyDownUpSync(message.content["number"].int) + + } else if (message.type == ResponseVerb.RESPONSE && message.request == Messages.CURRENT_PEDALBOARD_DATA) { + val index = message.content["pedalboard"].int + + Data.getInstance().currentPedalboardPosition = index + Data.getInstance().currentPedalboard = message.content["bank"]["pedalboards"][index] + + } else if (message.type == ResponseVerb.EVENT) { + val event = EventMessage(message.content) + + if (event.type == EventType.CURRENT) { + val id = event.content["pedalboard"].int + + Data.getInstance().currentPedalboardPosition = id + Data.getInstance().currentPedalboard = event.content["value"] + } + } + + listener.onMessage(message) + } +} diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/EventMessage.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/EventMessage.kt new file mode 100644 index 0000000..77e47ce --- /dev/null +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/message/response/EventMessage.kt @@ -0,0 +1,32 @@ +package io.github.pedalpi.pedalpi_display.communication.message.response + +import com.github.salomonbrys.kotson.get +import com.github.salomonbrys.kotson.string +import com.google.gson.JsonElement + + +enum class EventType(private val type : String) { + CURRENT("CURRENT"), + BANK("BANK"), + PEDALBOARD("PEDALBOARD"), + EFFECT("EFFECT"), + EFFECT_TOGGLE("EFFECT-TOGGLE"), + PARAM("PARAM"), + CONNECTION("CONNECTION") +} + +enum class UpdateType(private val type : String) { + CREATED("CREATED"), + UPDATED("UPDATED"), + DELETED("DELETED"), + + NULL("NULL") +} + +class EventMessage(val type : EventType, val content : JsonElement) { + + val updateType : UpdateType? + get() = UpdateType.valueOf(content["updateType"].string ) + + constructor(content: JsonElement) : this(EventType.valueOf(content["type"].string), content) +} diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/ServerApplication.java b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/App.java similarity index 91% rename from app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/ServerApplication.java rename to app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/App.java index 160c9f0..3a34900 100644 --- a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/ServerApplication.java +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/App.java @@ -4,7 +4,7 @@ import java.io.IOException; -public class ServerApplication extends Application { +public class App extends Application { @Override public void onCreate() { diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/Server.java b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/Server.java index 67e605f..768e30d 100644 --- a/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/Server.java +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/communication/server/Server.java @@ -7,6 +7,7 @@ import java.util.List; import io.github.pedalpi.pedalpi_display.communication.Client; +import io.github.pedalpi.pedalpi_display.communication.ResponseMessageProcessor; import io.github.pedalpi.pedalpi_display.communication.message.request.Messages; import io.github.pedalpi.pedalpi_display.communication.message.request.RequestMessage; import io.github.pedalpi.pedalpi_display.communication.message.request.SystemMessages; @@ -26,7 +27,7 @@ public static synchronized Server getInstance() { private ServerSocket connection; private List clients = new LinkedList<>(); - private Client.OnMessageListener listener = message -> {}; + private ResponseMessageProcessor listener = new ResponseMessageProcessor(); public void start(int port) { try { @@ -67,9 +68,6 @@ public void sendBroadcast(RequestMessage message) { } public void setListener(Client.OnMessageListener listener) { - this.listener = listener; - - for (Client client: clients) - client.setOnMessageListener(listener); + this.listener.setListener(listener); } } diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsActivity.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsActivity.kt new file mode 100644 index 0000000..f165796 --- /dev/null +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsActivity.kt @@ -0,0 +1,162 @@ +package io.github.pedalpi.pedalpi_display.effects + +import android.os.Bundle +import android.support.v7.app.AppCompatActivity +import android.util.Log +import android.widget.ListView +import com.github.salomonbrys.kotson.array +import com.github.salomonbrys.kotson.get +import com.github.salomonbrys.kotson.string +import io.github.pedalpi.pedalpi_display.Data +import io.github.pedalpi.pedalpi_display.R +import io.github.pedalpi.pedalpi_display.communication.server.Server +import java.util.* + + +class EffectsActivity : AppCompatActivity() { + + private lateinit var listView: ListView + private lateinit var adapter: EffectsListItemAdapter + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_effects) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + + if (Data.getInstance().currentPedalboard != null) { + title = title() + } + + populateViews() + + Server.getInstance().setListener({ }) //onMessage(it) + } + + private fun title(): String { + return "%02d - %s".format(Data.getInstance().currentPedalboardPosition, Data.getInstance().currentPedalboard["name"].string) + } + + private fun populateViews() { + this.listView = findViewById(R.id.effectsList) as ListView + this.adapter = EffectsListItemAdapter(this, generateData()) + + this.adapter.toggleStatusListener = { Log.i("TOGGLE", it.index.toString()) } + this.adapter.selectEffectListener = { Log.i("SELECT", it.name) } + + this.listView.adapter = adapter + this.adapter.notifyDataSetChanged() + } + + private fun generateData(): List { + val elements = ArrayList() + + val pedalboard = Data.getInstance().currentPedalboard + if (pedalboard != null) { //Fixme REMOVE + pedalboard["effects"].array.mapIndexedTo(elements) { index, value -> EffectsListItemDTO(index, value) } + } + + for (i in 0..9) + elements.add(EffectsListItemDTO(i, "{}", "Bett"+i, false)) + + return elements + } + + public override fun onResume() { + super.onResume() + + //val container = findViewById(R.id.effectsList) as ListView + //container.removeAllViews() + + //populateViews() + } + + override fun onSupportNavigateUp(): Boolean { + finish() + return true + } + + /* + private fun createEffectButton(effect: Effect, container: LinearLayout, layoutParams: LinearLayout.LayoutParams): Button { + val button = Button(this) + button.setTextAppearance(applicationContext, R.style.Effect) + + button.setBackgroundColor(if (effect.isActive()) Color.rgb(60, 179, 113) else Color.rgb(178, 34, 34)) + button.setText(effect.getName()) + button.setOnClickListener { + toggleStatusEffect(effect, button) + updateEffectStatusToServer(effect) + } + + button.setOnLongClickListener { + openScreenParamsList(effect) + true + } + + container.addView(button, layoutParams) + return button + } + + private fun toggleStatusEffect(effect: Effect, button: Button) { + Log.i("BOTAO", "Effect selected: " + effect) + effect.toggleStatus() + runOnUiThread { + button.setBackgroundColor(if (effect.isActive()) Color.rgb(60, 179, 113) else Color.rgb(178, 34, 34)) + Toast.makeText(applicationContext, "efeito " + effect, Toast.LENGTH_SHORT).show() + } + } + + + private fun updateEffectStatusToServer(effect: Effect) { + val message = MessageProcessor.generateEffectStatusToggled(effect) + Server.getInstance().send(message) + } + + private fun openScreenParamsList(effect: Effect) { + val intent = Intent(baseContext, ParamsActivity::class.java) + intent.putExtra(PatchActivity.PATCH, this.patch) + intent.putExtra(EffectsActivity.EFFECT_INDEX, effect.getIndex()) + startActivityForResult(intent, 0) + } + + override fun onBackPressed() { + val intent = Intent() + intent.putExtra(PatchActivity.PATCH, this.patch) + + setResult(0, intent) // onActivityResult da tela anterior + finish() + } + + public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { + Server.getInstance().setListener(this) + Log.i("MESSAGE", String.valueOf(this.patch!!.getEffects())) + this.patch = data.extras!!.getSerializable(PatchActivity.PATCH) as Patch + val settedCurrentPatch = data.getBooleanExtra(PatchActivity.SETTED_CURRENT_PATCH, false) + + if (settedCurrentPatch) + onBackPressed() + + super.onActivityResult(requestCode, resultCode, data) + + } + + @Throws(JSONException::class) + fun onMessage(message: Message) { + Log.i("MESSAGE", message.getType().toString()) + this.patch = MessageProcessor.process(message, this.patch) + + if (message.getType() === ProtocolType.EFFECT) { + val indexEffect = message.getContent().getInt("index") + toggleStatusEffectView(this.patch!!.getEffects().get(indexEffect), buttons!![indexEffect]) + } else if (message.getType() === ProtocolType.PATCH) { + onBackPressed() + } + } + + private fun toggleStatusEffectView(effect: Effect, button: Button) { + runOnUiThread { + button.setBackgroundColor(if (effect.isActive()) Color.rgb(60, 179, 113) else Color.rgb(178, 34, 34)) + Toast.makeText(applicationContext, "efeito " + effect, Toast.LENGTH_SHORT).show() + } + } + */ +} \ No newline at end of file diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemAdapter.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemAdapter.kt new file mode 100644 index 0000000..8c5bbc0 --- /dev/null +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemAdapter.kt @@ -0,0 +1,74 @@ +package io.github.pedalpi.pedalpi_display.effects + +import android.app.Activity +import android.content.Context +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.BaseAdapter +import android.widget.Button +import android.widget.ToggleButton +import io.github.pedalpi.pedalpi_display.R + +typealias ToggleStatusListener = (effect: EffectsListItemDTO) -> Unit +typealias SelectEffectListener = (effect: EffectsListItemDTO) -> Unit + +/** + * Based in https://github.com/betranthanh/android-KotlinListView + */ +class EffectsListItemAdapter(private val activity: Activity, private val items: List): BaseAdapter() { + + var toggleStatusListener : ToggleStatusListener = { } + var selectEffectListener : SelectEffectListener = { } + + private class ViewHolder(private val row: View, private val adapter : EffectsListItemAdapter) { + val name = row.findViewById(R.id.effectsListItemName) as Button + val status = row.findViewById(R.id.effectsListItemStatus) as ToggleButton + + lateinit var dto: EffectsListItemDTO + + init { + name.setOnClickListener { adapter.selectEffectListener(dto) } + status.setOnClickListener { adapter.toggleStatusListener(dto) } + } + + fun update(effect : EffectsListItemDTO) { + dto = effect + + name.text = effect.name + status.isChecked = effect.status + } + } + + override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { + val view: View + val viewHolder: ViewHolder + + if (convertView == null) { + val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater + view = inflater.inflate(R.layout.effects_list_item, null) + viewHolder = ViewHolder(view, this) + view.tag = viewHolder + + } else { + view = convertView + viewHolder = view.tag as ViewHolder + } + + viewHolder.update(items[position]) + + return view + } + + override fun getItem(i: Int): EffectsListItemDTO { + return items[i] + } + + override fun getItemId(i: Int): Long { + return i.toLong() + } + + override fun getCount(): Int { + return items.size + } +} diff --git a/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemDTO.kt b/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemDTO.kt new file mode 100644 index 0000000..9a88d40 --- /dev/null +++ b/app/src/main/java/io/github/pedalpi/pedalpi_display/effects/EffectsListItemDTO.kt @@ -0,0 +1,9 @@ +package io.github.pedalpi.pedalpi_display.effects + +import com.github.salomonbrys.kotson.bool +import com.github.salomonbrys.kotson.get +import com.google.gson.JsonElement + +class EffectsListItemDTO(val index : Int, val data: String, val name: String, val status: Boolean) { + constructor(index : Int, data: JsonElement) : this(index, data.toString(), "Effect name" + index, data["active"].bool) +} diff --git a/app/src/main/res/layout/activity_effects.xml b/app/src/main/res/layout/activity_effects.xml new file mode 100644 index 0000000..d4d0390 --- /dev/null +++ b/app/src/main/res/layout/activity_effects.xml @@ -0,0 +1,9 @@ + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e7ab324..8b099fc 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="io.github.pedalpi.pedalpi_display.MainActivity"> + tools:context="io.github.pedalpi.pedalpi_display.MainActivity" > +