From c15e051fce07aabb3b6e35c93d31d229b3bd915b Mon Sep 17 00:00:00 2001 From: miloszwasacz Date: Sat, 21 Mar 2020 23:08:05 +0100 Subject: [PATCH] Clean up the code --- .../tictactoe9x9/CloseSocketTask.kt | 26 -------- .../tictactoe9x9/CommunicationTask.kt | 65 ------------------- .../tictactoe9x9/CommunicationViewModel.kt | 34 ++-------- .../miloszwasacz/tictactoe9x9/ConnectTask.kt | 57 ---------------- .../tictactoe9x9/EchoWebSocketListener.kt | 1 - .../miloszwasacz/tictactoe9x9/MainActivity.kt | 2 +- .../miloszwasacz/tictactoe9x9/SendTask.kt | 14 ---- app/src/main/res/layout/activity_main.xml | 3 +- app/src/main/res/values/arrays.xml | 2 +- 9 files changed, 8 insertions(+), 196 deletions(-) delete mode 100644 app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CloseSocketTask.kt delete mode 100644 app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationTask.kt delete mode 100644 app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/ConnectTask.kt delete mode 100644 app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/SendTask.kt diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CloseSocketTask.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CloseSocketTask.kt deleted file mode 100644 index c7a2bcb..0000000 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CloseSocketTask.kt +++ /dev/null @@ -1,26 +0,0 @@ -@file:Suppress("DEPRECATION") -package com.gmail.miloszwasacz.tictactoe9x9 - -/*class CloseSocketTask(private val viewModel: CommunicationViewModel): AsyncTask() { - - override fun doInBackground(vararg arg0: Void): Void? { - for(task in viewModel.communicationTaskList) { - if(task.status == Status.RUNNING || task.status == Status.PENDING) - task.cancel(false) - } - for(task in viewModel.sendTaskList) { - if(task.status == Status.RUNNING || task.status == Status.PENDING) - task.cancel(false) - } - - return null - }*/ - - /*override fun onPostExecute(result: Void?) { - if(viewModel.socket != null && !viewModel.socket!!.isClosed) { - viewModel.socket!!.shutdownInput() - viewModel.socket!!.shutdownOutput() - viewModel.socket!!.close() - } - } -}*/ \ No newline at end of file diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationTask.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationTask.kt deleted file mode 100644 index 35703c6..0000000 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationTask.kt +++ /dev/null @@ -1,65 +0,0 @@ -@file:Suppress("InflateParams", "SetTextI18n") -package com.gmail.miloszwasacz.tictactoe9x9 - -import android.os.AsyncTask -import android.util.Log -import java.io.BufferedReader - -class CommunicationTask(private val viewModel: CommunicationViewModel, private val inputPacket: Packet?/*, private var socket: Socket, private var output: OutputStream, private var inputStream: InputStreamReader*/): AsyncTask() { - private lateinit var input: BufferedReader - //private var inputStream = viewModel.inputStream - - /*override fun onPreExecute() { - input = BufferedReader(inputStream) - - if(inputPacket != null && inputPacket is PacketSTT) { - viewModel.currentGameState.value = Event(viewModel.createBoardState(inputPacket)) - } - }*/ - - override fun doInBackground(vararg arg0: Void): Packet { - return if(!isCancelled) { - val packetString = input.readLine() - val packet = viewModel.deserializePacketFromServer(packetString) - - Log.i("packet", packetString) - packet - } - else PacketBadErrDbgUin(method = "DBG", params = ParamsBadErrDbgUin("Task cancelled"), time = (System.currentTimeMillis()/1000L).toInt()) - } - - /*override fun onPostExecute(resultPacket: Packet) { - if(!isCancelled) { - when(resultPacket) { - //Odbieranie planszy - is PacketSTT -> { - viewModel.dialogId.value = Event(viewModel.removeDialog) - viewModel.currentGameState.value = Event(viewModel.createBoardState(resultPacket)) - viewModel.communicate(null) - } - //Wysłanie odpowiedzi na pakiet "PNG" - is PacketGetPngPog -> { - if(resultPacket.method == "PNG") { - viewModel.sendPOG() - } - viewModel.communicate(null) - } - //Wyświetlanie info o oprogramowaniu - is PacketVER -> { - viewModel.versionPacket = resultPacket - viewModel.dialogId.value = Event(viewModel.versionDialogId) - viewModel.communicate(null) - } - //Zapisywanie błędów itp. w Log'u - else -> { - Log.i("packetMSG", (resultPacket as PacketBadErrDbgUin).params.msg) - - viewModel.communicate(null) - } - } - } - else { - Log.i("state", (resultPacket as PacketBadErrDbgUin).params.msg) - } - }*/ -} \ No newline at end of file diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationViewModel.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationViewModel.kt index 8335b3d..5f26405 100644 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationViewModel.kt +++ b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/CommunicationViewModel.kt @@ -19,28 +19,21 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap val versionDialogId = 1 //IP i port serwera - val serverIP = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_ip), application.getString(R.string.default_ip)) ?: application.getString(R.string.default_ip)).toString() - val serverPORT = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_port), application.getString(R.string.default_port)) ?: application.getString(R.string.default_port)).toInt() + private val serverIP = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_ip), application.getString(R.string.default_ip)) ?: application.getString(R.string.default_ip)).toString() + private val serverPORT = (PreferenceManager.getDefaultSharedPreferences(application).getString(application.getString(R.string.key_port), application.getString(R.string.default_port)) ?: application.getString(R.string.default_port)).toInt() //Socket - val client = OkHttpClient() - var socket: WebSocket? = null - //lateinit var output: OutputStream - //lateinit var inputStream: InputStreamReader + private val client = OkHttpClient() + private var socket: WebSocket? = null val NORMAL_CLOSURE_STATUS = 1000 - //Listy AsyncTask'ów - //val communicationTaskList = ArrayList() - //val sendTaskList = ArrayList() val interpretationTaskList = ArrayList() - var currentGameState = MutableLiveData>() var wrongSocket = MutableLiveData>() var versionPacket: PacketVER? = null //Łączenie z serwerem fun connect(roomName: String) { - //ConnectTask(this@CommunicationViewModel, roomName).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) dialogId.value = Event(connectDialogId) wrongSocket.value = Event(false) val request = Request.Builder().url("ws://$serverIP:$serverPORT").build() @@ -48,35 +41,20 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap socket = client.newWebSocket(request, listener) } - /* - //Odbieranie pakietu - fun communicate(packet: Packet?) { - val task = CommunicationTask(this@CommunicationViewModel, packet) - communicationTaskList.add(task) - task.execute() - }*/ - //Wysyłanie ruchu gracza fun sendMove(x: Int, y: Int) { - /*val task = SendTask(this@CommunicationViewModel, PacketSET(params = ParamsSET(x, y), time = (System.currentTimeMillis()/1000L).toInt())) - sendTaskList.add(task) - task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)*/ val packet = PacketSET(params = ParamsSET(x, y), time = (System.currentTimeMillis()/1000L).toInt()) socket?.send(Gson().toJson(packet)) } //Wysyłanie odpowiedzi na Ping fun sendPOG() { - /*val task = SendTask(this@CommunicationViewModel, PacketGetPngPog(method = "POG", params = ParamsGetPngPog(), time = (System.currentTimeMillis()/1000L).toInt())) - sendTaskList.add(task) - task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)*/ val packet = PacketGetPngPog(method = "POG", params = ParamsGetPngPog(), time = (System.currentTimeMillis()/1000L).toInt()) socket?.send(Gson().toJson(packet)) } //Zamykanie Socketa override fun onCleared() { - //CloseSocketTask(this@CommunicationViewModel).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) for(task in interpretationTaskList) { if(task.status == AsyncTask.Status.RUNNING || task.status == AsyncTask.Status.PENDING) task.cancel(false) @@ -87,8 +65,6 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap //Deserializacja pakietu z serwera w obiekt fun deserializePacketFromServer(input: String): Packet { - //val packetTypeJON = object: TypeToken() {}.type - //val packetTypeSET = object: TypeToken() {}.type val packetTypeSTT = object: TypeToken() {}.type val packetTypeVER = object: TypeToken() {}.type val packetTypeBadErrDbgUin = object: TypeToken() {}.type @@ -97,8 +73,6 @@ open class CommunicationViewModel(application: Application): AndroidViewModel(ap val tempPacket = Gson().fromJson(input, packetTypeGetPngPog) return when(tempPacket.method) { - //"JON" -> Gson().fromJson(input, packetTypeJON) - //"SET" -> Gson().fromJson(input, packetTypeSET) "STT" -> Gson().fromJson(input, packetTypeSTT) "VER" -> Gson().fromJson(input, packetTypeVER) "BAD" -> Gson().fromJson(input, packetTypeBadErrDbgUin) diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/ConnectTask.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/ConnectTask.kt deleted file mode 100644 index 66e0ffa..0000000 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/ConnectTask.kt +++ /dev/null @@ -1,57 +0,0 @@ -package com.gmail.miloszwasacz.tictactoe9x9 - - -/*class ConnectTask(private var viewModel: CommunicationViewModel, private var roomName: String = "public"): AsyncTask() { - private lateinit var socket: WebSocket - private lateinit var output: OutputStream - private lateinit var inputStream: InputStreamReader - private val serverIP = viewModel.serverIP - private val serverPORT = viewModel.serverPORT - - override fun onPreExecute() { - viewModel.dialogId.value = Event(viewModel.connectDialogId) - viewModel.wrongSocket.value = Event(false) - val request = Request.Builder().url("ws://$serverIP:$serverPORT").build() - val listener = EchoWebSocketListener(viewModel) - socket = viewModel.client.newWebSocket(request, listener) - } - - override fun doInBackground(vararg arg0: Void): Void? { - /* - try { - sockSocketAddress(serverIP, serverPORT)) - output = socket.getOutputStream() - val packet = Gson().toJson(PacketJON(params = ParamsJON(roomName), time = (System.currentTimeMillis()/1000L).toInt())) - output.write(packet.toByteArray(charset("UTF-8"))) - - inputStream = InputStreamReader(socket.getInputStream()) - val input = BufferedReader(inputStream) - - val resultJSON = input.readLine() - Log.i("packet", resultJSON) - - return resultJSON - } - catch(e: IOException) { - return null - }*/ - - val packet = Gson().toJson(PacketJON(params = ParamsJON(roomName), time = (System.currentTimeMillis()/1000L).toInt())) - socket.send(packet) - return null - } - - override fun onPostExecute(result: String?) { - if(result != null) { - viewModel.socket = socket - viewModel.output = output - viewModel.inputStream = inputStream - if(viewModel.deserializePacketFromServer(result).method == "STT") viewModel.dialogId.value = Event(viewModel.removeDialog) - val resultPacket = viewModel.deserializePacketFromServer(result) - viewModel.communicate(resultPacket) - } - else { - viewModel.wrongSocket.value = Event(true) - } - } -}*/ \ No newline at end of file diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/EchoWebSocketListener.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/EchoWebSocketListener.kt index fc787b7..509511a 100644 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/EchoWebSocketListener.kt +++ b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/EchoWebSocketListener.kt @@ -17,7 +17,6 @@ class EchoWebSocketListener(private val viewModel: CommunicationViewModel, priva val task = InterpretationTask(viewModel, text) viewModel.interpretationTaskList.add(task) task.execute() - //Log.i("Receiving", text) } override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/MainActivity.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/MainActivity.kt index 108cac3..5fe7bab 100644 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/MainActivity.kt +++ b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/MainActivity.kt @@ -40,8 +40,8 @@ class MainActivity: AppCompatActivity() { //Obsługa ikonek override fun onOptionsItemSelected(item: MenuItem): Boolean { val id: Int = item.itemId + //Ustawienia if(id == R.id.action_settings) { - // launch settings activity startActivity(Intent(this@MainActivity, SettingsActivity::class.java)) return true } diff --git a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/SendTask.kt b/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/SendTask.kt deleted file mode 100644 index e7feb45..0000000 --- a/app/src/main/java/com/gmail/miloszwasacz/tictactoe9x9/SendTask.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.gmail.miloszwasacz.tictactoe9x9 - -/*class SendTask(viewModel: CommunicationViewModel, private var packet: Packet): AsyncTask() { - private var output = viewModel.output - - override fun doInBackground(vararg arg0: Void): Void? { - val packetJSON = Gson().toJson(packet) - output.write(packetJSON.toByteArray(charset("UTF-8"))) - - Log.i("status", "SENT: $packetJSON") - - return null - } -}*/ \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 364ae12..e6e9ef4 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -28,7 +28,8 @@ app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toTopOf="parent" /> + app:layout_constraintTop_toTopOf="parent" + tools:ignore="Autofill" />