diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/activities/MainActivity.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/activities/MainActivity.kt index 0ff7b0e..fe16b24 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/activities/MainActivity.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/activities/MainActivity.kt @@ -88,136 +88,11 @@ class MainActivity : AppCompatActivity() { super.onResume() // server communication - val dataReceiver0: (String) -> Unit = { data0: String -> - val dataReceiver1: (String) -> Unit = { data1: String -> - val json = JsonManager(this, data1, "JSONObject") - val names = json.currentJsonObject!!.names() - - try { - names?.let { - mainFragmentHost.childFragmentManager.fragments[0]?.let { - if (it is homeFragment) { - LabeledValuesSupplier.wipeData() - - for (n in 0 until names.length()) { - val labeledValue = LabeledValue(names[n].toString(), json.getAttributeContent(names[n].toString())) - if (!LabeledValuesSupplier.checkIfContains(labeledValue)) { - LabeledValuesSupplier.appendLabeledValue(labeledValue) - } - } - - it.labeled_values_recycler_view.post { - val layoutManager = LinearLayoutManager(it.requireContext()) - layoutManager.orientation = LinearLayoutManager.VERTICAL - it.labeled_values_recycler_view.layoutManager = layoutManager - - val adapter = LabeledValuesAdapter(it.requireContext(), LabeledValuesSupplier.labeledValues) - it.labeled_values_recycler_view.adapter = adapter - } - } - } - } - } catch (e: Throwable) { println("[debug] Exception in main activity, sensors connection : $e") } - - } - - if (!ServerManagement.serverManager.checkIfConnectionAlreadyExists("sensorsConnection")){ - ServerManagement.serverManager.addReceiverConnection(dataReceiver1, this, "sensorsConnection", data0.toInt(), ServerManagement.sensors_keyword) - } - - // getting other needed information - val dataReceiver2: (String) -> Unit = {data1: String -> - var json = JsonManager(this, data1) - json = JsonManager(this, json.findJsonObjectByAttribute("ID", data0.toInt()), "JSONObject") // todo doesnt return correct result - val positionsList = json.getAttributeContent("location").split(",") - MapManagement.connectedServerPosition = LatLng(positionsList[0].toDouble(), positionsList[1].toDouble()) - } - - if (!ServerManagement.serverManager.checkIfConnectionAlreadyExists("mapConnection")){ - ServerManagement.serverManager.addReceiverConnection(dataReceiver2, this, "mapConnection", data0.toInt(), "", "GET_WHOLE_ARRAY") - } - - val dataReceiver3: (String) -> Unit = { data1: String -> - val json = JsonManager(this, data1) - json.findJsonObjectByAttribute("ID", data0.toInt()) - - fun updateFileViewsRecyclerView(fragment: Fragment) { - try { - fragment.homeFragmentInnerFragment?.let { - it.file_views_recycler_view.post { - val layoutManager = LinearLayoutManager(fragment.requireContext()) - layoutManager.orientation = LinearLayoutManager.VERTICAL - it.file_views_recycler_view.layoutManager = layoutManager - - val adapter = FileViewsAdapter(fragment.requireContext(), FileViewsSupplier.fileViews) - it.file_views_recycler_view.adapter = adapter - } - } - } catch (e: Throwable) { println("[debug] e1 that i couldnt fix so try catch Exception: $e") } - } - - try { - mainFragmentHost.childFragmentManager.fragments[0]?.let { - when (it) { - - is homeFragment -> { - - json.getAttributeContent("files") - - for (n in 0 until json.currentJsonAttribute1!!.length()) { - val fileInfo = JsonManagerLite(json.getAttributeContentByPath("files/$n"), "JSONObject") - val filetype = fileInfo.getAttributeContentByPath("format").split(".")[1] - val filename = fileInfo.getAttributeContentByPath("name") - val fileDescription = fileInfo.getAttributeContentByPath("description") - - // handling text - if ("txt json".contains(filetype)) { - val fileView = FileView(filetype, filename, fileDescription, "$data0|||||$filename.$filetype") - if (!FileViewsSupplier.checkIfContains(fileView)) { - FileViewsSupplier.appendFileView(fileView) - updateFileViewsRecyclerView(it) - } - } - - // handling images - if ("jpg png".contains(filetype)) { - val fileView = FileView(filetype, filename, fileDescription, null, "$data0|||||$filename.$filetype") - if (!FileViewsSupplier.checkIfContains(fileView)) { - FileViewsSupplier.appendFileView(fileView) - updateFileViewsRecyclerView(it) - } - } - - // handling pdf files - if ("pdf".contains(filetype)) { - val fileView = FileView(filetype, filename, fileDescription, null, null, "${ServerManagement.baseUrl}files/$data0/$filename.$filetype") - if (!FileViewsSupplier.checkIfContains(fileView)) { - FileViewsSupplier.appendFileView(fileView) - updateFileViewsRecyclerView(it) - } - - } - - } - } - } - } - } catch (e: Throwable) { println("[debug] Exception in main activity, files data request : $e") } - - } - - ServerManagement.serverManager.addReceiverConnection(dataReceiver3, this, "fileViewsConnection", data0.toInt(), "", "GET_WHOLE_ARRAY") - } - - ServerManagement.serverManager.getData(dataReceiver0, this, 0, "", "connected_id", 3) connectExploreFragmentAdapterModel() } override fun onPause() { PlaceSupplier.saveToCache(this) - ServerManagement.serverManager.deleteConnection("sensorsConnection") - ServerManagement.serverManager.deleteConnection("mapConnection") - ServerManagement.serverManager.deleteConnection("fileViewsConnection") ServerManagement.serverManager.deleteConnection("exploreListConnection") super.onPause() } diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/chatFragment.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/chatFragment.kt index 05d3421..7e7c0dc 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/chatFragment.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/chatFragment.kt @@ -97,7 +97,6 @@ class chatFragment : Fragment(R.layout.fragment_chat) { MessagesSupplier.clearWaitingMessages() for (i in 0 until length) { - println("message at index n: ${json.getAttributeContentByPath("data/$i")}") val jsonOfMessage = JsonManager(requireContext(), json.getAttributeContentByPath("data/$i"), "JSONObject") val message = Message(jsonOfMessage.getAttributeContent("sender"), jsonOfMessage.getAttributeContent("message"), diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/homeFragment.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/homeFragment.kt index 1af5263..42e988c 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/homeFragment.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/homeFragment.kt @@ -5,11 +5,23 @@ import android.os.Bundle import android.view.View import androidx.fragment.app.Fragment import androidx.navigation.Navigation +import androidx.recyclerview.widget.LinearLayoutManager import com.example.wikispot.GeneralVariables +import com.example.wikispot.MapManagement import com.example.wikispot.R import com.example.wikispot.ServerManagement +import com.example.wikispot.adapters.FileViewsAdapter +import com.example.wikispot.adapters.LabeledValuesAdapter import com.example.wikispot.modelClasses.JsonManager +import com.example.wikispot.modelClasses.JsonManagerLite +import com.example.wikispot.modelsForAdapters.FileView +import com.example.wikispot.modelsForAdapters.FileViewsSupplier +import com.example.wikispot.modelsForAdapters.LabeledValue +import com.example.wikispot.modelsForAdapters.LabeledValuesSupplier +import com.google.android.gms.maps.model.LatLng +import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.fragment_home.* +import kotlinx.android.synthetic.main.fragment_info.* import kotlinx.android.synthetic.main.fragment_info.view.* @@ -30,7 +42,11 @@ class homeFragment : Fragment(R.layout.fragment_home) { super.onResume() // connecting to server - val dataReceiver: (String) -> Unit = {data: String -> + + val serverConnectorThread = Thread(ServerConnector()) + serverConnectorThread.start() + + val dataReceiver0: (String) -> Unit = { data: String -> try { val json = JsonManager(requireContext(), data) @@ -82,15 +98,153 @@ class homeFragment : Fragment(R.layout.fragment_home) { } catch (e: Throwable) { println(e) } } - ServerManagement.serverManager.getData(dataReceiver, requireContext(), 0, "", "GET_WHOLE_ARRAY", 4) + val dataReceiver1: (String) -> Unit = {connectedId: String -> + ServerManagement.connectedServerId = connectedId.toInt() + } + + ServerManagement.serverManager.getData(dataReceiver0, requireContext(), 0, "", "GET_WHOLE_ARRAY", 4) + ServerManagement.serverManager.getData(dataReceiver1, requireContext(), 0, "", "connected_id", 3) } override fun onPause() { super.onPause() + ServerManagement.serverManager.deleteConnection("sensorsConnection") + ServerManagement.serverManager.deleteConnection("mapConnection") + ServerManagement.serverManager.deleteConnection("fileViewsConnection") saveCache() } + private fun tryConnectingToServer() { + ServerManagement.connectedServerId?.let{ connectedServerId: Int -> + context?.let { + val dataReceiver1: (String) -> Unit = { data1: String -> + + try { + val json = JsonManager(requireContext(), data1, "JSONObject") + val names = json.currentJsonObject!!.names() + + names?.let { + LabeledValuesSupplier.wipeData() + + for (n in 0 until names.length()) { + val labeledValue = LabeledValue(names[n].toString(), json.getAttributeContent(names[n].toString())) + if (!LabeledValuesSupplier.checkIfContains(labeledValue)) { + LabeledValuesSupplier.appendLabeledValue(labeledValue) + } + } + + labeled_values_recycler_view.post { + val layoutManager = LinearLayoutManager(requireContext()) + layoutManager.orientation = LinearLayoutManager.VERTICAL + labeled_values_recycler_view.layoutManager = layoutManager + + val adapter = LabeledValuesAdapter(requireContext(), LabeledValuesSupplier.labeledValues) + labeled_values_recycler_view.adapter = adapter + } + } + } catch (e: Throwable) { println("[debug] Exception in main activity, sensors connection : $e") } + + } + + if (!ServerManagement.serverManager.checkIfConnectionAlreadyExists("sensorsConnection")){ + ServerManagement.serverManager.addReceiverConnection(dataReceiver1, requireContext(), "sensorsConnection", connectedServerId, ServerManagement.sensors_keyword) + } + + // getting other needed information + val dataReceiver2: (String) -> Unit = {data1: String -> + context?.let { + var json = JsonManager(requireContext(), data1) + json = JsonManager(requireContext(), json.findJsonObjectByAttribute("ID", connectedServerId), "JSONObject") // todo doesnt return correct result + val positionsList = json.getAttributeContent("location").split(",") + MapManagement.connectedServerPosition = LatLng(positionsList[0].toDouble(), positionsList[1].toDouble()) + } + } + + if (!ServerManagement.serverManager.checkIfConnectionAlreadyExists("mapConnection")){ + ServerManagement.serverManager.addReceiverConnection(dataReceiver2, requireContext(), "mapConnection", connectedServerId, "", "GET_WHOLE_ARRAY") + } + + val dataReceiver3: (String) -> Unit = { data1: String -> + + fun updateFileViewsRecyclerView(fragment: Fragment) { + try { + fragment.context?.let { + fragment.homeFragmentInnerFragment?.let { + it.file_views_recycler_view.post { + val layoutManager = LinearLayoutManager(fragment.requireContext()) + layoutManager.orientation = LinearLayoutManager.VERTICAL + it.file_views_recycler_view.layoutManager = layoutManager + + val adapter = FileViewsAdapter(fragment.requireContext(), FileViewsSupplier.fileViews) + it.file_views_recycler_view.adapter = adapter + } + } + } + } catch (e: Throwable) { println("[debug] e1 that i couldnt fix so try catch Exception: $e") } + } + + try { + val json = JsonManager(requireContext(), data1) + json.findJsonObjectByAttribute("ID", connectedServerId) + + json.getAttributeContent("files") + + for (n in 0 until json.currentJsonAttribute1!!.length()) { + val fileInfo = JsonManagerLite(json.getAttributeContentByPath("files/$n"), "JSONObject") + val filetype = fileInfo.getAttributeContentByPath("format").split(".")[1] + val filename = fileInfo.getAttributeContentByPath("name") + val fileDescription = fileInfo.getAttributeContentByPath("description") + + // handling text + if ("txt json".contains(filetype)) { + val fileView = FileView(filetype, filename, fileDescription, "$connectedServerId|||||$filename.$filetype") + if (!FileViewsSupplier.checkIfContains(fileView)) { + FileViewsSupplier.appendFileView(fileView) + updateFileViewsRecyclerView(this) + } + } + + // handling images + if ("jpg png".contains(filetype)) { + val fileView = FileView(filetype, filename, fileDescription, null, "$connectedServerId|||||$filename.$filetype") + if (!FileViewsSupplier.checkIfContains(fileView)) { + FileViewsSupplier.appendFileView(fileView) + updateFileViewsRecyclerView(this) + } + } + + // handling pdf files + if ("pdf".contains(filetype)) { + val fileView = FileView(filetype, filename, fileDescription, null, null, "${ServerManagement.baseUrl}files/$connectedServerId/$filename.$filetype") + if (!FileViewsSupplier.checkIfContains(fileView)) { + FileViewsSupplier.appendFileView(fileView) + updateFileViewsRecyclerView(this) + } + + } + + } + } catch (e: Throwable) { println("[debug] Exception in home fragment, files data request : $e") } + + } + + if (!ServerManagement.serverManager.checkIfConnectionAlreadyExists("fileViewsConnection")) { + ServerManagement.serverManager.addReceiverConnection(dataReceiver3, requireContext(), "fileViewsConnection", connectedServerId, "", "GET_WHOLE_ARRAY") + } + } + } + } + + inner class ServerConnector(private val numberOfAttempts: Int=3): Runnable { + override fun run() { + for (n in 0 until numberOfAttempts) { + tryConnectingToServer() + Thread.sleep(1000) + } + } + } + private fun loadCache() {} private fun saveCache() {} diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/infoFragment.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/infoFragment.kt index 041c463..d05e7a3 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/infoFragment.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/infoFragment.kt @@ -31,7 +31,7 @@ class infoFragment : Fragment(R.layout.fragment_info) { var location: LatLng? = null var phoneNumber: Int? = null var email: String? = null - var executeLoadFuntion = false + var executeLoadFunction = false override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -42,17 +42,21 @@ class infoFragment : Fragment(R.layout.fragment_info) { updateFileViewsRecyclerView() try { - executeLoadFuntion = args.executeLoadFuntion - } catch (e: Throwable) { println("[debug] Exception in Info Fragment while getting args: $e") } + executeLoadFunction = args.executeLoadFuntion + } catch (e: Throwable) { + if (!e.toString().contains("has null arguments")){ + println("[debug] Exception in Info Fragment while getting args: $e") + } + } - if (executeLoadFuntion) { + if (executeLoadFunction) { load() } else { getContactInfoFromGeneralVariables() } locationBtn.setOnClickListener { - if (executeLoadFuntion) { + if (executeLoadFunction) { if (location != null) { val action = infoFragmentDirections.infoFragmentToMapFragment(location!!) Navigation.findNavController(it).navigate(action) @@ -243,7 +247,9 @@ class infoFragment : Fragment(R.layout.fragment_info) { phoneNumber?.let { try { phoneBtn.post { - phoneBtn.visibility = View.VISIBLE + phoneBtn?.let { + phoneBtn.visibility = View.VISIBLE + } } } catch (e: Throwable) { println("[debug] Exception in checkContactInformation: $e") } } @@ -251,7 +257,9 @@ class infoFragment : Fragment(R.layout.fragment_info) { email?.let { try { emailBtn.post { - emailBtn.visibility = View.VISIBLE + emailBtn?.let { + emailBtn.visibility = View.VISIBLE + } } } catch (e: Throwable) { println("[debug] Exception in checkContactInformation: $e") } } diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/mapFragment.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/mapFragment.kt index 1b2ce5d..24bb303 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/mapFragment.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/mapFragment.kt @@ -87,14 +87,12 @@ class mapFragment : Fragment(), GoogleMap.OnMarkerClickListener { CustomBackstackVariables.infoFragmentBackDestination = "mapFragment" MapManagement.lastCoordinates = marker.position ServerManagement.selectedServerId = PlaceSupplier.places[n]!!.id!! - val action = mapFragmentDirections.mapFragmentToInfoFragment() + val action = mapFragmentDirections.mapFragmentToInfoFragment(true) Navigation.findNavController(navControllerView).navigate(action) } } } lastClickedMarkerTitle = marker.title - println("[debug] marker title ${marker.title}") - println(System.currentTimeMillis()) } return false } diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/modelClasses/ServerManager.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/modelClasses/ServerManager.kt index b2901e5..7299f06 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/modelClasses/ServerManager.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/modelClasses/ServerManager.kt @@ -206,36 +206,51 @@ class ServerManager { fun deleteConnection(connectionName: String, connectionType: String = "any") { // other types are any, activity and view if ((connectionType == "any") or (connectionType == "receiver")) { + val indexesToRemove = mutableListOf() for (i in 0 until receiverConnections.size) { // checking in connections try { if (receiverConnections[i].connectionName == connectionName) { receiverConnections[i].running = false - receiverConnections.removeAt(i) + indexesToRemove.add(i) } } catch (e: Throwable) { println("In deleteConnection: $e") } } + + for (i in 0 until indexesToRemove.size) { + receiverConnections.removeAt(indexesToRemove[i] - i) + } } if ((connectionType == "any") or (connectionType == "view")) { + val indexesToRemove = mutableListOf() for (i in 0 until viewConnections.size) { // checking in connections try { if (viewConnections[i].connectionName == connectionName) { viewConnections[i].running = false - viewConnections.removeAt(i) + indexesToRemove.add(i) } } catch (e: Throwable) { println("In deleteConnection: $e") } } + + for (i in 0 until indexesToRemove.size) { + viewConnections.removeAt(indexesToRemove[i] - i) + } } if ((connectionType == "any") or (connectionType == "chat")) { + val indexesToRemove = mutableListOf() for (i in 0 until chatConnections.size) { // checking in connections try { if (chatConnections[i].connectionName == connectionName) { chatConnections[i].running = false - chatConnections.removeAt(i) + indexesToRemove.add(i) } } catch (e: Throwable) { println("In deleteConnection: $e") } } + + for (i in 0 until indexesToRemove.size) { + chatConnections.removeAt(indexesToRemove[i] - i) + } } } diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/projectScopeVariables.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/projectScopeVariables.kt index 94182d6..6bca192 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/projectScopeVariables.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/projectScopeVariables.kt @@ -37,6 +37,7 @@ object ServerManagement { const val dataRequestOnAttemptWait: Long = 2000 const val imageRequestOnAttemptWait: Long = 2000 var baseUrl = "http://192.168.1.156:8000/" + var connectedServerId: Int? = null var selectedServerId = 0 const val sensors_keyword = "_[{(S,e,n,s,o,r,s)}]_" diff --git a/app/WikiSpot/app/src/main/res/drawable/settings_fragment_gradient_background.xml b/app/WikiSpot/app/src/main/res/drawable/settings_fragment_gradient_background.xml index 20337a0..0a6ff3b 100644 --- a/app/WikiSpot/app/src/main/res/drawable/settings_fragment_gradient_background.xml +++ b/app/WikiSpot/app/src/main/res/drawable/settings_fragment_gradient_background.xml @@ -4,6 +4,7 @@ - + android:backgroundTint="?attr/exploreFragmentListItemBackgroundColor" + card_view:cardCornerRadius="10dp" + card_view:cardElevation="4dp"> diff --git a/app/WikiSpot/app/src/main/res/navigation/main_activity_navigation.xml b/app/WikiSpot/app/src/main/res/navigation/main_activity_navigation.xml index 733ea4a..ed0cfe4 100644 --- a/app/WikiSpot/app/src/main/res/navigation/main_activity_navigation.xml +++ b/app/WikiSpot/app/src/main/res/navigation/main_activity_navigation.xml @@ -48,9 +48,6 @@ - #F48FB1 @color/black - #fff + #E6FFFFFF #89000000 #89000000 #96262626 @@ -38,7 +38,7 @@ #A1DEFC #A5CBFF #F9B6F6 - #fff + #FBF1FF #83E1FF #9EC6FF @@ -57,9 +57,15 @@ #FF7272 - #C4FFA5 - #D1FBA0 - #F6FEBA + + + + #99C8FCFF + #99CCF7FF + #99E7EFFF @@ -72,53 +78,57 @@ #F48FB1 @color/black - #F2F2F2 - #CCC5C5C5 - #D6D6D6 - #F0F5F5F5 + #E7E7E7 + #CCBEBEBE + #CCCCCC + #CDE0DFDF - #4343E6 - #0C16AA - #604B4B4B + #F76751F8 + #F75241C2 + #26644F69 #EC000000 - #A3422392 - #9451357D + #80AC78C4 + #80855C95 - #0F0F1C - #0F0B15 + #33284E + #302444 - #2643386A - #2643386A + #4D473D6A + #2DC39CE1 #6F3131 - #36135C - #06134B - #030729 - #191021 + #4D306D + #222D54 + #1D2043 + #221934 #BF323232 - #091121 - #08133A - #FA2D1148 - #1B1A1F + #1E293E + #212C51 + #FA452862 + #323251 . - #001132 - #0B204A - #36054F + #263863 + #1F2360 + #452159 - #2D145B - #140945 - #0F1237 + #4B3377 + #352C5B + #2B2E5C - #380B4B - #0E1A33 - #0B1D4B + #542766 + #1A294A + #2A3B65 - #8833374A + #8842475F + #79454D78 + #6F363F67 + +