Skip to content

Commit

Permalink
Merge pull request #23 from azrael8576/feat/video
Browse files Browse the repository at this point in the history
Implement functionality of the VideoSearchScreen
  • Loading branch information
azrael8576 authored Dec 6, 2023
2 parents 0dc584b + ed27ebd commit e798abc
Show file tree
Hide file tree
Showing 20 changed files with 710 additions and 57 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
## Screenshots


<img src="https://github.com/azrael8576/picquest/blob/main/docs/demo/demo.gif" alt="Demo">
<img src="https://github.com/azrael8576/picquest/blob/main/docs/demo/demo.gif" alt="Demo">

<img src="https://github.com/azrael8576/picquest/blob/main/docs/demo/demo-split-screen.gif" alt="Demo-Split-Screen">
<img src="https://github.com/azrael8576/picquest/blob/main/docs/demo/demo-split-screen.gif" alt="Demo-Split-Screen">

<img src="https://github.com/azrael8576/picquest/blob/main/docs/demo/demo-video.gif" alt="Demo-Video">


## Tech stack
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/com/wei/picquest/navigation/PqNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.wei.picquest.feature.home.home.navigation.homeRoute
import com.wei.picquest.feature.photo.photolibrary.navigation.photoLibraryGraph
import com.wei.picquest.feature.photo.photosearch.navigation.photoSearchGraph
import com.wei.picquest.feature.video.videolibrary.navigation.videoLibraryGraph
import com.wei.picquest.feature.video.videosearch.navigation.videoSearchGraph
import com.wei.picquest.ui.PqAppState

/**
Expand Down Expand Up @@ -46,8 +47,11 @@ fun PqNavHost(
photoLibraryGraph(navController = navController)
},
)
videoLibraryGraph(
videoSearchGraph(
navController = navController,
nestedGraphs = {
videoLibraryGraph(navController = navController)
},
)
contactMeGraph(
navController = navController,
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/wei/picquest/ui/PqAppState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import com.wei.picquest.feature.home.home.navigation.homeRoute
import com.wei.picquest.feature.home.home.navigation.navigateToHome
import com.wei.picquest.feature.photo.photosearch.navigation.navigateToPhotoSearch
import com.wei.picquest.feature.photo.photosearch.navigation.photoSearchRoute
import com.wei.picquest.feature.video.videolibrary.navigation.navigateToVideoLibrary
import com.wei.picquest.feature.video.videolibrary.navigation.videoLibraryRoute
import com.wei.picquest.feature.video.videosearch.navigation.navigateToVideoSearch
import com.wei.picquest.feature.video.videosearch.navigation.videoSearchRoute
import com.wei.picquest.navigation.TopLevelDestination
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.SharingStarted
Expand Down Expand Up @@ -156,7 +156,7 @@ class PqAppState(
@Composable get() = when (currentDestination?.route) {
homeRoute -> TopLevelDestination.HOME
photoSearchRoute -> TopLevelDestination.PHOTO
videoLibraryRoute -> TopLevelDestination.VIDEO
videoSearchRoute -> TopLevelDestination.VIDEO
contactMeRoute -> TopLevelDestination.CONTACT_ME
else -> null
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class PqAppState(
topLevelNavOptions,
)

TopLevelDestination.VIDEO -> navController.navigateToVideoLibrary(
TopLevelDestination.VIDEO -> navController.navigateToVideoSearch(
topLevelNavOptions,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ class DefaultUserDataRepository @Inject constructor(
paPreferencesDataSource.setTokenString(tokenString)
}

override suspend fun addRecentSearchQuery(newQuery: String) {
paPreferencesDataSource.addRecentSearchQuery(newQuery)
override suspend fun addRecentSearchPhotoQuery(newQuery: String) {
paPreferencesDataSource.addRecentSearchPhotoQuery(newQuery)
}

override suspend fun clearRecentSearchQueries() {
paPreferencesDataSource.clearRecentSearchQueries()
override suspend fun addRecentSearchVideoQuery(newQuery: String) {
paPreferencesDataSource.addRecentSearchVideoQuery(newQuery)
}

override suspend fun clearRecentSearchPhotoQueries() {
paPreferencesDataSource.clearRecentSearchPhotoQueries()
}

override suspend fun clearRecentSearchVideoQueries() {
paPreferencesDataSource.clearRecentSearchVideoQueries()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ interface UserDataRepository {
suspend fun setTokenString(tokenString: String)

/**
* Adds a new search query to the user's recent search queries.
* Adds a new search query to the user's recent search photo queries.
*/
suspend fun addRecentSearchQuery(newQuery: String)
suspend fun addRecentSearchPhotoQuery(newQuery: String)

/**
* Clears all the user's recent search queries.
* Adds a new search query to the user's recent search video queries.
*/
suspend fun clearRecentSearchQueries()
suspend fun addRecentSearchVideoQuery(newQuery: String)

/**
* Clears all the user's recent search photo queries.
*/
suspend fun clearRecentSearchPhotoQueries()

/**
* Clears all the user's recent search video queries.
*/
suspend fun clearRecentSearchVideoQueries()
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class PqPreferencesDataSource @Inject constructor(
LanguageConfigProto.LANGUAGE_CONFIG_ENGLISH -> LanguageConfig.ENGLISH
LanguageConfigProto.LANGUAGE_CONFIG_CHINESE -> LanguageConfig.CHINESE
},
recentSearchQueries = it.recentSearchQueriesList,
recentSearchPhotoQueries = it.recentSearchPhotoQueriesList,
recentSearchVideoQueries = it.recentSearchVideoQueriesList,
)
}

