more changes

This commit is contained in:
Tucan444 2021-03-22 20:14:02 +01:00
parent 4ef7318c27
commit c5ed73adb4
18 changed files with 219 additions and 114 deletions

@ -14,8 +14,9 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"
android:theme="@style/Theme.WikiSpotWithActionBar"> android:theme="@style/Theme.WikiSpot">
<activity android:name=".activities.MainActivity"> <activity android:name=".activities.MainActivity"
android:screenOrientation="portrait">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

@ -42,7 +42,7 @@ fun Context.getThemeId(): Int {
// for client // for client
fun Context.getDataFromServer(): String { fun Context.getDataFromServer(): String {
// requesting data // 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 request = Request.Builder().url(url).build()
val client = OkHttpClient() val client = OkHttpClient()
var receivedResponse = "" var receivedResponse = ""

@ -6,8 +6,10 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController import androidx.navigation.ui.setupWithNavController
import com.example.wikispot.* import com.example.wikispot.*
import com.example.wikispot.fragments.*
import com.example.wikispot.modelClasses.SettingsSaveManager import com.example.wikispot.modelClasses.SettingsSaveManager
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_home.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -21,13 +23,7 @@ class MainActivity : AppCompatActivity() {
} }
override fun onCreate(savedInstanceState: Bundle?) { 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() loadSettings()
ServerManagement.serverManager.addActivityConnection(this, "main",0)
ServerManagement.serverManager.getData(this, this, 0, "", "", true)
setTheme(getThemeId()) setTheme(getThemeId())
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -37,8 +33,36 @@ class MainActivity : AppCompatActivity() {
mainBottomNavigationView.setupWithNavController(navController) mainBottomNavigationView.setupWithNavController(navController)
handleExtras() 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() { private fun handleExtras() {
@ -55,5 +79,4 @@ class MainActivity : AppCompatActivity() {
val settingsSaveManager = SettingsSaveManager(this) val settingsSaveManager = SettingsSaveManager(this)
settingsSaveManager.loadSettings() settingsSaveManager.loadSettings()
} }
} }

@ -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) {
}

@ -45,11 +45,10 @@ class anotherDebugFragment : Fragment(R.layout.fragment_another_debug) {
createViewConnectionBtn.setOnClickListener { createViewConnectionBtn.setOnClickListener {
val attributePath = attributePathInput.text.toString() val attributePath = attributePathInput.text.toString()
val setWholeContent = wholeContentCheckBox.isChecked
val filePath = filePathInput.text.toString() val filePath = filePathInput.text.toString()
ServerManagement.serverManager.deleteConnection("debug", "view") 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 // handling navigation between debug fragments

@ -6,7 +6,28 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.example.wikispot.R 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) { 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")
}
} }

@ -1,31 +1,30 @@
package com.example.wikispot.modelClasses package com.example.wikispot.modelClasses
import android.app.Activity
import android.content.Context import android.content.Context
import android.widget.TextView import android.widget.TextView
import com.example.wikispot.ServerManagement import com.example.wikispot.ServerManagement
import com.example.wikispot.receiveData
import okhttp3.* import okhttp3.*
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException import java.io.IOException
class ServerManager { class ServerManager {
private var activityConnections = mutableListOf<ActivityConnection>() private var receiverConnections = mutableListOf<ReceiverConnection>()
private var viewConnections = mutableListOf<ViewConnection>() private var viewConnections = mutableListOf<ViewConnection>()
fun getData(activity: Activity, context: Context, serverId: Int, path: String, attributePath: String, getWholeContent: Boolean=false, numberOfAttempts: Int=2) { fun getData(dataReceiver: (String) -> Unit, context: Context, serverId: Int, path: String, attributePath: String="", numberOfAttempts: Int=2) {
val dataRequestThread = Thread(DataRequest(activity, context, serverId, path, attributePath, getWholeContent, numberOfAttempts)) val dataRequestThread = Thread(DataRequest(dataReceiver, context, serverId, path, attributePath, numberOfAttempts))
dataRequestThread.start() 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() { override fun run() {
for (n in 0 until numberOfAttempts) { for (n in 0 until numberOfAttempts) {
var url = "http://192.168.1.230:8000/devices_list" var url = "${ServerManagement.baseUrl}devices_list"
if (path != "") { 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() val request = Request.Builder().url(url).build()
@ -47,21 +46,31 @@ class ServerManager {
jsonManager.getJsonObject(serverId) jsonManager.getJsonObject(serverId)
} else { } else {
if (attributePath == "") { if (attributePath == "") {
getWholeContent = true throw Throwable()
} }
} }
if (getWholeContent) { if(attributePath != "") {
activity.receiveData(jsonManager.currentJsonObject.toString()) dataReceiver(jsonManager.getAttributeContentByPath(attributePath))
println("[debug] testing ${activity.localClassName} ; ${activity.componentName} ; ${activity.packageName}")
} else if(attributePath != "") {
activity.receiveData(jsonManager.getAttributeContentByPath(attributePath))
} else { } else {
println("[debug] path or whole content needs to be chosen") dataReceiver(jsonManager.currentJsonObject.toString())
} }
} catch (exception: Throwable) { } 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 fun deleteConnection(connectionName: String, connectionType: String="any") { // other types are any, activity and view
if ((connectionType == "any") or (connectionType == "activity")) { if ((connectionType == "any") or (connectionType == "activity")) {
for (i in 0 until activityConnections.size) { // checking in connections for (i in 0 until receiverConnections.size) { // checking in connections
if (activityConnections[i].connectionName == connectionName) { if (receiverConnections[i].connectionName == connectionName) {
activityConnections[i].running = false receiverConnections[i].running = false
activityConnections.removeAt(i) receiverConnections.removeAt(i)
} }
} }
} }
@ -99,11 +108,11 @@ class ServerManager {
} }
} }
fun addActivityConnection(activity: Activity, connectionName: String, serverId: Int, path: String?=null) { fun addReceiverConnection(dataReceiver: (String) -> Unit, context: Context, connectionName: String, serverId: Int, path: String?=null, attributePath: String="") {
activityConnections.add(ActivityConnection(activity, connectionName, serverId, path)) 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 var running = true
@ -115,36 +124,10 @@ class ServerManager {
inner class CheckingServerData : Runnable { inner class CheckingServerData : Runnable {
override fun run() { override fun run() {
while (running) { while (running) {
var url = "${ServerManagement.baseUrl}devices_list"
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"
if (path != "") { 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() val request = Request.Builder().url(url).build()
@ -170,21 +153,120 @@ class ServerManager {
} }
} }
if (getWholeContent) { if(attributePath != "") {
view.post { dataReceiver(jsonManager.getAttributeContentByPath(attributePath))
view.text = jsonManager.currentJsonObject.toString() } 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.post {
view.text = jsonManager.getAttributeContentByPath(attributePath) view.text = jsonManager.getAttributeContentByPath(attributePath)
} }
} else { } else {
println("[debug] path or whole content needs to be chosen") view.post {
view.text = jsonManager.currentJsonObject.toString()
}
} }
} catch (exception: Throwable) { } catch (exception: Throwable) {
view.post { try {
view.text = receivedString 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
}
} }
} }
} }

@ -21,12 +21,13 @@ object ServerManagement {
const val activityConnectionOnCheckWait: Long = 4000 const val activityConnectionOnCheckWait: Long = 4000
const val viewConnectionOnCheckWait: Long = 5000 const val viewConnectionOnCheckWait: Long = 5000
const val dataRequestOnAttemptWait: Long = 2000 const val dataRequestOnAttemptWait: Long = 2000
const val baseUrl = "http://192.168.1.230:8000/"
} }
object ThemeOptions { object ThemeOptions {
var darkTheme = false var darkTheme = false
var actionBar = true var actionBar = false
} }

@ -28,4 +28,5 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_activity_navigation" /> app:navGraph="@navigation/main_activity_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -73,21 +73,6 @@
android:textAlignment="center" /> android:textAlignment="center" />
</ScrollView> </ScrollView>
<CheckBox
android:id="@+id/wholeContentCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:shadowColor="#B4B4B4"
android:text="Whole Content"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
app:layout_constraintStart_toStartOf="@+id/attributePathInput"
app:layout_constraintTop_toBottomOf="@+id/attributePathInput"
app:useMaterialThemeColors="false" />
<EditText <EditText
android:id="@+id/attributePathInput" android:id="@+id/attributePathInput"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@ -103,11 +88,10 @@
android:id="@+id/createViewConnectionBtn" android:id="@+id/createViewConnectionBtn"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="Create View Connection" android:text="Create View Connection"
android:textSize="12sp" android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/wholeContentCheckBox" app:layout_constraintStart_toStartOf="@+id/attributePathInput"
app:layout_constraintTop_toBottomOf="@+id/attributePathInput" /> app:layout_constraintTop_toBottomOf="@+id/attributePathInput" />
<Button <Button
@ -118,8 +102,8 @@
android:text="Stop Connection" android:text="Stop Connection"
android:textSize="10sp" android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="@+id/attributePathInput" app:layout_constraintBottom_toBottomOf="@+id/attributePathInput"
app:layout_constraintEnd_toEndOf="@+id/createViewConnectionBtn" app:layout_constraintStart_toEndOf="@+id/attributePathInput"
app:layout_constraintStart_toEndOf="@+id/attributePathInput" /> app:layout_constraintTop_toTopOf="@+id/attributePathInput" />
<EditText <EditText
android:id="@+id/filePathInput" android:id="@+id/filePathInput"

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#E11976D2" android:background="@color/chatFragmentBackground"
tools:context=".fragments.chatFragment"> tools:context=".fragments.chatFragment">
<TextView <TextView

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#2EBFB2" android:background="@color/exploreFragmentBackground"
tools:context=".fragments.exploreFragment"> tools:context=".fragments.exploreFragment">
<TextView <TextView

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#7CB342" android:background="@color/homeFragmentBackground"
tools:context=".fragments.homeFragment"> tools:context=".fragments.homeFragment">
<TextView <TextView
@ -14,6 +14,7 @@
android:textColor="@android:color/white" android:textColor="@android:color/white"
android:textSize="24sp" android:textSize="24sp"
android:textStyle="bold" android:textStyle="bold"
android:id="@+id/homeFragmentTextIdTest"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5" app:layout_constraintHorizontal_bias="0.5"

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#FBC02D" android:background="@color/mapFragmentBackground"
tools:context=".fragments.mapFragment"> tools:context=".fragments.mapFragment">
<TextView <TextView

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#F57C00" android:background="@color/settingsFragmentBackground"
tools:context=".fragments.settingsFragment"> tools:context=".fragments.settingsFragment">
<TextView <TextView

@ -9,6 +9,7 @@
<item name="colorSecondary">@color/teal_200</item> <item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item> <item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item> <item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. --> <!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. --> <!-- Customize your theme here. -->

@ -7,4 +7,11 @@
<color name="teal_700">#FF018786</color> <color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color> <color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color> <color name="white">#FFFFFFFF</color>
<color name="chatFragmentBackground">#E11976D2</color>
<color name="exploreFragmentBackground">#2EBFB2</color>
<color name="homeFragmentBackground">#7CB342</color>
<color name="mapFragmentBackground">#FBC02D</color>
<color name="settingsFragmentBackground">#F57C00</color>
</resources> </resources>

@ -9,6 +9,7 @@
<item name="colorSecondary">#E91E63</item> <item name="colorSecondary">#E91E63</item>
<item name="colorSecondaryVariant">#F48FB1</item> <item name="colorSecondaryVariant">#F48FB1</item>
<item name="colorOnSecondary">@color/black</item> <item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. --> <!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
@ -24,6 +25,7 @@
<item name="colorSecondary">#E91E63</item> <item name="colorSecondary">#E91E63</item>
<item name="colorSecondaryVariant">#F48FB1</item> <item name="colorSecondaryVariant">#F48FB1</item>
<item name="colorOnSecondary">@color/black</item> <item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. --> <!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item> <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. --> <!-- Customize your theme here. -->