Skip to content

Commit

Permalink
Merge branch 'dev' into strings-squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
alansley committed Aug 22, 2024
2 parents efc2ee2 + 8e10e1a commit c3c35de
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapLatest
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import org.session.libsignal.utilities.SettableFuture
import org.thoughtcrime.securesms.search.SearchRepository
import org.thoughtcrime.securesms.search.model.SearchResult
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine

@OptIn(ExperimentalCoroutinesApi::class)
@HiltViewModel
Expand All @@ -47,11 +45,8 @@ class GlobalSearchViewModel @Inject constructor(
// User input delay in case we get a new query within a few hundred ms this
// coroutine will be cancelled and the expensive query will not be run.
delay(300)
val settableFuture = SettableFuture<SearchResult>()
searchRepository.query(query.toString(), settableFuture::set)
try {
// search repository doesn't play nicely with suspend functions (yet)
settableFuture.get(10_000, TimeUnit.MILLISECONDS).toGlobalSearchResult()
searchRepository.suspendQuery(query.toString()).toGlobalSearchResult()
} catch (e: Exception) {
GlobalSearchResult(query.toString())
}
Expand All @@ -69,6 +64,12 @@ class GlobalSearchViewModel @Inject constructor(
}
}

private suspend fun SearchRepository.suspendQuery(query: String): SearchResult {
return suspendCoroutine { cont ->
query(query, cont::resume)
}
}

/**
* Re-emit whenever refreshes emits.
* */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
Expand All @@ -29,7 +28,6 @@ import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -40,7 +38,6 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.dp
import network.loki.messenger.R
import org.thoughtcrime.securesms.ui.AlertDialog
import org.thoughtcrime.securesms.ui.DialogButtonModel
Expand Down Expand Up @@ -293,5 +290,4 @@ private val MediaOverviewTab.titleResId: Int
get() = when (this) {
MediaOverviewTab.Media -> R.string.media
MediaOverviewTab.Documents -> R.string.document
}

}
24 changes: 21 additions & 3 deletions app/src/main/java/org/thoughtcrime/securesms/media/MediaPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,30 @@ private fun ThumbnailRow(
) {
it.diskCacheStrategy(DiskCacheStrategy.NONE)
}
} else if (item.hasPlaceholder) {
} else {
// The resource given by the placeholder needs tinting according to our theme.
// But the missing thumbnail picture does not.
var (placeholder, shouldTint) = if (item.hasPlaceholder) {
item.placeholder(LocalContext.current) to true
} else {
R.drawable.ic_missing_thumbnail_picture to false
}

if (placeholder == 0) {
placeholder = R.drawable.ic_missing_thumbnail_picture
shouldTint = false
}

Image(
painter = painterResource(item.placeholder(LocalContext.current)),
painter = painterResource(placeholder),
contentDescription = null,
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Inside
contentScale = ContentScale.Inside,
colorFilter = if (shouldTint) {
ColorFilter.tint(LocalColors.current.textSecondary)
} else {
null
}
)
}

Expand Down
59 changes: 39 additions & 20 deletions app/src/main/java/org/thoughtcrime/securesms/util/IP2Country.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,64 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import org.session.libsignal.utilities.Log
import com.opencsv.CSVReader
import org.session.libsession.snode.OnionRequestAPI
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.ThreadUtils
import java.io.File
import java.io.FileOutputStream
import java.io.FileReader
import java.util.SortedMap
import java.util.TreeMap