Expand All @@ -45,38 +46,69 @@ class PqPreferencesDataSource @Inject constructor(
}
}
} catch (ioException: IOException) {
Log.e("AtPreferences", "Failed to update user preferences", ioException)
Log.e("PqPreferences", "Failed to update user preferences", ioException)
}
}

suspend fun addRecentSearchQuery(newQuery: String) {
suspend fun addRecentSearchPhotoQuery(newQuery: String) {
try {
userPreferences.updateData { preferences ->
val updatedQueries = preferences.recentSearchQueriesList.toMutableList().apply {
val updatedQueries = preferences.recentSearchPhotoQueriesList.toMutableList().apply {
// 移除既有相同的查詢(如果存在),然後添加到列表末尾
remove(newQuery)
add(newQuery)
if (size > 10) removeAt(0)
}
preferences.toBuilder()
.clearRecentSearchQueries()
.addAllRecentSearchQueries(updatedQueries)
.clearRecentSearchPhotoQueries()
.addAllRecentSearchPhotoQueries(updatedQueries)
.build()
}
} catch (ioException: IOException) {
Log.e("PqPreferencesDataSource", "Failed to update recent search queries", ioException)
Log.e("PqPreferencesDataSource", "Failed to update recent search photo queries", ioException)
}
}

suspend fun clearRecentSearchQueries() {
suspend fun clearRecentSearchPhotoQueries() {
try {
userPreferences.updateData { preferences ->
preferences.toBuilder()
.clearRecentSearchQueries()
.clearRecentSearchPhotoQueries()
.build()
}
} catch (ioException: IOException) {
Log.e("PqPreferencesDataSource", "Failed to clear recent search queries", ioException)
Log.e("PqPreferencesDataSource", "Failed to clear recent search photo queries", ioException)
}
}

suspend fun addRecentSearchVideoQuery(newQuery: String) {
try {
userPreferences.updateData { preferences ->
val updatedQueries = preferences.recentSearchVideoQueriesList.toMutableList().apply {
// 移除既有相同的查詢(如果存在),然後添加到列表末尾
remove(newQuery)
add(newQuery)
if (size > 10) removeAt(0)
}
preferences.toBuilder()
.clearRecentSearchVideoQueries()
.addAllRecentSearchVideoQueries(updatedQueries)
.build()
}
} catch (ioException: IOException) {
Log.e("PqPreferencesDataSource", "Failed to update recent search video queries", ioException)
}
}

suspend fun clearRecentSearchVideoQueries() {
try {
userPreferences.updateData { preferences ->
preferences.toBuilder()
.clearRecentSearchVideoQueries()
.build()
}
} catch (ioException: IOException) {
Log.e("PqPreferencesDataSource", "Failed to clear recent search video queries", ioException)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ message UserPreferences {
ThemeBrandProto theme_brand = 5; // Specifies the brand theme the user has chosen
DarkThemeConfigProto dark_theme_config = 6; // User's preference for dark theme mode
LanguageConfigProto language_config = 7; // Language preferred by the user for the app interface
repeated string recent_search_queries = 8; // List of user's recent search queries
repeated string recent_search_photo_queries = 8; // List of user's recent search photo queries
repeated string recent_search_video_queries = 9; // List of user's recent search video queries

// NEXT AVAILABLE ID: 9
// NEXT AVAILABLE ID: 10
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ data class UserData(
val themeBrand: ThemeBrand,
val darkThemeConfig: DarkThemeConfig,
val languageConfig: LanguageConfig,
val recentSearchQueries: List<String>,
val recentSearchPhotoQueries: List<String>,
val recentSearchVideoQueries: List<String>,
)
Binary file added docs/demo/demo-video.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand Down Expand Up @@ -297,14 +296,15 @@ private fun SearchTextField(
maxLines = 1,
singleLine = true,
)
LaunchedEffect(Unit) {
focusRequester.requestFocus()
}
// TODO move to ViewModel
// LaunchedEffect(Unit) {
// focusRequester.requestFocus()
// }
}

@ThemePreviews
@Composable
fun SearchPhotoScreenPreview() {
fun PhotoSearchScreenPreview() {
PqTheme {
PhotoSearchScreen(
uiStates = PhotoSearchViewState(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PhotoSearchViewModel @Inject constructor(
userDataRepository.userData.collect { userData ->
updateState {
copy(
recentSearchQueries = userData.recentSearchQueries.asReversed(),
recentSearchQueries = userData.recentSearchPhotoQueries.asReversed(),
)
}
}
Expand All @@ -34,15 +34,15 @@ class PhotoSearchViewModel @Inject constructor(
private fun searchTriggered(query: String) {
if (query.isNotBlank()) {
viewModelScope.launch {
userDataRepository.addRecentSearchQuery(query)
userDataRepository.addRecentSearchPhotoQuery(query)
}
}
updateState { copy(searchQuery = "") }
}

private fun clearRecentSearchQueries() {
viewModelScope.launch {
userDataRepository.clearRecentSearchQueries()
userDataRepository.clearRecentSearchPhotoQueries()
}
}

Expand Down
Loading

0 comments on commit e798abc

Please sign in to comment.