diff --git a/app/WikiSpot/app/src/main/AndroidManifest.xml b/app/WikiSpot/app/src/main/AndroidManifest.xml index 2230352..476e577 100644 --- a/app/WikiSpot/app/src/main/AndroidManifest.xml +++ b/app/WikiSpot/app/src/main/AndroidManifest.xml @@ -14,8 +14,9 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:usesCleartextTraffic="true" - android:theme="@style/Theme.WikiSpotWithActionBar"> - + android:theme="@style/Theme.WikiSpot"> + diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/contextExtentions.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/Extentions.kt similarity index 98% rename from app/WikiSpot/app/src/main/java/com/example/wikispot/contextExtentions.kt rename to app/WikiSpot/app/src/main/java/com/example/wikispot/Extentions.kt index 5084d18..cc26a32 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/contextExtentions.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/Extentions.kt @@ -42,7 +42,7 @@ fun Context.getThemeId(): Int { // for client fun Context.getDataFromServer(): String { // requesting data - val url = "http://192.168.1.230:8000/devices_list" + val url = "${ServerManagement.baseUrl}devices_list" val request = Request.Builder().url(url).build() val client = OkHttpClient() var receivedResponse = "" 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 27b0916..0b55dc7 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 @@ -6,8 +6,10 @@ import androidx.appcompat.app.AppCompatActivity import androidx.navigation.findNavController import androidx.navigation.ui.setupWithNavController import com.example.wikispot.* +import com.example.wikispot.fragments.* import com.example.wikispot.modelClasses.SettingsSaveManager import kotlinx.android.synthetic.main.activity_main.* +import kotlinx.android.synthetic.main.fragment_home.* class MainActivity : AppCompatActivity() { @@ -21,13 +23,7 @@ class MainActivity : AppCompatActivity() { } override fun onCreate(savedInstanceState: Bundle?) { - /*if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), - ManifestRelatedVariables.REQUEST_READ_EXTERNAL) - } */ loadSettings() - ServerManagement.serverManager.addActivityConnection(this, "main",0) - ServerManagement.serverManager.getData(this, this, 0, "", "", true) setTheme(getThemeId()) super.onCreate(savedInstanceState) @@ -37,8 +33,36 @@ class MainActivity : AppCompatActivity() { mainBottomNavigationView.setupWithNavController(navController) handleExtras() + } - println("[debug] ${getDataFromServer()}") + override fun onResume() { + super.onResume() + // server communication + + val dataReceiver: (String) -> Unit = {data: String -> + println("Data here: $data") + + when (mainFragmentHost.childFragmentManager.fragments[0]) { + is chatFragment -> {} + is exploreFragment -> {} + is homeFragment -> { + val view = mainFragmentHost.childFragmentManager.fragments[0].homeFragmentTextIdTest + view.post { + view.text = data + } + } + is mapFragment -> {} + is settingsFragment -> {} + } + + } + + ServerManagement.serverManager.addReceiverConnection(dataReceiver, this, "mainConnection", 0, "test0.json") + } + + override fun onPause() { + super.onPause() + ServerManagement.serverManager.deleteConnection("mainConnection") } private fun handleExtras() { @@ -55,5 +79,4 @@ class MainActivity : AppCompatActivity() { val settingsSaveManager = SettingsSaveManager(this) settingsSaveManager.loadSettings() } - } diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/activityExtentions.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/activityExtentions.kt deleted file mode 100644 index c1d117f..0000000 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/activityExtentions.kt +++ /dev/null @@ -1,18 +0,0 @@ -package com.example.wikispot - -import android.app.Activity -import android.os.Build -import androidx.annotation.RequiresApi -import kotlinx.android.synthetic.main.activity_main.* - -// for serverManager -fun Activity.receiveData(data: String) { - - when (this.localClassName) { - "activities.MainActivity" -> { this.receiveDataForMainActivity(data) } - } -} - -fun Activity.receiveDataForMainActivity(data: String) { - -} \ No newline at end of file diff --git a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/anotherDebugFragment.kt b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/anotherDebugFragment.kt index 86e38de..6fa76ae 100644 --- a/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/anotherDebugFragment.kt +++ b/app/WikiSpot/app/src/main/java/com/example/wikispot/fragments/anotherDebugFragment.kt @@ -45,11 +45,10 @@ class anotherDebugFragment : Fragment(R.layout.fragment_another_debug) { createViewConnectionBtn.setOnClickListener { val attributePath = attributePathInput.text.toString() - val setWholeContent = wholeContentCheckBox.isChecked val filePath = filePathInput.text.toString() ServerManagement.serverManager.deleteConnection("debug", "view") - ServerManagement.serverManager.addViewConnection(requireContext(), dataContentView, "debug",0, filePath, attributePath, setWholeContent) + ServerManagement.serverManager.addViewConnection(requireContext(), dataContentView, "debug",0, filePath, attributePath) } // handling navigation between debug fragments 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 c753c7f..c2b8c0a 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 @@ -6,7 +6,28 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.example.wikispot.R +import com.example.wikispot.getStringFromSharedPreferences +import com.example.wikispot.saveString +import kotlinx.android.synthetic.main.fragment_home.* class homeFragment : Fragment(R.layout.fragment_home) { + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + loadCache() + } + + override fun onPause() { + super.onPause() + saveCache() + } + + private fun loadCache() { + homeFragmentTextIdTest.text = requireContext().getStringFromSharedPreferences("title", "homeFragmentCache" ) + } + + private fun saveCache() { + requireContext().saveString("title", homeFragmentTextIdTest.text.toString(), "homeFragmentCache") + } } \ No newline at end of file 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 8b50b8d..0c4f388 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 @@ -1,31 +1,30 @@ package com.example.wikispot.modelClasses -import android.app.Activity import android.content.Context import android.widget.TextView import com.example.wikispot.ServerManagement -import com.example.wikispot.receiveData import okhttp3.* import org.json.JSONArray +import org.json.JSONObject import java.io.IOException class ServerManager { - private var activityConnections = mutableListOf() + private var receiverConnections = mutableListOf() private var viewConnections = mutableListOf() - fun getData(activity: Activity, context: Context, serverId: Int, path: String, attributePath: String, getWholeContent: Boolean=false, numberOfAttempts: Int=2) { - val dataRequestThread = Thread(DataRequest(activity, context, serverId, path, attributePath, getWholeContent, numberOfAttempts)) + fun getData(dataReceiver: (String) -> Unit, context: Context, serverId: Int, path: String, attributePath: String="", numberOfAttempts: Int=2) { + val dataRequestThread = Thread(DataRequest(dataReceiver, context, serverId, path, attributePath, numberOfAttempts)) dataRequestThread.start() } - inner class DataRequest(val activity: Activity, val context: Context, val serverId: Int, val path: String="", val attributePath: String, var getWholeContent: Boolean=false, val numberOfAttempts: Int=2): Runnable{ + inner class DataRequest(val dataReceiver: (String) -> Unit, val context: Context, val serverId: Int, val path: String="", val attributePath: String, private val numberOfAttempts: Int=2): Runnable{ override fun run() { for (n in 0 until numberOfAttempts) { - var url = "http://192.168.1.230:8000/devices_list" + var url = "${ServerManagement.baseUrl}devices_list" if (path != "") { - url = "http://192.168.1.230:8000/files/$serverId/$path" + url = "${ServerManagement.baseUrl}files/$serverId/$path" } val request = Request.Builder().url(url).build() @@ -47,21 +46,31 @@ class ServerManager { jsonManager.getJsonObject(serverId) } else { if (attributePath == "") { - getWholeContent = true + throw Throwable() } } - if (getWholeContent) { - activity.receiveData(jsonManager.currentJsonObject.toString()) - println("[debug] testing ${activity.localClassName} ; ${activity.componentName} ; ${activity.packageName}") - } else if(attributePath != "") { - activity.receiveData(jsonManager.getAttributeContentByPath(attributePath)) + if(attributePath != "") { + dataReceiver(jsonManager.getAttributeContentByPath(attributePath)) } else { - println("[debug] path or whole content needs to be chosen") + dataReceiver(jsonManager.currentJsonObject.toString()) } } catch (exception: Throwable) { - activity.receiveData(receivedString) + try { + JSONObject(receivedString) + + val jsonManager = JsonManager(context, receivedString, "JSONObject") + + if(attributePath != "") { + dataReceiver(jsonManager.getAttributeContentByPath(attributePath)) + } else { + dataReceiver(jsonManager.currentJsonObject.toString()) + } + + } catch (exception: Throwable) { + dataReceiver(receivedString) + } } } } @@ -81,10 +90,10 @@ class ServerManager { fun deleteConnection(connectionName: String, connectionType: String="any") { // other types are any, activity and view if ((connectionType == "any") or (connectionType == "activity")) { - for (i in 0 until activityConnections.size) { // checking in connections - if (activityConnections[i].connectionName == connectionName) { - activityConnections[i].running = false - activityConnections.removeAt(i) + for (i in 0 until receiverConnections.size) { // checking in connections + if (receiverConnections[i].connectionName == connectionName) { + receiverConnections[i].running = false + receiverConnections.removeAt(i) } } } @@ -99,11 +108,11 @@ class ServerManager { } } - fun addActivityConnection(activity: Activity, connectionName: String, serverId: Int, path: String?=null) { - activityConnections.add(ActivityConnection(activity, connectionName, serverId, path)) + fun addReceiverConnection(dataReceiver: (String) -> Unit, context: Context, connectionName: String, serverId: Int, path: String?=null, attributePath: String="") { + receiverConnections.add(ReceiverConnection(dataReceiver, context, connectionName, serverId, path, attributePath)) } - inner class ActivityConnection(val activity: Activity, val connectionName: String, val serverId: Int, val path: String?=null) { + inner class ReceiverConnection(val dataReceiver: (String) -> Unit, val context: Context, val connectionName: String, val serverId: Int, val path: String?=null, val attributePath: String) { var running = true @@ -115,36 +124,10 @@ class ServerManager { inner class CheckingServerData : Runnable { override fun run() { while (running) { - - println("[debug] connection thread running") - - Thread.sleep(ServerManagement.activityConnectionOnCheckWait) - } - } - } - - } - - fun addViewConnection(context: Context, view: TextView, connectionName: String, serverId: Int, path: String="", attributePath: String, getWholeContent: Boolean=false) { - viewConnections.add(ViewConnection(context, view, connectionName, serverId, path, attributePath, getWholeContent)) - } - - inner class ViewConnection(val context: Context, val view: TextView, val connectionName: String, val serverId: Int, val path: String="", var attributePath: String, var getWholeContent: Boolean=false) { - - var running = true - - init { - val checkingServerDataThread = Thread(CheckingServerData()) - checkingServerDataThread.start() - } - - inner class CheckingServerData: Runnable { - override fun run() { - while (running) { - var url = "http://192.168.1.230:8000/devices_list" + var url = "${ServerManagement.baseUrl}devices_list" if (path != "") { - url = "http://192.168.1.230:8000/files/$serverId/$path" + url = "${ServerManagement.baseUrl}files/$serverId/$path" } val request = Request.Builder().url(url).build() @@ -170,21 +153,120 @@ class ServerManager { } } - if (getWholeContent) { - view.post { - view.text = jsonManager.currentJsonObject.toString() + if(attributePath != "") { + dataReceiver(jsonManager.getAttributeContentByPath(attributePath)) + } else { + dataReceiver(jsonManager.currentJsonObject.toString()) + } + + } catch (exception: Throwable) { + try { + JSONObject(receivedString) + + val jsonManager = JsonManager(context, receivedString, "JSONObject") + + if(attributePath != "") { + dataReceiver(jsonManager.getAttributeContentByPath(attributePath)) + } else { + dataReceiver(jsonManager.currentJsonObject.toString()) } - } else if(attributePath != "") { + + } catch (exception: Throwable) { + dataReceiver(receivedString) + } + } + } + } + + override fun onFailure(call: Call, e: IOException) { + println("Request Failed") + println(e) + } + }) + + Thread.sleep(ServerManagement.dataRequestOnAttemptWait) + } + } + } + + } + + fun addViewConnection(context: Context, view: TextView, connectionName: String, serverId: Int, path: String="", attributePath: String="") { + viewConnections.add(ViewConnection(context, view, connectionName, serverId, path, attributePath)) + } + + inner class ViewConnection(val context: Context, val view: TextView, val connectionName: String, val serverId: Int, val path: String="", var attributePath: String) { + + var running = true + + init { + val checkingServerDataThread = Thread(CheckingServerData()) + checkingServerDataThread.start() + } + + inner class CheckingServerData: Runnable { + override fun run() { + while (running) { + var url = "${ServerManagement.baseUrl}devices_list" + + if (path != "") { + url = "${ServerManagement.baseUrl}files/$serverId/$path" + } + + val request = Request.Builder().url(url).build() + val client = OkHttpClient() + + client.newCall(request).enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + response.body?.let { + val receivedString = response.body!!.string() + + if (receivedString == "Internal Server Error") { + return + } + + try { + JSONArray(receivedString) + + val jsonManager = JsonManager(context, receivedString) + if (path == "") { + jsonManager.getJsonObject(serverId) + } else { + if (attributePath == "") { + throw Throwable() + } + } + + if(attributePath != "") { view.post { view.text = jsonManager.getAttributeContentByPath(attributePath) } } else { - println("[debug] path or whole content needs to be chosen") + view.post { + view.text = jsonManager.currentJsonObject.toString() + } } } catch (exception: Throwable) { - view.post { - view.text = receivedString + try { + JSONObject(receivedString) + + val jsonManager = JsonManager(context, receivedString, "JSONObject") + + if(attributePath != "") { + view.post { + view.text = jsonManager.getAttributeContentByPath(attributePath) + } + } else { + view.post { + view.text = jsonManager.currentJsonObject.toString() + } + } + + } catch (exception: Throwable) { + view.post { + view.text = receivedString + } } } } 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 7a99e0c..0e5e98d 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 @@ -21,12 +21,13 @@ object ServerManagement { const val activityConnectionOnCheckWait: Long = 4000 const val viewConnectionOnCheckWait: Long = 5000 const val dataRequestOnAttemptWait: Long = 2000 + const val baseUrl = "http://192.168.1.230:8000/" } object ThemeOptions { var darkTheme = false - var actionBar = true + var actionBar = false } diff --git a/app/WikiSpot/app/src/main/res/layout/activity_main.xml b/app/WikiSpot/app/src/main/res/layout/activity_main.xml index 4838199..91e90b0 100644 --- a/app/WikiSpot/app/src/main/res/layout/activity_main.xml +++ b/app/WikiSpot/app/src/main/res/layout/activity_main.xml @@ -28,4 +28,5 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/main_activity_navigation" /> + \ No newline at end of file diff --git a/app/WikiSpot/app/src/main/res/layout/fragment_another_debug.xml b/app/WikiSpot/app/src/main/res/layout/fragment_another_debug.xml index 42143cd..dfd3a38 100644 --- a/app/WikiSpot/app/src/main/res/layout/fragment_another_debug.xml +++ b/app/WikiSpot/app/src/main/res/layout/fragment_another_debug.xml @@ -73,21 +73,6 @@ android:textAlignment="center" /> - -