Skip to content

Commit e2f3e34

Browse files
committed
All fragment UI dynamically initialized
1 parent 2b4f9b1 commit e2f3e34

File tree

10 files changed

+191
-199
lines changed

10 files changed

+191
-199
lines changed

app/src/main/java/com/prateekcode/githubbrowser/db/RepoDatabase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.room.Database
55
import androidx.room.Room
66
import androidx.room.RoomDatabase
77

8-
@Database(entities = [Repotity::class], version = 1, exportSchema = false)
8+
@Database(entities = [Repotity::class], version = 2, exportSchema = false)
99
abstract class RepoDatabase:RoomDatabase() {
1010

1111
abstract fun repoDao(): Repodao

app/src/main/java/com/prateekcode/githubbrowser/db/Repotity.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@ data class Repotity(
1111
@ColumnInfo(name = "Description")
1212
val descriptionRepo:String,
1313
@ColumnInfo(name = "HTML Url")
14-
val htmlUrl:String
14+
val htmlUrl:String,
15+
@ColumnInfo(name = "Owner")
16+
val ownerName:String
1517
)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class AddRepoFragment : Fragment() {
7878
val htmlUrl = response.body()!!.html_url
7979
Log.d(TAG, "Description of the user: ${response.body()!!.description}")
8080
Log.d(TAG, "Html Url of the user: ${response.body()!!.html_url}")
81-
val repo = Repotity(repoName, descriptionOfRepo, htmlUrl)
81+
val repo = Repotity(repoName, descriptionOfRepo, htmlUrl, userName)
8282
viewModel!!.insertTheRepo(repo)
8383
fragmentManager!!.popBackStack()
8484
}

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

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,25 @@ import androidx.fragment.app.Fragment
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8+
import androidx.databinding.DataBindingUtil
89
import com.prateekcode.githubbrowser.R
10+
import com.prateekcode.githubbrowser.databinding.FragmentCommitBinding
911

10-
// TODO: Rename parameter arguments, choose names that match
11-
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
12-
private const val ARG_PARAM1 = "param1"
13-
private const val ARG_PARAM2 = "param2"
14-
15-
/**
16-
* A simple [Fragment] subclass.
17-
* Use the [CommitFragment.newInstance] factory method to
18-
* create an instance of this fragment.
19-
*/
2012
class CommitFragment : Fragment() {
21-
// TODO: Rename and change types of parameters
22-
private var param1: String? = null
23-
private var param2: String? = null
2413

25-
override fun onCreate(savedInstanceState: Bundle?) {
26-
super.onCreate(savedInstanceState)
27-
arguments?.let {
28-
param1 = it.getString(ARG_PARAM1)
29-
param2 = it.getString(ARG_PARAM2)
30-
}
31-
}
14+
lateinit var binding: FragmentCommitBinding
3215

3316
override fun onCreateView(
3417
inflater: LayoutInflater, container: ViewGroup?,
3518
savedInstanceState: Bundle?
3619
): View? {
37-
// Inflate the layout for this fragment
38-
return inflater.inflate(R.layout.fragment_commit, container, false)
39-
}
20+
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_commit, container, false)
4021

41-
companion object {
42-
/**
43-
* Use this factory method to create a new instance of
44-
* this fragment using the provided parameters.
45-
*
46-
* @param param1 Parameter 1.
47-
* @param param2 Parameter 2.
48-
* @return A new instance of fragment CommitFragment.
49-
*/
50-
// TODO: Rename and change types and number of parameters
51-
@JvmStatic
52-
fun newInstance(param1: String, param2: String) =
53-
CommitFragment().apply {
54-
arguments = Bundle().apply {
55-
putString(ARG_PARAM1, param1)
56-
putString(ARG_PARAM2, param2)
57-
}
58-
}
22+
binding.commitMaterialToolbar.setNavigationOnClickListener {
23+
fragmentManager!!.popBackStack()
24+
}
25+
26+
return binding.root
5927
}
28+
6029
}
Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,67 @@
11
package com.prateekcode.githubbrowser.fragment
22

