Skip to content

Commit

Permalink
Adding UI for the Profile Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Debanshu777 committed May 14, 2022
1 parent d79969f commit 962f605
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation "androidx.compose.runtime:runtime-livedata:$compose_version"

// Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ object AppModule {
level = LogLevel.ALL
}
install(HttpTimeout) {
socketTimeoutMillis = 30_000
requestTimeoutMillis = 30_000
connectTimeoutMillis = 30_000
socketTimeoutMillis = 30_0000
requestTimeoutMillis = 30_0000
connectTimeoutMillis = 30_0000
}
defaultRequest {
contentType(ContentType.Application.Json)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,21 @@ import com.debanshu777.compose_github.ui.feature_trending.state.RepositoryTrendi
import com.debanshu777.compose_github.utils.Resource
import composedb.githubDB.DeveloperFollow
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GitHubViewModel @Inject constructor(private val mainRepository: MainRepository) : ViewModel() {
val userDataState = MutableStateFlow(ProfileState())
private val _userDataState = MutableLiveData(ProfileState())
val userDataState:LiveData<ProfileState> = _userDataState
val trendingRepositoryDataState = MutableStateFlow(RepositoryTrendingState())
val trendingDeveloperDataState = MutableStateFlow(DeveloperTrendingState())
val developerList= mainRepository.getAllDeveloper()
Expand All @@ -35,7 +42,6 @@ class GitHubViewModel @Inject constructor(private val mainRepository: MainReposi
init {
getTrendingRepository("monthly")
getTrendingDeveloper("monthly")
//followDeveloperDataState.value= DeveloperFollowState(isLoading = false, data = developerList)
}
private val _searchWidgetState: MutableState<SearchWidgetState> =
mutableStateOf(value = SearchWidgetState.CLOSED)
Expand All @@ -56,13 +62,13 @@ class GitHubViewModel @Inject constructor(private val mainRepository: MainReposi
fun getUserData(userName: String) = viewModelScope.launch {
when (val result = mainRepository.getUserData(userName)) {
is Resource.Loading -> {
userDataState.value = ProfileState(isLoading = true)
_userDataState.value = ProfileState(isLoading = true)
}
is Resource.Success -> {
userDataState.value = ProfileState(data = result.data)
_userDataState.value = ProfileState(data = result.data)
}
is Resource.Error -> {
searchState.value = SearchState(error = result.message)
_userDataState.value = ProfileState(error = result.message)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ fun Navigation(viewModel: GitHubViewModel, navController: NavHostController) {
ProfileScreen(viewModel)
}
composable(route = Screen.TrendingScreen.route) {
TrendingScreen(viewModel)
TrendingScreen(viewModel,navController)
}
composable(route = Screen.FollowScreen.route) {
FollowScreen()
FollowScreen(navController)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package com.debanshu777.compose_github.ui.base.components.tabHandler

import androidx.compose.foundation.layout.Column
import androidx.compose.runtime.Composable
import androidx.navigation.NavController
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.PagerState

@ExperimentalPagerApi
@Composable
fun TabHandler(pagerState: PagerState, pageCount: Int, tabNames: List<String>, dataList: List<List<Any>>) {
fun TabHandler(pagerState: PagerState, pageCount: Int, tabNames: List<String>, dataList: List<List<Any>>, navController: NavController) {
Column() {
Tabs(pagerState = pagerState, tabNames)
TabsContent(pagerState = pagerState, pageCount, dataList)
TabsContent(pagerState = pagerState, pageCount, dataList,navController)
}
}

@ExperimentalPagerApi
@Composable
fun TabsContent(pagerState: PagerState, pageCount: Int, dataList: List<List<Any>>) {
fun TabsContent(pagerState: PagerState, pageCount: Int, dataList: List<List<Any>>, navController: NavController) {
HorizontalPager(state = pagerState, count = pageCount) { page ->
when (page) {
0 -> TabLayout(dataList[0])
1 -> TabLayout(dataList[1])
0 -> TabLayout(dataList[0],navController)
1 -> TabLayout(dataList[1],navController)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.debanshu777.compose_github.network.model.TrendingDeveloperItem
import com.debanshu777.compose_github.network.model.TrendingRepositoryItem
import com.debanshu777.compose_github.ui.feature_follow.components.FollowDeveloperCard
Expand All @@ -18,7 +19,7 @@ import composedb.githubDB.DeveloperFollow
import composedb.githubDB.RepositoryFollow

@Composable
fun <T> TabLayout(data: List<T>) {
fun <T> TabLayout(data: List<T>, navController: NavController) {
Column(
modifier = Modifier.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
Expand All @@ -41,7 +42,7 @@ fun <T> TabLayout(data: List<T>) {
FollowRepositoryCard(item)
}
if (item is TrendingDeveloperItem) {
DeveloperCard(item)
DeveloperCard(item,navController)
}
Spacer(modifier = Modifier.height(5.dp))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.debanshu777.compose_github.network.dataSource.GitHubViewModel
import com.debanshu777.compose_github.ui.base.components.tabHandler.TabHandler
import com.google.accompanist.pager.ExperimentalPagerApi
Expand All @@ -24,13 +25,13 @@ import composedb.githubDB.DeveloperFollow

@OptIn(ExperimentalPagerApi::class)
@Composable
fun FollowScreen(viewModel:GitHubViewModel= hiltViewModel()) {
fun FollowScreen(navController: NavController,viewModel:GitHubViewModel= hiltViewModel()) {
// val trendingDeveloperDataState by viewModel.trendingDeveloperDataState.collectAsState()
val developerFollowList by viewModel.developerList.collectAsState(emptyList())
val repositoryFollowList by viewModel.repositoryList.collectAsState(emptyList())
val pagerState = rememberPagerState(0)
val pageCount = 2
val tabList = listOf("Repository", "Developer")
val dataList = listOf(repositoryFollowList,developerFollowList)
TabHandler(pagerState, pageCount, tabList, dataList)
TabHandler(pagerState, pageCount, tabList, dataList,navController)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,204 @@
package com.debanshu777.compose_github.ui.feature_profile

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import coil.compose.AsyncImage
import com.debanshu777.compose_github.network.dataSource.GitHubViewModel
import com.debanshu777.compose_github.ui.feature_profile.state.ProfileState

@OptIn(ExperimentalUnitApi::class)
@Composable
fun ProfileScreen(viewModel: GitHubViewModel) {
val profileData by viewModel.userDataState.collectAsState()
Text(text = profileData.data.toString())
fun ProfileScreen(viewModel: GitHubViewModel= hiltViewModel()) {
val profileData by viewModel.userDataState.observeAsState()
Column(
modifier = Modifier
.fillMaxSize()
.padding(5.dp),
horizontalAlignment = Alignment.Start
) {
Row(
modifier= Modifier
.fillMaxWidth()
.padding(top = 15.dp),
verticalAlignment=Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
AsyncImage(
modifier = Modifier
.height(120.dp)
.width(120.dp)
.clip(CircleShape),
model = profileData?.data?.avatarUrl,
contentScale = ContentScale.Fit,
contentDescription = "User Avatar"
)
Row(
modifier=Modifier.fillMaxWidth(),
verticalAlignment=Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Column {
Text(
text = "Followers",
fontSize = TextUnit(value = 18F, type = TextUnitType.Sp),
)
Text(
text = "${profileData?.data?.followers ?: "0"}",
fontWeight = FontWeight.Bold,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
Spacer(modifier = Modifier.width(14.dp))
Column {
Text(
text = "Following",
fontSize = TextUnit(value = 18F, type = TextUnitType.Sp),
)
Text(
text = "${profileData?.data?.following ?: "0"}",
fontWeight = FontWeight.Bold,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
}
}
Text(
text = profileData?.data?.name ?: "NA",
maxLines = 1,
overflow = TextOverflow.Ellipsis,
fontWeight = FontWeight.Bold,
fontSize = TextUnit(value = 24F, type = TextUnitType.Sp),
)
Text(
text = "@${profileData?.data?.login ?: "NA"}",
maxLines = 1,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 18F, type = TextUnitType.Sp),
)
Spacer(modifier = Modifier.height(15.dp))
profileData?.data?.bio.let {
Text(
text = profileData?.data?.bio ?: "",
maxLines = 3,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
Spacer(modifier = Modifier.height(10.dp))
profileData?.data?.company.let {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Company",
fontSize = TextUnit(value = 16F, type = TextUnitType.Sp),
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = profileData?.data?.company ?: "NA",
maxLines = 1,
fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
}
profileData?.data?.location.let {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Location",
fontSize = TextUnit(value = 16F, type = TextUnitType.Sp),
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = profileData?.data?.location ?: "NA",
maxLines = 1,
fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
}
profileData?.data?.email.let {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Email",
fontSize = TextUnit(value = 16F, type = TextUnitType.Sp),
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = profileData?.data?.email ?: "NA",
maxLines = 1,
fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
}
profileData?.data?.blog.let {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Blog",
fontSize = TextUnit(value = 16F, type = TextUnitType.Sp),
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = profileData?.data?.blog ?: "NA",
maxLines = 1,
fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
}
profileData?.data?.twitterUsername.let {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Twitter",
fontSize = TextUnit(value = 16F, type = TextUnitType.Sp),
)
Spacer(modifier = Modifier.width(7.dp))
Text(
text = "@${profileData?.data?.twitterUsername ?: "NA"}",
maxLines = 1,
fontWeight = FontWeight.Bold,
overflow = TextOverflow.Ellipsis,
fontSize = TextUnit(value = 14F, type = TextUnitType.Sp),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ data class ProfileState(
val isLoading: Boolean = false,
val data: GitHubUserResponse? = null,
val error: String? = ""
)
){
override fun equals(other: Any?): Boolean {
return false
}

override fun hashCode(): Int {
return data?.hashCode() ?: 0
}
}
Loading

0 comments on commit 962f605

Please sign in to comment.