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