3+
import android.content.ActivityNotFoundException
4+
import android.content.Intent
5+
import android.net.Uri
36
import android.os.Bundle
4-
import androidx.fragment.app.Fragment
7+
import android.util.Log
58
import android.view.LayoutInflater
69
import android.view.View
710
import android.view.ViewGroup
11+
import androidx.databinding.DataBindingUtil
12+
import androidx.fragment.app.Fragment
813
import com.prateekcode.githubbrowser.R
14+
import com.prateekcode.githubbrowser.databinding.FragmentDetailBinding
915

10-
// TODO: Rename parameter arguments, choose names that match
11-
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
12-
private const val ARG_PARAM1 = "param1"
13-
private const val ARG_PARAM2 = "param2"
14-
15-
/**
16-
* A simple [Fragment] subclass.
17-
* Use the [DetailFragment.newInstance] factory method to
18-
* create an instance of this fragment.
19-
*/
20-
class DetailFragment : Fragment() {
21-
// TODO: Rename and change types of parameters
22-
private var param1: String? = null
23-
private var param2: String? = null
24-
25-
override fun onCreate(savedInstanceState: Bundle?) {
26-
super.onCreate(savedInstanceState)
27-
arguments?.let {
28-
param1 = it.getString(ARG_PARAM1)
29-
param2 = it.getString(ARG_PARAM2)
30-
}
31-
}
16+
17+
class DetailFragment(repoName: String, description: String, htmUrl: String, ownerId: String) : Fragment() {
18+
19+
lateinit var binding: FragmentDetailBinding
20+
val repositoryName = repoName
21+
val description = description
22+
val htmlUrl = htmUrl
23+
val ownerId = ownerId
3224

3325
override fun onCreateView(
3426
inflater: LayoutInflater, container: ViewGroup?,
3527
savedInstanceState: Bundle?
3628
): View? {
37-
// Inflate the layout for this fragment
38-
return inflater.inflate(R.layout.fragment_detail, container, false)
39-
}
29+
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail, container, false)
30+
binding.detailTitle.text = repositoryName
31+
binding.detailDescription.text = description
4032

41-
companion object {
42-
/**
43-
* Use this factory method to create a new instance of
44-
* this fragment using the provided parameters.
45-
*
46-
* @param param1 Parameter 1.
47-
* @param param2 Parameter 2.
48-
* @return A new instance of fragment DetailFragment.
49-
*/
50-
// TODO: Rename and change types and number of parameters
51-
@JvmStatic
52-
fun newInstance(param1: String, param2: String) =
53-
DetailFragment().apply {
54-
arguments = Bundle().apply {
55-
putString(ARG_PARAM1, param1)
56-
putString(ARG_PARAM2, param2)
33+
binding.detailMaterialToolbar.setNavigationOnClickListener {
34+
fragmentManager!!.popBackStack()
35+
}
36+
37+
binding.detailMaterialToolbar.setOnMenuItemClickListener { menuItem ->
38+
when (menuItem.itemId) {
39+
R.id.delete_repo_btn -> {
40+
replaceFragment(CommitFragment())
41+
true
5742
}
43+
R.id.open_repo_btn -> {
44+
try {
45+
val uri: Uri = Uri.parse("googlechrome://navigate?url=$htmlUrl")
46+
val i = Intent(Intent.ACTION_VIEW, uri)
47+
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
48+
startActivity(i)
49+
} catch (e: ActivityNotFoundException) {
50+
Log.d("TAG", "onCreate: $e")
51+
}
52+
true
53+
}
54+
else -> false
5855
}
56+
}
57+
58+
return binding.root
59+
}
60+
61+
private fun replaceFragment(fragment: Fragment) {
62+
val transaction = activity!!.supportFragmentManager.beginTransaction()
63+
transaction.replace(R.id.fragment_container, fragment)
64+
transaction.addToBackStack(null)
65+
transaction.commit()
5966
}
6067
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ class HomeFragment : Fragment(), RepoAdapter.OnItemClickListener {
7474
}
7575

7676
override fun onClick(position: Int) {
77-
replaceFragment(DetailFragment())
77+
replaceFragment(DetailFragment(
78+
repoList[position].repositoryName,
79+
repoList[position].descriptionRepo,
80+
repoList[position].htmlUrl,
81+
repoList[position].ownerName
82+
))
7883
}
7984

8085
override fun onShareButtonClick(position: Int) {

app/src/main/java/com/prateekcode/githubbrowser/util/RepoUtil.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ class RepoUtil(private val oldList: List<Repotity>, private val newList: List<Re
1616
return oldList[oldItemPosition].repositoryName == newList[newItemPosition].repositoryName
1717
&& oldList[oldItemPosition].descriptionRepo == newList[newItemPosition].descriptionRepo
1818
&& oldList[oldItemPosition].htmlUrl == newList[newItemPosition].htmlUrl
19+
&& oldList[oldItemPosition].ownerName == newList[newItemPosition].ownerName
1920
}
2021
}

app/src/main/res/layout/commit_single_item.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
android:layout_height="wrap_content"
2727
android:layout_alignParentRight="true"
2828
android:layout_margin="4dp"
29-
android:background="@color/purple_200"
29+
android:background="@color/teal_700"
3030
android:paddingStart="4dp"
3131
android:paddingEnd="4dp"
3232
android:text="a30efa"
Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
3+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools"
5-
android:layout_width="match_parent"
6-
android:layout_height="match_parent"
7-
tools:context=".fragment.CommitFragment">
5+
xmlns:tools="http://schemas.android.com/tools">
86

9-
<com.google.android.material.appbar.MaterialToolbar
10-
android:id="@+id/commit_material_toolbar"
7+
<RelativeLayout
118
android:layout_width="match_parent"
12-
android:layout_height="?attr/actionBarSize"
13-
android:background="@color/black"
14-
app:navigationIcon="@drawable/ic_back"
15-
app:subtitle="Branch Name"
16-
app:subtitleTextColor="@color/white"
17-
app:title="Commits"
18-
app:titleTextColor="@color/white" />
9+
android:layout_height="match_parent"
10+
tools:context=".fragment.CommitFragment">
1911

20-
<ProgressBar
21-
android:id="@+id/commit_progress_bar"
22-
android:layout_width="wrap_content"
23-
android:layout_height="wrap_content"
24-
android:layout_centerInParent="true"
25-
android:backgroundTint="@color/black"
26-
android:visibility="gone" />
12+
<com.google.android.material.appbar.MaterialToolbar
13+
android:id="@+id/commit_material_toolbar"
14+
android:layout_width="match_parent"
15+
android:layout_height="?attr/actionBarSize"
16+
android:background="@color/black"
17+
app:navigationIcon="@drawable/ic_back"
18+
app:subtitle="Branch Name"
19+
app:subtitleTextColor="@color/white"
20+
app:title="Commits"
21+
app:titleTextColor="@color/white" />
2722

28-
<androidx.recyclerview.widget.RecyclerView
29-
android:id="@+id/commit_recycler_view"
30-
android:layout_width="match_parent"
31-
android:layout_height="match_parent"
32-
android:layout_below="@id/commit_material_toolbar"
33-
android:visibility="visible"
34-
tools:listitem="@layout/commit_single_item" />
23+
<ProgressBar
24+
android:id="@+id/commit_progress_bar"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_centerInParent="true"
28+
android:backgroundTint="@color/black"
29+
android:visibility="gone" />
30+
31+
<androidx.recyclerview.widget.RecyclerView
32+
android:id="@+id/commit_recycler_view"
33+
android:layout_width="match_parent"
34+
android:layout_height="match_parent"
35+
android:layout_below="@id/commit_material_toolbar"
36+
android:visibility="visible"
37+
tools:listitem="@layout/commit_single_item" />
3538

3639

37-
</RelativeLayout>
40+
</RelativeLayout>
41+
</layout>

0 commit comments

Comments
 (0)