Skip to content

Commit caa06cb

Browse files
committed
Internet connectivity added with Snackbar
1 parent 425af2b commit caa06cb

File tree

4 files changed

+142
-90
lines changed

4 files changed

+142
-90
lines changed

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

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.prateekcode.githubbrowser.fragment
22

33

4+
import android.content.Intent
45
import android.os.Bundle
6+
import android.provider.Settings
57
import android.util.Log
68
import android.view.LayoutInflater
79
import android.view.View
@@ -11,11 +13,13 @@ import android.widget.Toast
1113
import androidx.databinding.DataBindingUtil
1214
import androidx.fragment.app.Fragment
1315
import androidx.lifecycle.ViewModelProvider
16+
import com.google.android.material.snackbar.Snackbar
1417
import com.prateekcode.githubbrowser.R
1518
import com.prateekcode.githubbrowser.databinding.FragmentAddRepoBinding
1619
import com.prateekcode.githubbrowser.db.RepoDatabase
1720
import com.prateekcode.githubbrowser.db.Repodao
1821
import com.prateekcode.githubbrowser.db.Repotity
22+
import com.prateekcode.githubbrowser.util.Utils
1923
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
2024
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
2125

@@ -36,22 +40,27 @@ class AddRepoFragment : Fragment() {
3640
//Initializing the database
3741
repodao = RepoDatabase.getDatabase(context!!).repoDao()
3842

39-
40-
41-
4243
binding.addMaterialToolbar.setNavigationOnClickListener {
4344
fragmentManager!!.popBackStack()
4445
}
4546

46-
binding.addRepositoryBtn.setOnClickListener {
47-
if (isEmptyTextInput(binding.ownerEditText) || isEmptyTextInput(binding.repoEditText)){
48-
binding.ownerEditText.error ="Enter Username/Organization"
49-
binding.repoEditText.error = "Enter Repo Name"
50-
Toast.makeText(context, "Enter correct details", Toast.LENGTH_SHORT).show()
51-
}else{
52-
//Toast.makeText(context, "Repo added", Toast.LENGTH_SHORT).show()
53-
findTheRepo(binding.ownerEditText.text.toString(), binding.repoEditText.text.toString())
47+
if (Utils.isConnected(context!!)){
48+
binding.addRepositoryBtn.setOnClickListener {
49+
if (isEmptyTextInput(binding.ownerEditText) || isEmptyTextInput(binding.repoEditText)){
50+
binding.ownerEditText.error ="Enter Username/Organization"
51+
binding.repoEditText.error = "Enter Repo Name"
52+
Toast.makeText(context, "Enter correct details", Toast.LENGTH_SHORT).show()
53+
}else{
54+
//Toast.makeText(context, "Repo added", Toast.LENGTH_SHORT).show()
55+
findTheRepo(binding.ownerEditText.text.toString(), binding.repoEditText.text.toString())
56+
}
5457
}
58+
}else{
59+
Snackbar.make(activity!!.findViewById(android.R.id.content), "You're not connected to Internet", Snackbar.LENGTH_INDEFINITE)
60+
.setAction("Setting"){
61+
startActivity(Intent(Settings.ACTION_SETTINGS))
62+
}
63+
.show()
5564
}
5665

5766
return binding.root
@@ -67,26 +76,38 @@ class AddRepoFragment : Fragment() {
6776
viewModel!!.githubRepository(userName, repoName)
6877
viewModel!!.repoResponse.observe(viewLifecycleOwner, { response ->
6978
if (response.isSuccessful) {
70-
Log.d(TAG, "Name of the user: ${response.body()!!.name}")
71-
val repoName = response.body()!!.name
72-
var descriptionOfRepo = response.body()!!.description
73-
if (descriptionOfRepo == null) {
74-
descriptionOfRepo = "Not Found"
79+
if (response.body() != null) {
80+
Log.d(TAG, "Name of the user: ${response.body()!!.name}")
81+
val repoName = response.body()!!.name
82+
var descriptionOfRepo = response.body()!!.description
83+
if (descriptionOfRepo == null) {
84+
descriptionOfRepo = "Not Found"
85+
} else {
86+
descriptionOfRepo
87+
}
88+
val htmlUrl = response.body()!!.html_url
89+
Log.d(TAG, "Description of the user: ${response.body()!!.description}")
90+
Log.d(TAG, "Html Url of the user: ${response.body()!!.html_url}")
91+
val repo = Repotity(repoName, descriptionOfRepo, htmlUrl, userName)
92+
viewModel!!.insertTheRepo(repo)
93+
fragmentManager!!.popBackStack()
7594
} else {
76-
descriptionOfRepo
95+
binding.ownerEditText.text.clear()
96+
binding.repoEditText.text.clear()
97+
binding.ownerEditText.error = "Enter Correct Username/Organization"
98+
binding.repoEditText.error = "Enter Correct Repo Name"
99+
Toast.makeText(context, "Owner/Repo not found", Toast.LENGTH_SHORT).show()
77100
}
78-
val htmlUrl = response.body()!!.html_url
79-
Log.d(TAG, "Description of the user: ${response.body()!!.description}")
80-
Log.d(TAG, "Html Url of the user: ${response.body()!!.html_url}")
81-
val repo = Repotity(repoName, descriptionOfRepo, htmlUrl, userName)
82-
viewModel!!.insertTheRepo(repo)
83-
fragmentManager!!.popBackStack()
84101
} else {
85102
binding.ownerEditText.text.clear()
86103
binding.repoEditText.text.clear()
87-
binding.ownerEditText.error ="Enter Correct Username/Organization"
104+
binding.ownerEditText.error = "Enter Correct Username/Organization"
88105
binding.repoEditText.error = "Enter Correct Repo Name"
89106
Toast.makeText(context, "Owner/Repo not found", Toast.LENGTH_SHORT).show()
107+
Log.d(TAG, "findTheRepo: Getting the error message ${response.message()}")
108+
Log.d(TAG, "findTheRepo: Getting the error message ${response.raw()}")
109+
Log.d(TAG, "findTheRepo: Getting the error message ${response.errorBody()}")
110+
Log.d(TAG, "findTheRepo: Getting the error message ${response.headers()}")
90111
}
91112
})
92113
}

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package com.prateekcode.githubbrowser.fragment
22

3+
import android.content.Intent
34
import android.os.Bundle
5+
import android.provider.Settings
46
import androidx.fragment.app.Fragment
57
import android.view.LayoutInflater
68
import android.view.View
79
import android.view.ViewGroup
810
import androidx.databinding.DataBindingUtil
911
import androidx.lifecycle.ViewModelProvider
1012
import androidx.recyclerview.widget.LinearLayoutManager
13+
import com.google.android.material.snackbar.Snackbar
1114
import com.prateekcode.githubbrowser.R
1215
import com.prateekcode.githubbrowser.adapter.CommitAdapter
1316
import com.prateekcode.githubbrowser.databinding.FragmentCommitBinding
1417
import com.prateekcode.githubbrowser.db.RepoDatabase
1518
import com.prateekcode.githubbrowser.db.Repodao
19+
import com.prateekcode.githubbrowser.util.Utils
1620
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
1721
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
1822

@@ -38,22 +42,33 @@ class CommitFragment(branchName: String, ownerName: String, repoName: String, sh
3842
fragmentManager!!.popBackStack()
3943
}
4044

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-
})
56-
binding.commitMaterialToolbar.subtitle = branchName
45+
if (Utils.isConnected(context!!)){
46+
//Initializing the database
47+
commitAdapter = CommitAdapter()
48+
repodao = RepoDatabase.getDatabase(context!!).repoDao()
49+
binding.commitRecyclerView.layoutManager = LinearLayoutManager(context)
50+
binding.commitRecyclerView.adapter = commitAdapter
51+
binding.commitProgressBar.visibility = View.VISIBLE
52+
val factory = ApiViewModelFactory(repodao)
53+
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
54+
viewModel!!.getCommitMessage(
55+
ownerName, repoName, shaKey
56+
)
57+
viewModel!!.commitMessageResponse.observe(viewLifecycleOwner, { response ->
58+
if (response.isSuccessful) {
59+
binding.commitProgressBar.visibility = View.GONE
60+
commitAdapter.setData(response.body()!!)
61+
}
62+
})
63+
binding.commitMaterialToolbar.subtitle = branchName
64+
}else{
65+
Snackbar.make(activity!!.findViewById(android.R.id.content), "You're not connected to Internet", Snackbar.LENGTH_INDEFINITE)
66+
.setAction("Setting"){
67+
startActivity(Intent(Settings.ACTION_SETTINGS))
68+
}
69+
.show()
70+
}
71+
5772

5873
return binding.root
5974
}

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

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.content.ActivityNotFoundException
44
import android.content.Intent
55
import android.net.Uri
66
import android.os.Bundle
7+
import android.provider.Settings
78
import android.util.Log
89
import android.view.LayoutInflater
910
import android.view.View
@@ -14,13 +15,15 @@ import androidx.fragment.app.Fragment
1415
import androidx.lifecycle.ViewModelProvider
1516
import androidx.recyclerview.widget.LinearLayoutManager
1617
import com.google.android.material.dialog.MaterialAlertDialogBuilder
18+
import com.google.android.material.snackbar.Snackbar
1719
import com.prateekcode.githubbrowser.R
1820
import com.prateekcode.githubbrowser.adapter.BranchAdapter
1921
import com.prateekcode.githubbrowser.databinding.FragmentDetailBinding
2022
import com.prateekcode.githubbrowser.db.RepoDatabase
2123
import com.prateekcode.githubbrowser.db.Repodao
2224
import com.prateekcode.githubbrowser.db.Repotity
2325
import com.prateekcode.githubbrowser.model.branch.CustomBranch
26+
import com.prateekcode.githubbrowser.util.Utils
2427
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
2528
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
2629

@@ -47,65 +50,72 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
4750

4851
//Initializing the database
4952
repodao = RepoDatabase.getDatabase(context!!).repoDao()
50-
5153
binding.detailMaterialToolbar.setNavigationOnClickListener {
5254
fragmentManager!!.popBackStack()
5355
}
54-
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-
68-
69-
issueCounter()
70-
binding.detailMaterialToolbar.setOnMenuItemClickListener { menuItem ->
71-
when (menuItem.itemId) {
72-
R.id.delete_repo_btn -> {
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
56+
if (Utils.isConnected(context!!)){
57+
binding.branchList.adapter = branchAdapter
58+
binding.branchList.layoutManager = LinearLayoutManager(context)
59+
60+
val factory = ApiViewModelFactory(repodao)
61+
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
62+
viewModel!!.getBranches(ownerId, repositoryName)
63+
viewModel!!.branchResponse.observe(viewLifecycleOwner, { response ->
64+
if (response.isSuccessful) {
65+
Log.d(TAG, "BRANCH LIST ${response.body()}")
66+
branchAdapter.setData(response.body()!!)
67+
}
68+
})
69+
70+
71+
issueCounter()
72+
binding.detailMaterialToolbar.setOnMenuItemClickListener { menuItem ->
73+
when (menuItem.itemId) {
74+
R.id.delete_repo_btn -> {
75+
MaterialAlertDialogBuilder(context!!)
76+
.setTitle("Are you sure?")
77+
.setNegativeButton("Cancel") { dialog, _ ->
78+
dialog.dismiss()
79+
}
80+
.setPositiveButton("Delete") { _, _ ->
81+
viewModel!!.deleteTheRepo(
82+
Repotity(
83+
repositoryName,
84+
description,
85+
htmlUrl,
86+
ownerId
87+
)
8588
)
86-
)
87-
Toast.makeText(context, "File delete successfully", Toast.LENGTH_SHORT)
88-
.show()
89-
fragmentManager!!.popBackStack()
89+
Toast.makeText(context, "File delete successfully", Toast.LENGTH_SHORT)
90+
.show()
91+
fragmentManager!!.popBackStack()
92+
}
93+
.show()
94+
true
95+
}
96+
R.id.open_repo_btn -> {
97+
try {
98+
val uri: Uri = Uri.parse("googlechrome://navigate?url=$htmlUrl")
99+
val i = Intent(Intent.ACTION_VIEW, uri)
100+
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
101+
startActivity(i)
102+
} catch (e: ActivityNotFoundException) {
103+
Log.d("TAG", "onCreate: $e")
90104
}
91-
.show()
92-
true
93-
}
94-
R.id.open_repo_btn -> {
95-
try {
96-
val uri: Uri = Uri.parse("googlechrome://navigate?url=$htmlUrl")
97-
val i = Intent(Intent.ACTION_VIEW, uri)
98-
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
99-
startActivity(i)
100-
} catch (e: ActivityNotFoundException) {
101-
Log.d("TAG", "onCreate: $e")
105+
true
102106
}
103-
true
107+
else -> false
104108
}
105-
else -> false
106109
}
110+
}else{
111+
Snackbar.make(activity!!.findViewById(android.R.id.content), "You're not connected to Internet", Snackbar.LENGTH_INDEFINITE)
112+
.setAction("Setting"){
113+
startActivity(Intent(Settings.ACTION_SETTINGS))
114+
}
115+
.show()
107116
}
108117

118+
109119
return binding.root
110120
}
111121

@@ -147,6 +157,7 @@ class DetailFragment(repoName: String, description: String, htmUrl: String, owne
147157

148158
companion object {
149159
const val TAG = "DETAIL_FRAGMENT"
160+
const val SETTINGS_PACKAGE = "com.android.settings"
150161
}
151162

152163

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.databinding.DataBindingUtil
1010
import androidx.lifecycle.ViewModelProvider
1111
import androidx.recyclerview.widget.LinearLayoutManager
1212
import com.google.android.material.dialog.MaterialAlertDialogBuilder
13+
import com.google.android.material.snackbar.Snackbar
1314
import com.prateekcode.githubbrowser.R
1415
import com.prateekcode.githubbrowser.adapter.RepoAdapter
1516
import com.prateekcode.githubbrowser.databinding.FragmentHomeBinding
@@ -88,7 +89,11 @@ class HomeFragment : Fragment(), RepoAdapter.OnItemClickListener {
8889
}
8990
}
9091
}else{
91-
Toast.makeText(context, "You're not connected with Internet", Toast.LENGTH_SHORT).show()
92+
Snackbar.make(activity!!.findViewById(android.R.id.content), "You're not connected to Internet", Snackbar.LENGTH_INDEFINITE)
93+
.setAction("Setting"){
94+
startActivity(Intent(Settings.ACTION_SETTINGS))
95+
}
96+
.show()
9297
}
9398

9499
return binding.root

0 commit comments

Comments
 (0)