class IP2Country private constructor(private val context: Context) {
private val pathsBuiltEventReceiver: BroadcastReceiver
val countryNamesCache = mutableMapOf<String, String>()

private fun Ipv4Int(ip:String) = ip.takeWhile { it != '/' }.split('.').foldIndexed(0L) { i, acc, s ->
val asInt = s.toLong()
acc + (asInt shl (8 * (3-i)))
private fun Ipv4Int(ip: String): Int {
var result = 0L
var currentValue = 0L
var octetIndex = 0

for (char in ip) {
if (char == '.' || char == '/') {
result = result or (currentValue shl (8 * (3 - octetIndex)))
currentValue = 0
octetIndex++
if (char == '/') break
} else {
currentValue = currentValue * 10 + (char - '0')
}
}

// Handle the last octet
result = result or (currentValue shl (8 * (3 - octetIndex)))

return result.toInt()
}

private val ipv4ToCountry by lazy {
private val ipv4ToCountry: TreeMap<Int, Int?> by lazy {
val file = loadFile("geolite2_country_blocks_ipv4.csv")
val csv = CSVReader(FileReader(file.absoluteFile)).apply {
skip(1)
}
CSVReader(FileReader(file.absoluteFile)).use { csv ->
csv.skip(1)

csv.readAll()
.associate { cols ->
Ipv4Int(cols[0]) to cols[1].toIntOrNull()
}
csv.asSequence().associateTo(TreeMap()) { cols ->
Ipv4Int(cols[0]).toInt() to cols[1].toIntOrNull()
}
}
}

private val countryToNames by lazy {
private val countryToNames: Map<Int, String> by lazy {
val file = loadFile("geolite2_country_locations_english.csv")
val csv = CSVReader(FileReader(file.absoluteFile)).apply {
skip(1)
}
csv.readAll()
CSVReader(FileReader(file.absoluteFile)).use { csv ->
csv.skip(1)

csv.asSequence()
.filter { cols -> !cols[0].isNullOrEmpty() && !cols[1].isNullOrEmpty() }
.associate { cols ->
cols[0].toInt() to cols[5]
}
}
}

// region Initialization
Expand Down Expand Up @@ -95,9 +114,8 @@ class IP2Country private constructor(private val context: Context) {
// return early if cached
countryNamesCache[ip]?.let { return it }

val comps = ipv4ToCountry.asSequence()

val bestMatchCountry = comps.lastOrNull { it.key <= Ipv4Int(ip) }?.let { (_, code) ->
val ipInt = Ipv4Int(ip)
val bestMatchCountry = ipv4ToCountry.floorEntry(ipInt)?.let { (_, code) ->
if (code != null) {
countryToNames[code]
} else {
Expand Down Expand Up @@ -127,3 +145,4 @@ class IP2Country private constructor(private val context: Context) {
}
// endregion
}

15 changes: 0 additions & 15 deletions app/src/main/java/org/thoughtcrime/securesms/util/ListUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import androidx.annotation.NonNull;

import com.annimon.stream.Stream;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public final class ListUtil {
Expand All @@ -21,16 +18,4 @@ public static <E> List<List<E>> chunk(@NonNull List<E> list, int chunkSize) {

return chunks;
}

@SafeVarargs
public static <T> List<T> concat(Collection<T>... items) {
//noinspection Convert2MethodRef
final List<T> concat = new ArrayList<>(Stream.of(items).map(Collection::size).reduce(0, (lhs, rhs) -> lhs+rhs));

for (Collection<T> list : items) {
concat.addAll(list);
}

return concat;
}
}
9 changes: 0 additions & 9 deletions app/src/main/res/drawable/ic_outline_message_requests_24.xml

This file was deleted.

2 changes: 1 addition & 1 deletion app/src/main/res/layout/view_message_request_banner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
android:layout_marginVertical="@dimen/medium_spacing"
android:layout_marginStart="@dimen/medium_spacing"
android:padding="10dp"
android:src="@drawable/ic_outline_message_requests_24"
android:src="@drawable/ic_message_requests"
android:tint="?unreadIndicatorTextColor"
app:circleColor="?unreadIndicatorBackgroundColor"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@
<item name="attachment_document_icon_small">@drawable/ic_document_small_dark</item>
<item name="attachment_document_icon_large">@drawable/ic_document_large_dark</item>
<item name="colorError">?danger</item>
</style>

<item name="conversation_icon_attach_audio">@drawable/ic_audio_dark</item>
<item name="conversation_icon_attach_video">@drawable/ic_video_dark</item>
</style>

<!-- This should be the default theme for the application. -->
<style name="Theme.Session.DayNight" parent="Base.Theme.Session">
Expand Down Expand Up @@ -206,9 +209,6 @@
<item name="linkpreview_secondary_text_color">?android:textColorPrimary</item>
<item name="linkpreview_divider_color">@color/transparent</item>

<item name="conversation_icon_attach_audio">@drawable/ic_audio_dark</item>
<item name="conversation_icon_attach_video">@drawable/ic_video_dark</item>

<item name="sticker_management_icon">@drawable/sticker_button_dark</item>
<item name="sticker_management_divider_color">@color/core_grey_75</item>
<item name="sticker_management_empty_background_color">@color/core_grey_85</item>
Expand Down

0 comments on commit c3c35de

Please sign in to comment.