@@ -10,8 +10,13 @@ import android.view.View
1010import android.view.ViewGroup
1111import androidx.databinding.DataBindingUtil
1212import androidx.fragment.app.Fragment
13+ import androidx.lifecycle.ViewModelProvider
1314import com.prateekcode.githubbrowser.R
1415import com.prateekcode.githubbrowser.databinding.FragmentDetailBinding
16+ import com.prateekcode.githubbrowser.db.RepoDatabase
17+ import com.prateekcode.githubbrowser.db.Repodao
18+ import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
19+ import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
1520
1621
1722class DetailFragment (repoName : String , description : String , htmUrl : String , ownerId : String ) : Fragment() {
@@ -21,6 +26,8 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
2126 val description = description
2227 val htmlUrl = htmUrl
2328 val ownerId = ownerId
29+ var viewModel: ApiViewModel ? = null
30+ lateinit var repodao: Repodao
2431
2532 override fun onCreateView (
2633 inflater : LayoutInflater , container : ViewGroup ? ,
@@ -30,14 +37,22 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
3037 binding.detailTitle.text = repositoryName
3138 binding.detailDescription.text = description
3239
40+ // Initializing the database
41+ repodao = RepoDatabase .getDatabase(context!! ).repoDao()
42+
3343 binding.detailMaterialToolbar.setNavigationOnClickListener {
3444 fragmentManager!! .popBackStack()
3545 }
3646
47+ binding.branchesIv.setOnClickListener {
48+ replaceFragment(CommitFragment (" master" ))
49+ }
50+
51+ issueCounter()
3752 binding.detailMaterialToolbar.setOnMenuItemClickListener { menuItem ->
3853 when (menuItem.itemId) {
3954 R .id.delete_repo_btn -> {
40- replaceFragment(CommitFragment ())
55+ replaceFragment(CommitFragment (" master " ))
4156 true
4257 }
4358 R .id.open_repo_btn -> {
@@ -58,6 +73,19 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
5873 return binding.root
5974 }
6075
76+ private fun issueCounter (){
77+ val factory = ApiViewModelFactory (repodao)
78+ viewModel = ViewModelProvider (this , factory).get(ApiViewModel ::class .java)
79+ viewModel!! .getOpenIssue(ownerId, repositoryName, " open" )
80+ viewModel!! .openIssueResponse.observe(viewLifecycleOwner, {response->
81+ if (response.isSuccessful){
82+ if (response.body()!! .isNotEmpty()){
83+ binding.issueTv.text = " ISSUES(${response!! .body()!! .size} )"
84+ }
85+ }
86+ })
87+ }
88+
6189 private fun replaceFragment (fragment : Fragment ) {
6290 val transaction = activity!! .supportFragmentManager.beginTransaction()
6391 transaction.replace(R .id.fragment_container, fragment)
0 commit comments