Skip to content

Commit

Permalink
fix PratyushSingh07#45:Migrate home screen to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
GautamCoder4019k committed Mar 24, 2024
1 parent bcef1d0 commit 886a97c
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 57 deletions.
18 changes: 9 additions & 9 deletions app/src/main/java/com/example/findissues/models/home/User.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.example.findissues.models.home

data class User(
val login: String,
val avatar_url: String,
val name: String,
val bio: String,
val company: String,
val location: String,
val twitter_username: String,
val followers: Int,
val following: Int
val login: String="",
val avatar_url: String="",
val name: String="",
val bio: String="",
val company: String="",
val location: String="",
val twitter_username: String="",
val followers: Int=0,
val following: Int=0
)
121 changes: 73 additions & 48 deletions app/src/main/java/com/example/findissues/ui/fragments/HomeFragment.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.example.findissues.ui.fragments

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.material3.Button
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.findissues.R
import com.example.findissues.databinding.FragmentHomeBinding
import com.example.findissues.models.home.User
import com.example.findissues.ui.adapters.PinnedRepoAdapter
import com.example.findissues.ui.followers.FollowersFragment
import com.example.findissues.ui.following.FollowingFragment
import com.example.findissues.ui.home.HomeScreen
import com.example.findissues.ui.issues.IssuesScreen
import com.example.findissues.utils.Browser
import com.example.findissues.utils.Constants.FOLLOWERS
import com.example.findissues.utils.Constants.FOLLOWING
Expand All @@ -31,8 +41,10 @@ import javax.inject.Inject
@AndroidEntryPoint
class HomeFragment : Fragment() {

private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
// private var _binding: FragmentHomeBinding? = null
// private val binding get() = _binding!!

private var user by mutableStateOf(User())

private lateinit var userViewModel: UserViewModel
private lateinit var pinnedRepoViewModel: PinnedRepoViewModel
Expand All @@ -43,36 +55,37 @@ class HomeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
_binding = FragmentHomeBinding.inflate(inflater, container, false)
binding.toolbar.root.title = resources.getString(R.string.home)
if(!Network.isConnected(activity)){
Toaster.show(binding.root,"Connect to internet")
return binding.root
}
binding.rvPinned.apply {
layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
adapter = pinnedRepoAdapter
}
// _binding = FragmentHomeBinding.inflate(inflater, container, false)
// binding.toolbar.root.title = resources.getString(R.string.home)
// if(!Network.isConnected(activity)){
// Toaster.show(binding.root,"Connect to internet")
// return binding.root
// }
// binding.rvPinned.apply {
// layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
// adapter = pinnedRepoAdapter
// }
userViewModel = ViewModelProvider(this)[UserViewModel::class.java]
pinnedRepoViewModel = ViewModelProvider(this)[PinnedRepoViewModel::class.java]

userViewModel.getUserDetail()
userViewModel.observeUserLiveData().observe(viewLifecycleOwner) {
with(binding) {
name.text = it.name
githubUsername.text = it.login
val glideLoader = GlideLoader(requireContext())
glideLoader.loadCircularImage(it.avatar_url, profileImage)
bio.text = it.bio.replace("\n", "")
tvCompany.text = it.company
tvLocation.text = it.location
tvTwitter.text = it.twitter_username
tvTwitter.setOnClickListener {
Browser(requireContext()).launch(goToTwitter())
}
tvFollowers.text = it.followers.toString() + " " + FOLLOWERS
tvFollowing.text = it.following.toString() + " " + FOLLOWING
}
// with(binding) {
// name.text = it.name
// githubUsername.text = it.login
// val glideLoader = GlideLoader(requireContext())
// glideLoader.loadCircularImage(it.avatar_url, profileImage)
// bio.text = it.bio.replace("\n", "")
// tvCompany.text = it.company
// tvLocation.text = it.location
// tvTwitter.text = it.twitter_username
// tvTwitter.setOnClickListener {
// Browser(requireContext()).launch(goToTwitter())
// }
// tvFollowers.text = it.followers.toString() + " " + FOLLOWERS
// tvFollowing.text = it.following.toString() + " " + FOLLOWING
// }
user = it
}

lifecycleScope.launch {
Expand All @@ -85,39 +98,51 @@ class HomeFragment : Fragment() {
pinnedRepoAdapter.setUpPinnedRepoList(it)
}

with(binding) {
tvFollowers.setOnClickListener {
setUpFragment(FollowersFragment())
}
tvFollowing.setOnClickListener {
setUpFragment(FollowingFragment())
}
tvRepositories.setOnClickListener {
setUpFragment(RepositoryFragment())
}
tvStarred.setOnClickListener {
setUpFragment(StarredFragment())
// with(binding) {
// tvFollowers.setOnClickListener {
// setUpFragment(FollowersFragment())
// }
// tvFollowing.setOnClickListener {
// setUpFragment(FollowingFragment())
// }
// tvRepositories.setOnClickListener {
// setUpFragment(RepositoryFragment())
// }
// tvStarred.setOnClickListener {
// setUpFragment(StarredFragment())
// }
// }

// return binding.root
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.Default)
setContent {
HomeScreen(
user = user,
onFollowersClick = { setUpFragment(FollowersFragment()) },
onFollowingClick = { setUpFragment(FollowingFragment()) },
onRepositoriesClick = { setUpFragment(RepositoryFragment()) },
onStarredClick = { setUpFragment(StarredFragment()) },
goToTwitter = { Browser(requireContext()).launch(goToTwitter(it)) }
)
}
}

return binding.root
}

private fun goToTwitter(): String {
return TWITTER_BASE_URL + binding.tvTwitter.text.toString()
private fun goToTwitter(userName: String): String {
return TWITTER_BASE_URL + userName
}

private fun setUpFragment(fragment: Fragment) {
val fragment = fragment
val fragmentManager = requireActivity().supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.nav_host_fragment_activity_dashboard, fragment)
fragmentTransaction.addToBackStack(null)
fragmentTransaction.commit()
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
// override fun onDestroyView() {
// super.onDestroyView()
// _binding = null
// }
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/example/findissues/ui/home/HomeAppBar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.findissues.ui.home

import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import com.example.findissues.R

@Composable
fun HomeAppBar() {
TopAppBar(
title = {
Text(
text = stringResource(id = R.string.home),
fontWeight = FontWeight.Medium,
color = Color.White,
style = MaterialTheme.typography.headlineSmall
)
},
backgroundColor = Color(0xFF0d1117),

)
}

@Preview( showBackground = true)
@Composable
fun HomeAppBarPreview() {
HomeAppBar()
}
Loading

0 comments on commit 886a97c

Please sign in to comment.