Skip to content

Commit 2f5bb16

Browse files
committed
Branch view completed
1 parent 5135a18 commit 2f5bb16

File tree

16 files changed

+330
-28
lines changed

16 files changed

+330
-28
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.prateekcode.githubbrowser.adapter
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import android.widget.TextView
7+
import androidx.recyclerview.widget.DiffUtil
8+
import androidx.recyclerview.widget.RecyclerView
9+
import com.prateekcode.githubbrowser.model.branch.BranchItem
10+
import com.prateekcode.githubbrowser.util.BranchUtil
11+
12+
class BranchAdapter(private val onItemClickListener: OnItemClickListener): RecyclerView.Adapter<BranchAdapter.BranchViewHolder>() {
13+
var branch = emptyList<BranchItem>()
14+
15+
inner class BranchViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView){
16+
17+
init {
18+
itemView.setOnClickListener {
19+
onItemClickListener.onClick(adapterPosition)
20+
}
21+
}
22+
23+
fun bind(branch: BranchItem){
24+
itemView.findViewById<TextView>(android.R.id.text1).text = branch.name
25+
}
26+
}
27+
28+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BranchAdapter.BranchViewHolder {
29+
val itemView = LayoutInflater.from(parent.context).inflate(
30+
android.R.layout.simple_list_item_1,
31+
parent,
32+
false
33+
)
34+
return BranchViewHolder(itemView)
35+
}
36+
37+
override fun onBindViewHolder(holder: BranchAdapter.BranchViewHolder, position: Int) {
38+
val branch = branch[position]
39+
holder.bind(branch)
40+
}
41+
42+
override fun getItemCount(): Int= branch.size
43+
44+
fun setData(branchList: List<BranchItem>){
45+
val branchDiffUtil = BranchUtil(branch, branchList)
46+
val branchDiffResult = DiffUtil.calculateDiff(branchDiffUtil)
47+
this.branch = branchList
48+
branchDiffResult.dispatchUpdatesTo(this)
49+
}
50+
51+
interface OnItemClickListener {
52+
fun onClick(position: Int)
53+
}
54+
55+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.prateekcode.githubbrowser.adapter
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import androidx.recyclerview.widget.DiffUtil
7+
import androidx.recyclerview.widget.RecyclerView
8+
import coil.load
9+
import com.prateekcode.githubbrowser.R
10+
import com.prateekcode.githubbrowser.model.commit.CommitItem
11+
import com.prateekcode.githubbrowser.util.CommitUtil
12+
import com.prateekcode.githubbrowser.util.Utils
13+
import kotlinx.android.synthetic.main.commit_single_item.view.*
14+
15+
class CommitAdapter : RecyclerView.Adapter<CommitAdapter.CommitViewHolder>() {
16+
17+
var commitList = emptyList<CommitItem>()
18+
19+
inner class CommitViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
20+
21+
fun bind(commitItem: CommitItem) {
22+
itemView.date_text_tv.text =
23+
Utils.getFormattedDateFromTimestamp(Utils.getDateToMilliSeconds(commitItem.commit.committer.date))
24+
itemView.commit_message_tv.text = commitItem.commit.message
25+
if (commitItem.commit.committer.name.isNotEmpty()){
26+
itemView.user_name_tv.text = commitItem.commit.committer.name
27+
}else{
28+
itemView.user_name_tv.text = "Not Available"
29+
}
30+
if (commitItem.committer.avatar_url.isNotEmpty()){
31+
itemView.avatar_iv.load(commitItem.committer.avatar_url)
32+
}else{
33+
itemView.avatar_iv.load(R.drawable.ic_avatar)
34+
}
35+
itemView.sha_key_initial.text = Utils.getSixLetterString(commitItem.sha)
36+
}
37+
}
38+
39+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommitViewHolder {
40+
val itemView = LayoutInflater.from(parent.context).inflate(
41+
R.layout.commit_single_item,
42+
parent,
43+
false
44+
)
45+
return CommitViewHolder(itemView)
46+
}
47+
48+
override fun onBindViewHolder(holder: CommitViewHolder, position: Int) {
49+
val commit = commitList[position]
50+
holder.bind(commit)
51+
}
52+
53+
override fun getItemCount(): Int = commitList.size
54+
55+
fun setData(commitItem: List<CommitItem>) {
56+
val commitDiffUtil = CommitUtil(commitList, commitItem)
57+
val commitDiffResult = DiffUtil.calculateDiff(commitDiffUtil)
58+
this.commitList = commitItem
59+
commitDiffResult.dispatchUpdatesTo(this)
60+
}
61+
62+
}

