Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change button -> dropDownMenu #107

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions android/.idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ class EmojiViewModel @Inject constructor(
}
}

fun toggleSortingMode() {
sortByDate = sortByDate xor 1
fetchEmojiList()
}

fun fetchMyCreatedEmojiList() {
viewModelScope.launch {
emojiUseCase.fetchMyCreatedEmojiList()
Expand Down
45 changes: 36 additions & 9 deletions android/app/src/main/java/com/goliath/emojihub/views/EmojiPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material.DropdownMenu
import androidx.compose.material.DropdownMenuItem
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
Expand All @@ -34,13 +36,15 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.compose.collectAsLazyPagingItems
import com.goliath.emojihub.LocalNavController
import com.goliath.emojihub.NavigationDestination
import com.goliath.emojihub.extensions.toEmoji
import com.goliath.emojihub.navigateAsOrigin
import com.goliath.emojihub.ui.theme.Color.Black
import com.goliath.emojihub.ui.theme.Color.LightGray
Expand All @@ -64,6 +68,7 @@ fun EmojiPage() {
val emojiList = emojiViewModel.emojiList.collectAsLazyPagingItems()

var showNonUserDialog by remember { mutableStateOf(false) }
var dropDownMenuExpanded by remember { mutableStateOf(false) }

val permissionLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestPermission()
Expand Down Expand Up @@ -118,16 +123,38 @@ fun EmojiPage() {
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text("Trending 🔥", fontSize = 20.sp, fontWeight = FontWeight.Bold)
Text(text = if (emojiViewModel.sortByDate == 0) "Trending 🔥" else "Recently added " + "U+D83D U+DD52".toEmoji(), fontSize = 20.sp, fontWeight = FontWeight.Bold)

Button(
onClick = { emojiViewModel.toggleSortingMode() },
colors = ButtonDefaults.buttonColors(
backgroundColor = if (emojiViewModel.sortByDate == 0) Black else LightGray,
contentColor = White
)
) {
Text(text = if (emojiViewModel.sortByDate == 1) "Sort by Date" else "Sort by Save Count", fontSize = 12.sp)
Column {
Button(
onClick = { dropDownMenuExpanded = true },
colors = ButtonDefaults.buttonColors(
backgroundColor = Black,
contentColor = White
)
) {
Text(text = "Sort by", fontSize = 12.sp)
}

DropdownMenu(
expanded = dropDownMenuExpanded,
onDismissRequest = { dropDownMenuExpanded = false }
) {
DropdownMenuItem(onClick = {
emojiViewModel.sortByDate = 1
emojiViewModel.fetchEmojiList()
dropDownMenuExpanded = false
}) {
Text(text = "created date")
}
DropdownMenuItem(onClick = {
emojiViewModel.sortByDate = 0
emojiViewModel.fetchEmojiList()
dropDownMenuExpanded = false
}) {
Text(text = "save count")
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,21 @@ class EmojiViewModelTest {
}

@Test
fun toggleSortingMode_success_updateTrendingEmojiList() = runTest {
fun toggleSortingMode_success_updateLatestEmojiList() = runTest {
// given
// for simplicity of testing, we return the same list for both cases
val sampleFetchedEmojiList = createDeterministicTrendingEmojiList(10)
coEvery {
emojiUseCase.fetchEmojiList(1)
} returns sampleFetchedEmojiList
// when
emojiViewModel.toggleSortingMode()
emojiViewModel.sortByDate = 1
emojiViewModel.fetchEmojiList()
advanceUntilIdle()
// then
assertEquals(1, emojiViewModel.sortByDate)
coVerify(exactly = 1) { emojiUseCase.fetchEmojiList(1) }
coVerify(exactly = 1) { emojiUseCase.updateEmojiList(any()) }
assertEquals(1, emojiViewModel.sortByDate)
}

@Test
Expand Down