Skip to content

Commit 425af2b

Browse files
committed
Internet connectivity added
1 parent 2f5bb16 commit 425af2b

24 files changed

+73
-111
lines changed
23.1 KB
Loading

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

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

33
import android.content.Intent
44
import android.os.Bundle
5+
import android.provider.Settings
56
import android.view.*
67
import androidx.fragment.app.Fragment
78
import android.widget.Toast
89
import androidx.databinding.DataBindingUtil
910
import androidx.lifecycle.ViewModelProvider
1011
import androidx.recyclerview.widget.LinearLayoutManager
12+
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1113
import com.prateekcode.githubbrowser.R
1214
import com.prateekcode.githubbrowser.adapter.RepoAdapter
1315
import com.prateekcode.githubbrowser.databinding.FragmentHomeBinding
1416
import com.prateekcode.githubbrowser.db.RepoDatabase
1517
import com.prateekcode.githubbrowser.db.Repodao
1618
import com.prateekcode.githubbrowser.db.Repotity
19+
import com.prateekcode.githubbrowser.util.Utils
1720
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
1821
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
1922

@@ -32,44 +35,62 @@ class HomeFragment : Fragment(), RepoAdapter.OnItemClickListener {
3235
): View? {
3336
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
3437

35-
//Initializing the database
36-
repodao = RepoDatabase.getDatabase(context!!).repoDao()
37-
val factory = ApiViewModelFactory(repodao)
38-
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
3938

40-
repoList = repodao.getAllRepo()
41-
if (repoList.isEmpty()){
42-
binding.trackRepoTv.visibility = View.VISIBLE
43-
binding.addRepoButton.visibility = View.VISIBLE
44-
}else{
45-
binding.trackRepoTv.visibility = View.GONE
46-
binding.addRepoButton.visibility = View.GONE
47-
}
39+
if (Utils.isConnected(context!!)){
40+
//Initializing the database
41+
repodao = RepoDatabase.getDatabase(context!!).repoDao()
42+
val factory = ApiViewModelFactory(repodao)
43+
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
44+
45+
repoList = repodao.getAllRepo()
46+
if (repoList.isEmpty()){
47+
binding.trackRepoTv.visibility = View.VISIBLE
48+
binding.addRepoButton.visibility = View.VISIBLE
49+
}else{
50+
binding.trackRepoTv.visibility = View.GONE
51+
binding.addRepoButton.visibility = View.GONE
52+
repoAdapter.setData(repoList)
53+
val layoutManager = LinearLayoutManager(context!!)
54+
binding.repoRecyclerView.layoutManager = layoutManager
55+
binding.repoRecyclerView.adapter = repoAdapter
56+
layoutManager.stackFromEnd = true
57+
layoutManager.reverseLayout = true
58+
}
4859

49-
binding.addRepoButton.setOnClickListener {
50-
//Toast.makeText(context, "Hey Buddy!!!!!!", Toast.LENGTH_SHORT).show()
51-
replaceFragment(AddRepoFragment())
52-
}
60+
binding.addRepoButton.setOnClickListener {
61+
//Toast.makeText(context, "Hey Buddy!!!!!!", Toast.LENGTH_SHORT).show()
62+
replaceFragment(AddRepoFragment())
63+
}
5364

54-
repoAdapter.setData(repoList)
55-
val layoutManager = LinearLayoutManager(context!!)
56-
binding.repoRecyclerView.layoutManager = layoutManager
57-
binding.repoRecyclerView.adapter = repoAdapter
58-
layoutManager.stackFromEnd = true
59-
layoutManager.reverseLayout = true
60-
61-
binding.homeMaterialToolbar.setOnMenuItemClickListener {
62-
menuItem ->
63-
when (menuItem.itemId) {
64-
R.id.add_repo_menu_btn -> {
65-
replaceFragment(AddRepoFragment())
66-
true
65+
binding.homeMaterialToolbar.setOnMenuItemClickListener {
66+
menuItem ->
67+
when (menuItem.itemId) {
68+
R.id.add_repo_menu_btn -> {
69+
replaceFragment(AddRepoFragment())
70+
true
71+
}
72+
R.id.delete_all-> {
73+
MaterialAlertDialogBuilder(context!!)
74+
.setTitle("Delete All Repo")
75+
.setMessage("Are you sure?")
76+
.setNegativeButton("Cancel") { dialog, _ ->
77+
dialog.dismiss()
78+
}
79+
.setPositiveButton("Delete") { _, _ ->
80+
viewModel!!.deleteEntireDb()
81+
Toast.makeText(context, "Entire repo deleted", Toast.LENGTH_SHORT)
82+
.show()
83+
}
84+
.show()
85+
true
86+
}
87+
else -> false
6788
}
68-
else -> false
6989
}
90+
}else{
91+
Toast.makeText(context, "You're not connected with Internet", Toast.LENGTH_SHORT).show()
7092
}
7193

72-
7394
return binding.root
7495
}
7596

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

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:height="24dp" android:tint="#FFFFFF"
2+
android:viewportHeight="24" android:viewportWidth="24"
3+
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M6,19c0,1.1 0.9,2 2,2h8c1.1,0 2,-0.9 2,-2V7H6v12zM19,4h-3.5l-1,-1h-5l-1,1H5v2h14V4z"/>
5+
</vector>

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

Lines changed: 0 additions & 14 deletions
This file was deleted.

app/src/main/res/menu/home_menu.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@
44

55
<item
66
android:id="@+id/add_repo_menu_btn"
7-
android:title="Add Repo"
87
android:icon="@drawable/ic_add"
9-
app:showAsAction="always"/>
8+
android:title="Add Repo"
9+
app:showAsAction="always" />
10+
11+
<item
12+
android:icon="@drawable/ic_delete_all"
13+
android:id="@+id/delete_all"
14+
android:title="Delete All Repo"
15+
app:showAsAction="always" />
1016

1117
</menu>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3-
<background android:drawable="@drawable/ic_launcher_background" />
4-
<foreground android:drawable="@drawable/ic_launcher_foreground" />
3+
<background android:drawable="@color/ic_launcher_background"/>
4+
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
55
</adaptive-icon>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
3-
<background android:drawable="@drawable/ic_launcher_background" />
4-
<foreground android:drawable="@drawable/ic_launcher_foreground" />
3+
<background android:drawable="@color/ic_launcher_background"/>
4+
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
55
</adaptive-icon>
-1.59 KB
Loading
Loading
Loading
-1.13 KB
Loading
Loading
-871 Bytes
Loading
-2.1 KB
Loading
Loading
Loading
-3.62 KB
Loading
Loading
Loading
-4.42 KB
Loading
Loading
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="ic_launcher_background">#FFFFFF</color>
4+
</resources>

0 commit comments

Comments
 (0)