app/src/main/java/com/prateekcode/githubbrowser/api/RestApi.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.prateekcode.githubbrowser.api
22

33
import com.prateekcode.githubbrowser.model.branch.Branch
4+
import com.prateekcode.githubbrowser.model.commit.CommitItem
45
import com.prateekcode.githubbrowser.model.commit.Commits
56
import com.prateekcode.githubbrowser.model.issuedir.IssueCollection
67
import com.prateekcode.githubbrowser.model.repo.Repo
@@ -33,7 +34,7 @@ interface RestApi {
3334
@Path("owner") ownerName: String,
3435
@Path("repo") repoName: String,
3536
@Query("sha") shaKey:String
36-
): Response<Commits>
37+
): Response<List<CommitItem>>
3738

3839

3940
//For Getting the Open Issues

app/src/main/java/com/prateekcode/githubbrowser/fragment/AddRepoFragment.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ class AddRepoFragment : Fragment() {
6868
viewModel!!.repoResponse.observe(viewLifecycleOwner, { response ->
6969
if (response.isSuccessful) {
7070
Log.d(TAG, "Name of the user: ${response.body()!!.name}")
71-
var repoName = response.body()!!.name
71+
val repoName = response.body()!!.name
7272
var descriptionOfRepo = response.body()!!.description
73-
if (descriptionOfRepo==null){
73+
if (descriptionOfRepo == null) {
7474
descriptionOfRepo = "Not Found"
75-
}else{
75+
} else {
7676
descriptionOfRepo
7777
}
7878
val htmlUrl = response.body()!!.html_url
@@ -81,6 +81,12 @@ class AddRepoFragment : Fragment() {
8181
val repo = Repotity(repoName, descriptionOfRepo, htmlUrl, userName)
8282
viewModel!!.insertTheRepo(repo)
8383
fragmentManager!!.popBackStack()
84+
} else {
85+
binding.ownerEditText.text.clear()
86+
binding.repoEditText.text.clear()
87+
binding.ownerEditText.error ="Enter Correct Username/Organization"
88+
binding.repoEditText.error = "Enter Correct Repo Name"
89+
Toast.makeText(context, "Owner/Repo not found", Toast.LENGTH_SHORT).show()
8490
}
8591
})
8692
}

app/src/main/java/com/prateekcode/githubbrowser/fragment/CommitFragment.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,27 @@ import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
88
import androidx.databinding.DataBindingUtil
9+
import androidx.lifecycle.ViewModelProvider
10+
import androidx.recyclerview.widget.LinearLayoutManager
911
import com.prateekcode.githubbrowser.R
12+
import com.prateekcode.githubbrowser.adapter.CommitAdapter
1013
import com.prateekcode.githubbrowser.databinding.FragmentCommitBinding
14+
import com.prateekcode.githubbrowser.db.RepoDatabase
15+
import com.prateekcode.githubbrowser.db.Repodao
16+
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
17+
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
1118

12-
class CommitFragment(branchName:String) : Fragment() {
19+
class CommitFragment(branchName: String, ownerName: String, repoName: String, shaKey: String) :
20+
Fragment() {
1321

1422
lateinit var binding: FragmentCommitBinding
23+
private lateinit var commitAdapter: CommitAdapter
24+
var viewModel: ApiViewModel? = null
25+
lateinit var repodao: Repodao
1526
val branchName = branchName
27+
val ownerName = ownerName
28+
val repoName = repoName
29+
val shaKey = shaKey
1630

1731
override fun onCreateView(
1832
inflater: LayoutInflater, container: ViewGroup?,
@@ -24,6 +38,21 @@ class CommitFragment(branchName:String) : Fragment() {
2438
fragmentManager!!.popBackStack()
2539
}
2640

41+
//Initializing the database
42+
commitAdapter = CommitAdapter()
43+
repodao = RepoDatabase.getDatabase(context!!).repoDao()
44+
binding.commitRecyclerView.layoutManager = LinearLayoutManager(context)
45+
binding.commitRecyclerView.adapter = commitAdapter
46+
val factory = ApiViewModelFactory(repodao)
47+
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
48+
viewModel!!.getCommitMessage(
49+
ownerName, repoName, shaKey
50+
)
51+
viewModel!!.commitMessageResponse.observe(viewLifecycleOwner, { response ->
52+
if (response.isSuccessful) {
53+
commitAdapter.setData(response.body()!!)
54+
}
55+
})
2756
binding.commitMaterialToolbar.subtitle = branchName
2857

2958
return binding.root

app/src/main/java/com/prateekcode/githubbrowser/fragment/DetailFragment.kt

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,34 @@ import android.util.Log
88
import android.view.LayoutInflater
99
import android.view.View
1010
import android.view.ViewGroup
11+
import android.widget.Toast
1112
import androidx.databinding.DataBindingUtil
1213
import androidx.fragment.app.Fragment
1314
import androidx.lifecycle.ViewModelProvider
15+
import androidx.recyclerview.widget.LinearLayoutManager
16+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1417
import com.prateekcode.githubbrowser.R
18+
import com.prateekcode.githubbrowser.adapter.BranchAdapter
1519
import com.prateekcode.githubbrowser.databinding.FragmentDetailBinding
1620
import com.prateekcode.githubbrowser.db.RepoDatabase
1721
import com.prateekcode.githubbrowser.db.Repodao
22+
import com.prateekcode.githubbrowser.db.Repotity
23+
import com.prateekcode.githubbrowser.model.branch.CustomBranch
1824
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
1925
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
2026

2127

22-
class DetailFragment(repoName: String, description: String, htmUrl: String, ownerId: String) : Fragment() {
28+
class DetailFragment(repoName: String, description: String, htmUrl: String, ownerId: String) :
29+
Fragment(), BranchAdapter.OnItemClickListener {
2330

2431
lateinit var binding: FragmentDetailBinding
2532
val repositoryName = repoName
2633
val description = description
2734
val htmlUrl = htmUrl
2835
val ownerId = ownerId
29-
var viewModel: ApiViewModel? =null
36+
var viewModel: ApiViewModel? = null
3037
lateinit var repodao: Repodao
38+
val branchAdapter: BranchAdapter by lazy { BranchAdapter(this) }
3139

3240
override fun onCreateView(
3341
inflater: LayoutInflater, container: ViewGroup?,
@@ -44,15 +52,43 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
4452
fragmentManager!!.popBackStack()
4553
}
4654

47-
binding.branchesIv.setOnClickListener {
48-
replaceFragment(CommitFragment("master"))
49-
}
55+
binding.branchList.adapter = branchAdapter
56+
binding.branchList.layoutManager = LinearLayoutManager(context)
57+
58+
val factory = ApiViewModelFactory(repodao)
59+
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
60+
viewModel!!.getBranches(ownerId, repositoryName)
61+
viewModel!!.branchResponse.observe(viewLifecycleOwner, { response ->
62+
if (response.isSuccessful) {
63+
Log.d(TAG, "BRANCH LIST ${response.body()}")
64+
branchAdapter.setData(response.body()!!)
65+
}
66+
})
67+
5068

5169
issueCounter()
5270
binding.detailMaterialToolbar.setOnMenuItemClickListener { menuItem ->
5371
when (menuItem.itemId) {
5472
R.id.delete_repo_btn -> {
55-
replaceFragment(CommitFragment("master"))
73+
MaterialAlertDialogBuilder(context!!)
74+
.setTitle("Are you sure?")
75+
.setNegativeButton("Cancel") { dialog, _ ->
76+
dialog.dismiss()
77+
}
78+
.setPositiveButton("Delete") { _, _ ->
79+
viewModel!!.deleteTheRepo(
80+
Repotity(
81+
repositoryName,
82+
description,
83+
htmlUrl,
84+
ownerId
85+
)
86+
)
87+
Toast.makeText(context, "File delete successfully", Toast.LENGTH_SHORT)
88+
.show()
89+
fragmentManager!!.popBackStack()
90+
}
91+
.show()
5692
true
5793
}
5894
R.id.open_repo_btn -> {
@@ -73,13 +109,29 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
73109
return binding.root
74110
}
75111

76-
private fun issueCounter(){
77-
val factory = ApiViewModelFactory(repodao)
78-
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
112+
override fun onClick(position: Int) {
113+
viewModel!!.getBranches(ownerId, repositoryName)
114+
viewModel!!.branchResponse.observe(viewLifecycleOwner, { response ->
115+
if (response.isSuccessful) {
116+
Log.d(TAG, "BRANCH LIST ${response.body()}")
117+
replaceFragment(CommitFragment(
118+
response.body()!![position].name,
119+
ownerId,
120+
repositoryName,
121+
response.body()!![position].commit.sha
122+
))
123+
}
124+
})
125+
}
126+
127+
private fun issueCounter() {
128+
// val factory = ApiViewModelFactory(repodao)
129+
// viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
79130
viewModel!!.getOpenIssue(ownerId, repositoryName, "open")
80-
viewModel!!.openIssueResponse.observe(viewLifecycleOwner, {response->
81-
if (response.isSuccessful){
82-
if (response.body()!!.isNotEmpty()){
131+
viewModel!!.openIssueResponse.observe(viewLifecycleOwner, { response ->
132+
if (response.isSuccessful) {
133+
if (response.body()!!.isNotEmpty()) {
134+
Log.d(TAG, "issueCounter: ${response.body()}")
83135
binding.issueTv.text = "ISSUES(${response!!.body()!!.size})"
84136
}
85137
}
@@ -92,4 +144,10 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
92144
transaction.addToBackStack(null)
93145
transaction.commit()
94146
}
147+
148+
companion object {
149+
const val TAG = "DETAIL_FRAGMENT"
150+
}
151+
152+
95153
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.prateekcode.githubbrowser.model.branch
2+
3+
data class CustomBranch(
4+
val branchName:String,
5+
val shaKey:String
6+
)

app/src/main/java/com/prateekcode/githubbrowser/model/commit/Committer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package com.prateekcode.githubbrowser.model.commit
33
data class Committer(
44
val date: String,
55
val email: String,
6-
val name: String
6+
var name: String
77
)

app/src/main/java/com/prateekcode/githubbrowser/repository/Repository.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.prateekcode.githubbrowser.api.RetrofitInstance
44
import com.prateekcode.githubbrowser.db.Repodao
55
import com.prateekcode.githubbrowser.db.Repotity
66
import com.prateekcode.githubbrowser.model.branch.Branch
7+
import com.prateekcode.githubbrowser.model.commit.CommitItem
78
import com.prateekcode.githubbrowser.model.commit.Commits
89
import com.prateekcode.githubbrowser.model.issuedir.IssueCollection
910
import com.prateekcode.githubbrowser.model.repo.Repo
@@ -29,7 +30,7 @@ class Repository(private val repodao: Repodao) {
2930
ownerName: String,
3031
repoName: String,
3132
shaKey: String
32-
): Response<Commits> {
33+
): Response<List<CommitItem>> {
3334
return RetrofitInstance.api.getCommitMessage(ownerName, repoName, shaKey)
3435
}
3536

0 commit comments

Comments
 (0)