This commit is contained in:
Tucan444 2021-04-21 14:39:20 +02:00
parent 8737a5a471
commit 4aa55cd35e
24 changed files with 142 additions and 56 deletions

@ -13,7 +13,6 @@ import com.example.wikispot.modelsForAdapters.MessagesSupplier
import kotlinx.android.synthetic.main.fragment_chat.*
import okhttp3.*
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.json.JSONArray
import java.io.IOException
@ -44,7 +43,7 @@ class chatFragment : Fragment(R.layout.fragment_chat) {
override fun onResume() {
super.onResume()
loadNamesCache()
loadIdFromCache()
val dataReceiver: (String) -> Unit = { data: String ->
val json = JsonManager(requireContext(), data, "JSONObject")
@ -56,7 +55,7 @@ class chatFragment : Fragment(R.layout.fragment_chat) {
val r = requireContext().getRandomGenerator(GeneralVariables.id!!)
GeneralVariables.name = "${NamesDatabase.names[r.nextInt(NamesDatabase.names.size)]} - ${r.nextInt(9999).toString()}"
GeneralVariables.name = "${NamesDatabase.names[r.nextInt(NamesDatabase.names.size)]} - ${r.nextInt(9999)}"
json.getAttributeContent("data")
json.getAttributeContent("1")
@ -79,6 +78,8 @@ class chatFragment : Fragment(R.layout.fragment_chat) {
val length = json.currentJsonAttribute1!!.length()
json.clearSelectedAttribute()
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")
@ -86,17 +87,10 @@ class chatFragment : Fragment(R.layout.fragment_chat) {
jsonOfMessage.getAttributeContent("message"),
jsonOfMessage.getAttributeContent("timestamp"))
val lastMessageSentIndex = MessagesSupplier.getIndexOfLastMessageFromSelf()
if (message.senderId != GeneralVariables.id) {
if (!MessagesSupplier.checkIfContains(message)) {
MessagesSupplier.appendMessage(message)
updateRecyclerView()
}
} else {
lastMessageSentIndex?.let {
MessagesSupplier.messages[lastMessageSentIndex]!!.timestamp = jsonOfMessage.getAttributeContent("timestamp")
}
}
}
}
}
@ -108,7 +102,7 @@ class chatFragment : Fragment(R.layout.fragment_chat) {
override fun onPause() {
super.onPause()
saveNamesCache()
saveIdToCache()
ServerManagement.serverManager.deleteConnection("chatConnection")
}
@ -165,31 +159,17 @@ class chatFragment : Fragment(R.layout.fragment_chat) {
// loading and saving last names
private fun loadNamesCache() {
val namesCache = requireContext().getStringFromSharedPreferences("namesCache", "chatPreferences")
if (namesCache != "") {
ChatManagement.lastNames = JSONArray(namesCache)
private fun loadIdFromCache() {
val id = requireContext().getStringFromSharedPreferences("id")
if (id != "") {
GeneralVariables.id = id
}
}
private fun saveNamesCache() {
private fun saveIdToCache() {
GeneralVariables.id?.let {
ChatManagement.lastNames.let {
val namesCache = JSONArray()
var subtractAmount = it.length()
if (it.length() > (ChatManagement.numberOfNamesToCache - 1)) {
subtractAmount = ChatManagement.numberOfNamesToCache - 1
}
for (i in it.length() - subtractAmount until it.length()) {
namesCache.put(it[i])
}
namesCache.put(GeneralVariables.id)
requireContext().saveString("namesCache", namesCache.toString(), "chatPreferences")
}
requireContext().saveString("id", GeneralVariables.id!!)
}
}

@ -510,7 +510,6 @@ class ServerManager {
if (receivedString == "Internal Server Error") {
return
}
val returnJsonObject = JSONObject()
returnJsonObject.put("source", "messages/get")
returnJsonObject.put("data", JSONArray(receivedString))

@ -11,7 +11,7 @@ data class Message(var senderId: String, val content: String, var timestamp: Str
init {
val r = getRandomGenerator(senderId)
senderName = "${NamesDatabase.names[r.nextInt(NamesDatabase.names.size)]} - ${r.nextInt(9999).toString()}"
senderName = "${NamesDatabase.names[r.nextInt(NamesDatabase.names.size)]} - ${r.nextInt(9999)}"
}
private fun getRandomGenerator(seedString: String): Random {
@ -36,14 +36,22 @@ object MessagesSupplier {
messages = array
}
fun checkIfContains(message: Message): Boolean {
fun deleteMessageByIndex(i: Int) {
messages = messages.copyOfRange(0, i) + messages.copyOfRange(i + 1, messages.size)
}
fun checkIfContains(message: Message, checkTimestamp: Boolean=true): Boolean {
for (i in messages.indices) {
messages[i]?.let {
if (message.senderId == it.senderId) {
if (message.content == it.content) {
if (checkTimestamp) {
if (message.timestamp == it.timestamp) {
return true
}
} else {
return true
}
}
}
}
@ -51,6 +59,22 @@ object MessagesSupplier {
return false
}
fun clearWaitingMessages() {
val positionsOfItemsToRemove = mutableListOf<Int>()
for (i in messages.indices) {
if (messages[i]!!.timestamp == "waiting") {
positionsOfItemsToRemove.add(i)
println("waiting at: $i")
}
}
var subtractAmount = 0
for (index in positionsOfItemsToRemove) {
deleteMessageByIndex(index - subtractAmount)
subtractAmount += 1
}
}
fun getIndexOfLastMessageFromSelf(): Int? {
var i: Int? = null
for (n in messages.indices) {

@ -3,8 +3,8 @@
android:shape="rectangle">
<gradient
android:startColor="#C4FFA5"
android:centerColor="#D1FBA0"
android:endColor="#F6FEBA"/>
android:startColor="?attr/contrastingGradientStartColor"
android:centerColor="?attr/contrastingGradientCenterColor"
android:endColor="?attr/contrastingGradientEndColor"/>
</shape>

@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
android:tint="?attr/generalIconsColor">
<path
android:fillColor="@android:color/white"
android:pathData="M7,10l5,5 5,-5z"/>

@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
android:tint="?attr/generalIconsColor">
<path
android:fillColor="@android:color/white"
android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM6,9h12v2L6,11L6,9zM14,14L6,14v-2h8v2zM18,8L6,8L6,6h12v2z"/>

@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
android:tint="?attr/generalIconsColor">
<path
android:fillColor="@android:color/white"
android:pathData="M5,20h14v-2H5V20zM19,9h-4V3H9v6H5l7,7L19,9z"/>

@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
android:tint="?attr/generalIconsColor">
<path
android:fillColor="@android:color/white"
android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>

@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal"
android:tint="?attr/generalIconsColor"
android:autoMirrored="true">
<path
android:fillColor="@android:color/white"

@ -3,7 +3,7 @@
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
android:tint="?attr/generalIconsColor">
<path
android:fillColor="@android:color/white"
android:pathData="M12,2C8.13,2 5,5.13 5,9c0,5.25 7,13 7,13s7,-7.75 7,-13c0,-3.87 -3.13,-7 -7,-7zM12,11.5c-1.38,0 -2.5,-1.12 -2.5,-2.5s1.12,-2.5 2.5,-2.5 2.5,1.12 2.5,2.5 -1.12,2.5 -2.5,2.5z"/>

@ -29,10 +29,10 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="8dp"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="?attr/generalTextColor"
card_view:layout_constraintStart_toEndOf="@+id/item_img"
card_view:layout_constraintTop_toTopOf="parent"
tools:text="Title" />
@ -41,6 +41,7 @@
android:id="@+id/item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="?attr/generalTextColor"
tools:text="Description"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toStartOf="@+id/item_location_img"
@ -57,8 +58,7 @@
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintTop_toTopOf="parent"
card_view:srcCompat="@drawable/loacation_vector_asset"
card_view:tint="#F1E79F" />
card_view:srcCompat="@drawable/loacation_vector_asset" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -25,6 +25,7 @@
android:layout_marginTop="36dp"
android:textSize="24sp"
android:textStyle="bold"
android:textColor="?attr/generalTextColor"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent"
tools:text="Filename" />

@ -38,6 +38,7 @@
android:layout_marginEnd="5dp"
android:ems="10"
android:inputType="textPersonName"
android:textColor="?attr/generalTextColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/sendMessageBtn"
app:layout_constraintStart_toStartOf="parent"

@ -21,7 +21,7 @@
android:background="@drawable/text_background_gradient"
android:padding="5dp"
android:text="@string/home"
android:textColor="@color/white"
android:textColor="?attr/textOnTextBgColor"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"

@ -25,7 +25,7 @@
android:background="@drawable/text_background_gradient"
android:padding="5dp"
android:text=""
android:textColor="#fff"
android:textColor="?attr/textOnTextBgColor"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
@ -51,6 +51,8 @@
android:layout_marginTop="32dp"
android:text=""
android:textAlignment="center"
android:textSize="18sp"
android:textColor="?attr/generalTextColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
@ -75,7 +77,7 @@
android:padding="4dp"
android:text="@string/files"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textColor="?attr/textOnTextBgColor"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"

@ -25,7 +25,7 @@
android:text="@string/dark_theme"
android:background="@drawable/text_background_gradient"
android:padding="5dp"
android:textColor="#FFFFFF"
android:textColor="?attr/textOnTextBgColor"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"

@ -21,8 +21,8 @@
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="TextView"
android:textSize="40sp"
android:textColor="?attr/generalTextColor"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintStart_toStartOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />
@ -34,8 +34,8 @@
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="16dp"
android:text="TextView"
android:textSize="40sp"
android:textColor="?attr/generalTextColor"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintTop_toTopOf="parent" />

@ -23,6 +23,7 @@
android:layout_margin="2dp"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="?attr/generalTextColor"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintHorizontal_bias="0.5"
card_view:layout_constraintStart_toStartOf="parent"
@ -37,6 +38,7 @@
android:textAlignment="textStart"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="?attr/generalTextColor"
card_view:layout_constraintBottom_toBottomOf="parent"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintHorizontal_bias="0.5"

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WikiSpot</string>
<string name="chat">Chat</string>
<string name="explore">Explorar</string>
<string name="home">Casa</string>
<string name="map">Mapa</string>
<string name="settings">Ajustes</string>
<string name="files">Archivos</string>
<string name="dark_theme">Tema oscuro</string>
</resources>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WikiSpot</string>
<string name="dark_theme">Thème sombre</string>
<string name="files">Des dossiers</string>
<string name="settings">Paramètres</string>
<string name="map">Carte</string>
<string name="home">Domicile</string>
<string name="explore">Explorer</string>
<string name="chat">Discuter</string>
</resources>

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WikiSpot</string>
<string name="home"></string>
<string name="map">地图</string>
<string name="explore">探索</string>
<string name="chat">聊天</string>
<string name="settings">设定值</string>
<string name="files">档案文件</string>
<string name="dark_theme">黑暗主题</string>
</resources>

@ -1,6 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- General -->
<attr name="textOnTextBgColor" format="color" />
<attr name="generalIconsColor" format="color" />
<attr name="generalTextColor" format="color" />
<!-- For Backgrounds -->
<attr name="textBackgroundGradientStartColor" format="color"/>
@ -32,6 +38,10 @@
<attr name="infoFragmentGradientCenterColor" format="color"/>
<attr name="infoFragmentGradientEndColor" format="color"/>
<attr name="contrastingGradientStartColor" format="color"/>
<attr name="contrastingGradientCenterColor" format="color"/>
<attr name="contrastingGradientEndColor" format="color"/>
<!-- For bottomNavBar -->
<attr name="bottomNavBarCheckedItemColor" format="color"/>
<attr name="bottomNavBarRippleColor" format="color"/>

@ -12,6 +12,10 @@
<color name="secondaryVariant">#F48FB1</color>
<color name="onSecondary">@color/black</color>
<color name="textOnTextBgColor">#fff</color>
<color name="generalIconsColor">#89000000</color>
<color name="generalTextColor">#89000000</color>
<color name="bottomNavBarCheckedItemColor">#ECD21A1A</color>
<color name="bottomNavBarItemColor">#320747</color>
<color name="bottomNavBarRippleColor">#33C4BCC9</color>
@ -46,6 +50,10 @@
<color name="infoFragmentGradientCenterColor" >#A7D2FF</color>
<color name="infoFragmentGradientEndColor" >#96E7FF</color>
<color name="contrastingGradientStartColor">#C4FFA5</color>
<color name="contrastingGradientCenterColor">#D1FBA0</color>
<color name="contrastingGradientEndColor">#F6FEBA</color>
<!-- Dark Theme -->
<color name="primaryDark">#B078F4</color>
<color name="primaryVariantDark">#5100B3</color>
@ -55,6 +63,10 @@
<color name="secondaryVariantDark">#FF03DAC5</color>
<color name="onSecondaryDark">@color/black</color>
<color name="textOnTextBgColorDark">#aaa</color>
<color name="generalIconsColorDark">#96FFFFFF</color>
<color name="generalTextColorDark">#AAFFFFFF</color>
<color name="bottomNavBarCheckedItemColorDark">#FFB571</color>
<color name="bottomNavBarItemColorDark">#320747</color>
<color name="bottomNavBarRippleColorDark">#33222222</color>
@ -89,4 +101,8 @@
<color name="infoFragmentGradientCenterColorDark" >#69829D</color>
<color name="infoFragmentGradientEndColorDark" >#54818E</color>
<color name="contrastingGradientStartColorDark">#729360</color>
<color name="contrastingGradientCenterColorDark">#809A62</color>
<color name="contrastingGradientEndColorDark">#8F936C</color>
</resources>

@ -12,6 +12,11 @@
<item name="colorSecondaryVariant">@color/secondaryVariant</item>
<item name="colorOnSecondary">@color/onSecondary</item>
<!-- General -->
<item name="textOnTextBgColor">@color/textOnTextBgColor</item>
<item name="generalIconsColor">@color/generalIconsColor</item>
<item name="generalTextColor">@color/generalTextColor</item>
<!-- For Backgrounds -->
<item name="textBackgroundGradientStartColor" >@color/textBackgroundGradientStartColor</item>
@ -43,6 +48,10 @@
<item name="infoFragmentGradientCenterColor" >@color/infoFragmentGradientCenterColor</item>
<item name="infoFragmentGradientEndColor" >@color/infoFragmentGradientEndColor</item>
<item name="contrastingGradientStartColor">@color/contrastingGradientStartColor</item>
<item name="contrastingGradientCenterColor">@color/contrastingGradientCenterColor</item>
<item name="contrastingGradientEndColor">@color/contrastingGradientEndColor</item>
<!-- BottomNavCheckedItemColor -->
<item name="bottomNavBarCheckedItemColor">@color/bottomNavBarCheckedItemColor</item>
<item name="bottomNavBarRippleColor">@color/bottomNavBarRippleColor</item>
@ -73,6 +82,11 @@
<item name="colorSecondaryVariant">@color/secondaryVariantDark</item>
<item name="colorOnSecondary">@color/onSecondaryDark</item>
<!-- General -->
<item name="textOnTextBgColor">@color/textOnTextBgColorDark</item>
<item name="generalIconsColor">@color/generalIconsColorDark</item>
<item name="generalTextColor">@color/generalTextColorDark</item>
<!-- For Backgrounds -->
<item name="textBackgroundGradientStartColor" >@color/textBackgroundGradientStartColorDark</item>
@ -104,6 +118,10 @@
<item name="infoFragmentGradientCenterColor" >@color/infoFragmentGradientCenterColorDark</item>
<item name="infoFragmentGradientEndColor" >@color/infoFragmentGradientEndColorDark</item>
<item name="contrastingGradientStartColor">@color/contrastingGradientStartColorDark</item>
<item name="contrastingGradientCenterColor">@color/contrastingGradientCenterColorDark</item>
<item name="contrastingGradientEndColor">@color/contrastingGradientEndColorDark</item>
<!-- BottomNavCheckedItemColor -->
<item name="bottomNavBarCheckedItemColor">@color/bottomNavBarCheckedItemColorDark</item>
<item name="bottomNavBarRippleColor">@color/bottomNavBarRippleColorDark</item>