Skip to content

Commit

Permalink
Adding DurationType to trigger API type
Browse files Browse the repository at this point in the history
  • Loading branch information
Debanshu Datta committed Jun 22, 2022
1 parent 84176b0 commit 432c099
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.debanshu777.compose_github.ui.feature_trending.state.RepositoryTrendi
import com.debanshu777.compose_github.utils.DurationType
import com.debanshu777.compose_github.utils.Resource
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down Expand Up @@ -64,6 +65,8 @@ class GitHubViewModel @Inject constructor(private val mainRepository: MainReposi

fun updateDurationType(type: DurationType) {
_durationType.value = type
getTrendingRepository(_durationType.value.type)
getTrendingDeveloper(_durationType.value.type)
}

fun updateDurationTypeFilterVisibility() {
Expand All @@ -75,13 +78,13 @@ class GitHubViewModel @Inject constructor(private val mainRepository: MainReposi
}

fun getUserData(userName: String) = viewModelScope.launch {
async { getUserStats(userName) }
async { getPinnedProject(userName) }
when (val result = mainRepository.getUserData(userName)) {
is Resource.Loading -> {
_userDataState.value = ProfileState(isLoading = true)
}
is Resource.Success -> {
getUserStats(userName)
getPinnedProject(userName)
_userDataState.value = ProfileState(data = result.data)
}
is Resource.Error -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ import androidx.compose.ui.unit.toSize
import com.debanshu777.compose_github.utils.DurationType

@Composable
fun DropDownMenu(visible: Boolean) {
fun DropDownMenu(
durType: DurationType?,
visible: Boolean,
onTypeChangeCallback: (DurationType) -> Unit
) {
var expanded by remember { mutableStateOf(false) }
val suggestions = listOf(DurationType.WEEKLY, DurationType.MONTHLY, DurationType.YEARLY)
var selectedText by remember { mutableStateOf("") }
var selectedText by remember { mutableStateOf(durType?.type ?: "") }

var textfieldSize by remember { mutableStateOf(Size.Zero) }

Expand All @@ -57,6 +61,7 @@ fun DropDownMenu(visible: Boolean) {
OutlinedTextField(
value = selectedText,
onValueChange = { selectedText = it },
readOnly = true,
modifier = Modifier
.fillMaxWidth()
.onGloballyPositioned { coordinates ->
Expand All @@ -79,6 +84,7 @@ fun DropDownMenu(visible: Boolean) {
DropdownMenuItem(onClick = {
selectedText = label.type
expanded = false
onTypeChangeCallback(label)
},
text = { Text(text = label.type) }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fun FollowScreen(navController: NavController, viewModel: GitHubViewModel = hilt
val repositoryFollowList by viewModel.repositoryList.collectAsState(emptyList())
val pagerState = rememberPagerState(0)
val pageCount = 2
val tabList = listOf("Repository", "Developer")
val tabList = listOf("Repositories", "Developers")
val dataList = listOf(repositoryFollowList, developerFollowList)
Column(modifier = Modifier.padding(top = 56.dp)) {
TabHandler(pagerState, pageCount, tabList, dataList, navController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ import com.debanshu777.compose_github.ui.base.Screen
fun SearchScreen(viewModel: GitHubViewModel, navController: NavController) {
val searchData by viewModel.searchState.collectAsState()
LazyColumn(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.fillMaxWidth()
.padding(top = 56.dp),
contentPadding = PaddingValues(10.dp)
) {
items(searchData.data) { item ->
ElevatedCard(
modifier = Modifier.height(120.dp)
modifier = Modifier
.height(120.dp)
.padding(vertical = 5.dp)
) {
Row(
modifier = Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ fun TrendingScreen(viewModel: GitHubViewModel, navController: NavController) {
val trendingRepositoryDataState by viewModel.trendingRepositoryDataState.collectAsState()
val trendingDeveloperDataState by viewModel.trendingDeveloperDataState.collectAsState()
val durationTypeFilterVisibility by viewModel.durationTypeFilterVisibility.observeAsState()
val durationType by viewModel.durationType.observeAsState()
val pagerState = rememberPagerState(0)
val pageCount = 2
val tabList = listOf("Repository", "Developer")
val tabList = listOf("Repositories", "Developers")
val dataList = listOf(trendingRepositoryDataState.data, trendingDeveloperDataState.data)
Column(modifier = Modifier.padding(top = if (durationTypeFilterVisibility == true) 56.dp else 20.dp)) {
DropDownMenu(durationTypeFilterVisibility ?: false)
Column(modifier = Modifier.padding(top = if (durationTypeFilterVisibility == true) 56.dp else 15.dp)) {
DropDownMenu(
durationType,
durationTypeFilterVisibility ?: false
) { viewModel.updateDurationType(it) }
TabHandler(pagerState, pageCount, tabList, dataList, navController)
}
}
}

0 comments on commit 432c099

Please sign in to comment.