Skip to content

Commit

Permalink
search feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Amira authored and Amira committed Dec 5, 2024
1 parent e4f1ec6 commit e3fd4de
Show file tree
Hide file tree
Showing 15 changed files with 1,246 additions and 756 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ import java.net.URLDecoder
import java.net.URLEncoder
import javax.inject.Inject

class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
class SoundscapeIntents @Inject constructor(private val navigator: Navigator) {

private lateinit var geocoder: Geocoder
private fun useGeocoderToGetAddress(location : String,
context : Context) {
private fun useGeocoderToGetAddress(
location: String,
context: Context
) {
geocoder = Geocoder(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val geocodeListener = Geocoder.GeocodeListener { addresses ->
Log.d(TAG, "getFromLocationName results count " + addresses.size.toString())
val address = addresses.firstOrNull()
if (address != null) {
Log.d(TAG, "$address")
val ld = LocationDescription(address.getAddressLine(0), address.latitude, address.longitude)
val ld = LocationDescription(
name = address.getAddressLine(0),
latitude = address.latitude,
longitude = address.longitude,
)
navigator.navigate(generateLocationDetailsRoute(ld))
}
}
Expand All @@ -47,17 +53,18 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {

@Suppress("DEPRECATION")
val address = geocoder.getFromLocationName(location, 1)?.firstOrNull()
if(address != null) {
if (address != null) {
Log.d(TAG, "Address: $address")
val ld = LocationDescription(
address.getAddressLine(0),
address.latitude,
address.longitude
name = address.getAddressLine(0),
latitude = address.latitude,
longitude = address.longitude,
)
navigator.navigate(generateLocationDetailsRoute(ld))
}
}
}

private fun getRedirectUrl(url: String, context: Context) {
CoroutineScope(Dispatchers.IO).launch {
var urlTmp: URL? = null
Expand All @@ -73,8 +80,7 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
try {
Log.d(TAG, "Open connection")
connection = urlTmp!!.openConnection() as HttpURLConnection
}
catch (e: IOException) {
} catch (e: IOException) {
e.printStackTrace()
}
try {
Expand Down Expand Up @@ -114,7 +120,7 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
Navigation to the LocationDetails is done via the main activity navigator.
*/
fun parse(intent : Intent, mainActivity: MainActivity) {
fun parse(intent: Intent, mainActivity: MainActivity) {
when {
intent.action == Intent.ACTION_SEND -> {
if ("text/plain" == intent.type) {
Expand All @@ -130,6 +136,7 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
}
}
}

else -> {
val uriData: String =
URLDecoder.decode(intent.data.toString(), Charsets.UTF_8.name())
Expand All @@ -144,31 +151,29 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
val latitude = matchResult.groupValues[2]
val longitude = matchResult.groupValues[3]

if(matchResult.groupValues[1] == "soundscape") {
if (matchResult.groupValues[1] == "soundscape") {
// Switch to Street Preview mode
mainActivity.soundscapeServiceConnection.setStreetPreviewMode(
true,
latitude.toDouble(),
longitude.toDouble()
)
}
else {
} else {
try {
check(Geocoder.isPresent())
useGeocoderToGetAddress("$latitude,$longitude", mainActivity)
} catch (e: Exception) {
// No Geocoder available, so just report the uriData
val ld =
LocationDescription(
URLEncoder.encode(uriData, "utf-8"),
latitude.toDouble(),
longitude.toDouble()
name = URLEncoder.encode(uriData, "utf-8"),
latitude = latitude.toDouble(),
longitude = longitude.toDouble()
)
mainActivity.navigator.navigate(generateLocationDetailsRoute(ld))
}
}
}
else {
} else {
if (Intent.ACTION_VIEW == intent.action || Intent.ACTION_MAIN == intent.action) {
val data = intent.data
if (data != null) {
Expand All @@ -179,7 +184,7 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
} else if ("content" == data.scheme) {
Log.d(TAG, "Import data from content ${data.path}")
val uri = intent.data
if(uri != null) {
if (uri != null) {
try {
val input =
mainActivity.contentResolver.openInputStream(uri)
Expand All @@ -204,7 +209,7 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
}
}
}
} catch(e: Exception) {
} catch (e: Exception) {
Log.e(TAG, "Failed to import GPX from intent: $e")
}
}
Expand All @@ -215,6 +220,7 @@ class SoundscapeIntents @Inject constructor(private val navigator : Navigator) {
}
}
}

companion object {
private const val TAG = "SoundscapeIntents"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.ArrowBack
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SearchBar
import androidx.compose.material3.SearchBarDefaults
import androidx.compose.material3.Text
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.CollectionInfo
import androidx.compose.ui.semantics.CollectionItemInfo
import androidx.compose.ui.semantics.collectionInfo
import androidx.compose.ui.semantics.collectionItemInfo
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import org.scottishtecharmy.soundscape.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -32,76 +39,94 @@ fun MainSearchBar(
itemList: List<SearchItem>,
onSearchTextChange: (String) -> Unit,
onToggleSearch: () -> Unit,
onItemClick: (SearchItem) -> Unit
onItemClick: (SearchItem) -> Unit,
) {
val containerColor = Color.White
SearchBar(
query = searchText,
onQueryChange = onSearchTextChange,
onSearch = onSearchTextChange,
active = isSearching,
modifier =
Modifier
.fillMaxWidth()
.then(
if (!isSearching) {
Modifier.padding(horizontal = 16.dp)
} else {
Modifier.padding(horizontal = 0.dp)
},
),
shape = RoundedCornerShape(8.dp),
onActiveChange = { onToggleSearch() },
colors = SearchBarDefaults.colors(
containerColor = containerColor,
inputFieldColors = TextFieldDefaults.colors(
focusedTextColor = Color.Black,
focusedContainerColor = containerColor,
unfocusedContainerColor = containerColor,
disabledContainerColor = containerColor,
)
),
leadingIcon = {
if (!isSearching) {
Icon(
Icons.Rounded.Search,
null,
tint = Color.Gray
)
} else {
IconButton(
onClick = { onToggleSearch() },
) {
Icon(
Icons.AutoMirrored.Rounded.ArrowBack,
contentDescription = "Cancel search",
tint = Color.Gray
)
}
}
},
placeholder = {
Text(
text = "Search destination",
color = Color.Gray
colors = SearchBarDefaults.colors(containerColor = Color.White),
inputField = {
SearchBarDefaults.InputField(
query = searchText,
onQueryChange = onSearchTextChange,
onSearch = onSearchTextChange,
expanded = isSearching,
onExpandedChange = { onToggleSearch() },
placeholder = { Text(stringResource(id = R.string.search_hint_input)) },
leadingIcon = {
when {
!isSearching -> {
Icon(
imageVector = Icons.Rounded.Search,
contentDescription = null,
tint = Color.Gray,
)
}

else -> {
IconButton(
onClick = { onToggleSearch() },
) {
Icon(
Icons.AutoMirrored.Rounded.ArrowBack,
contentDescription =
stringResource(R.string.cancel_search_contentDescription),
tint = Color.Gray,
)
}
}
}
},
colors = SearchBarDefaults.inputFieldColors(focusedTextColor = Color.Black),
)
},
modifier = Modifier
.fillMaxWidth()
.then(
if (!isSearching) {
Modifier.padding(horizontal = 16.dp)
} else {
Modifier.padding(horizontal = 0.dp)
}
)
expanded = isSearching,
onExpandedChange = { onToggleSearch() },
) {
Column(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background)
modifier =
Modifier
.semantics {
this.collectionInfo =
CollectionInfo(
rowCount = itemList.size, // Total number of items
columnCount = 1, // Single-column list
)
}.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
) {
LazyColumn(modifier = Modifier.padding(top = 16.dp)) {
items(itemList) { item ->
SearchItemButton(
item = item,
onClick = {
onItemClick(item)
},
modifier = Modifier
)
itemsIndexed(itemList) { index, item ->
Column {
SearchItemButton(
item = item,
onClick = {
onItemClick(item)
},
modifier =
Modifier.semantics {
this.collectionItemInfo =
CollectionItemInfo(
rowSpan = 1,
columnSpan = 1,
rowIndex = index,
columnIndex = 0,
)
},
)
HorizontalDivider(color = Color.White)
}
}
}
}
}
}
}
Loading

0 comments on commit e3fd4de

Please sign in to comment.