Skip to content

Commit c35d0a7

Browse files
committed
ViewModel and repository added
1 parent 67a7799 commit c35d0a7

File tree

13 files changed

+384
-192
lines changed

13 files changed

+384
-192
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
id 'com.android.application'
33
id 'kotlin-android'
4+
id 'kotlin-kapt'
45
}
56

67
android {
@@ -30,6 +31,10 @@ android {
3031
kotlinOptions {
3132
jvmTarget = '1.8'
3233
}
34+
35+
buildFeatures{
36+
dataBinding true
37+
}
3338
}
3439

3540
dependencies {
@@ -63,6 +68,13 @@ dependencies {
6368
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
6469
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
6570

71+
// Room components
72+
implementation "androidx.room:room-runtime:2.3.0"
73+
kapt "androidx.room:room-compiler:2.3.0"
74+
implementation "androidx.room:room-ktx:2.3.0"
75+
androidTestImplementation "androidx.room:room-testing:2.3.0"
76+
77+
6678
//Coil Image Loader
6779
implementation "io.coil-kt:coil:1.1.1"
6880
}

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
android:roundIcon="@mipmap/ic_launcher_round"
1414
android:supportsRtl="true"
1515
android:theme="@style/Theme.GithubBrowser">
16-
<activity android:name=".MainActivity">
16+
<activity android:name=".MainActivity"
17+
android:windowSoftInputMode="adjustResize">
1718
<intent-filter>
1819
<action android:name="android.intent.action.MAIN" />
1920

app/src/main/java/com/prateekcode/githubbrowser/MainActivity.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ package com.prateekcode.githubbrowser
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import androidx.fragment.app.Fragment
6+
import com.prateekcode.githubbrowser.fragment.HomeFragment
57

68
class MainActivity : AppCompatActivity() {
79
override fun onCreate(savedInstanceState: Bundle?) {
810
super.onCreate(savedInstanceState)
911
setContentView(R.layout.activity_main)
12+
13+
setCurrentFragment(HomeFragment(), "HomeFragment")
1014
}
15+
16+
private fun setCurrentFragment(fragment: Fragment, tag: String) =
17+
supportFragmentManager.beginTransaction().replace(R.id.fragment_container, fragment, tag).commit()
1118
}
Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,68 @@
11
package com.prateekcode.githubbrowser.fragment
22

33
import android.os.Bundle
4-
import androidx.fragment.app.Fragment
4+
import android.util.Log
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8+
import android.widget.EditText
9+
import android.widget.Toast
10+
import androidx.databinding.DataBindingUtil
11+
import androidx.fragment.app.Fragment
12+
import androidx.lifecycle.ViewModelProvider
813
import com.prateekcode.githubbrowser.R
14+
import com.prateekcode.githubbrowser.databinding.FragmentAddRepoBinding
15+
import com.prateekcode.githubbrowser.viewmodel.ApiViewModel
16+
import com.prateekcode.githubbrowser.viewmodel.ApiViewModelFactory
917

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"
1418

15-
/**
16-
* A simple [Fragment] subclass.
17-
* Use the [AddRepoFragment.newInstance] factory method to
18-
* create an instance of this fragment.
19-
*/
2019
class AddRepoFragment : 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-
}
20+
21+
lateinit var binding: FragmentAddRepoBinding
22+
var viewModel: ApiViewModel? =null
3223

3324
override fun onCreateView(
3425
inflater: LayoutInflater, container: ViewGroup?,
3526
savedInstanceState: Bundle?
3627
): View? {
37-
// Inflate the layout for this fragment
38-
return inflater.inflate(R.layout.fragment_add_repo, container, false)
28+
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_add_repo, container, false)
29+
30+
binding.addMaterialToolbar.setNavigationOnClickListener {
31+
fragmentManager!!.popBackStack()
32+
}
33+
34+
binding.addRepositoryBtn.setOnClickListener {
35+
if (isEmptyTextInput(binding.ownerEditText) || isEmptyTextInput(binding.repoEditText)){
36+
binding.ownerEditText.error ="Enter Username/Organization"
37+
binding.repoEditText.error = "Enter Repo Name"
38+
Toast.makeText(context, "Enter correct details", Toast.LENGTH_SHORT).show()
39+
}else{
40+
//Toast.makeText(context, "Repo added", Toast.LENGTH_SHORT).show()
41+
findTheRepo(binding.ownerEditText.text.toString(), binding.repoEditText.text.toString())
42+
}
43+
}
44+
45+
return binding.root
3946
}
4047

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 AddRepoFragment.
49-
*/
50-
// TODO: Rename and change types and number of parameters
51-
@JvmStatic
52-
fun newInstance(param1: String, param2: String) =
53-
AddRepoFragment().apply {
54-
arguments = Bundle().apply {
55-
putString(ARG_PARAM1, param1)
56-
putString(ARG_PARAM2, param2)
57-
}
48+
private fun isEmptyTextInput(editText: EditText): Boolean {
49+
return editText.text.toString().trim().isEmpty()
50+
}
51+
52+
private fun findTheRepo(userName:String, repoName:String){
53+
val factory = ApiViewModelFactory()
54+
viewModel = ViewModelProvider(this, factory).get(ApiViewModel::class.java)
55+
viewModel!!.githubRepository(userName, repoName)
56+
viewModel!!.repoResponse.observe(viewLifecycleOwner, { response ->
57+
if (response.isSuccessful) {
58+
Log.d(TAG, "Name of the user: ${response.body()!!.name}")
59+
Log.d(TAG, "Description of the user: ${response.body()!!.description}")
60+
Log.d(TAG, "Html Url of the user: ${response.body()!!.html_url}")
5861
}
62+
})
63+
}
64+
65+
companion object{
66+
const val TAG = "REPO_FRAGMENT"
5967
}
6068
}
Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,51 @@
11
package com.prateekcode.githubbrowser.fragment
22

33
import android.os.Bundle
4+
import android.view.*
45
import androidx.fragment.app.Fragment
5-
import android.view.LayoutInflater
6-
import android.view.View
7-
import android.view.ViewGroup
6+
import android.widget.Toast
7+
import androidx.databinding.DataBindingUtil
88
import com.prateekcode.githubbrowser.R
9+
import com.prateekcode.githubbrowser.databinding.FragmentHomeBinding
910

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"
1411

15-
/**
16-
* A simple [Fragment] subclass.
17-
* Use the [HomeFragment.newInstance] factory method to
18-
* create an instance of this fragment.
19-
*/
2012
class HomeFragment : 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-
}
13+
14+
lateinit var binding: FragmentHomeBinding
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_home, container, false)
39-
}
20+
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false)
21+
22+
binding.addRepoButton.setOnClickListener {
23+
//Toast.makeText(context, "Hey Buddy!!!!!!", Toast.LENGTH_SHORT).show()
24+
replaceFragment(AddRepoFragment())
25+
}
26+
4027

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 HomeFragment.
49-
*/
50-
// TODO: Rename and change types and number of parameters
51-
@JvmStatic
52-
fun newInstance(param1: String, param2: String) =
53-
HomeFragment().apply {
54-
arguments = Bundle().apply {
55-
putString(ARG_PARAM1, param1)
56-
putString(ARG_PARAM2, param2)
28+
binding.homeMaterialToolbar.setOnMenuItemClickListener {
29+
menuItem ->
30+
when (menuItem.itemId) {
31+
R.id.add_repo_menu_btn -> {
32+
replaceFragment(AddRepoFragment())
33+
true
5734
}
35+
else -> false
5836
}
37+
}
38+
39+
40+
return binding.root
5941
}
42+
43+
44+
private fun replaceFragment(fragment: Fragment) {
45+
val transaction = activity!!.supportFragmentManager.beginTransaction()
46+
transaction.replace(R.id.fragment_container, fragment)
47+
transaction.addToBackStack(null)
48+
transaction.commit()
49+
}
50+
6051
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.prateekcode.githubbrowser.repository
2+
3+
import com.prateekcode.githubbrowser.api.RetrofitInstance
4+
import com.prateekcode.githubbrowser.model.branch.Branch
5+
import com.prateekcode.githubbrowser.model.commit.Commits
6+
import com.prateekcode.githubbrowser.model.issuedir.IssueCollection
7+
import com.prateekcode.githubbrowser.model.repo.Repo
8+
import retrofit2.Response
9+
10+
class Repository {
11+
12+
suspend fun getRepo(
13+
ownerName: String,
14+
repoName: String
15+
): Response<Repo> {
16+
return RetrofitInstance.api.getRepository(ownerName, repoName)
17+
}
18+
19+
suspend fun getBranches(
20+
ownerName: String,
21+
repoName: String
22+
): Response<Branch> {
23+
return RetrofitInstance.api.getBranches(ownerName, repoName)
24+
}
25+
26+
suspend fun getCommitMessage(
27+
ownerName: String,
28+
repoName: String,
29+
shaKey: String
30+
): Response<Commits> {
31+
return RetrofitInstance.api.getCommitMessage(ownerName, repoName, shaKey)
32+
}
33+
34+
suspend fun getOpenIssue(
35+
ownerName: String,
36+
repoName: String,
37+
state: String
38+
): Response<List<IssueCollection>> {
39+
return RetrofitInstance.api.getOpenIssues(ownerName, repoName, state)
40+
}
41+
42+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.prateekcode.githubbrowser.util
2+
3+
import android.R
4+
import android.app.Activity
5+
import android.content.Context
6+
import android.content.Intent
7+
import android.content.SharedPreferences
8+
import android.net.ConnectivityManager
9+
import android.net.Uri
10+
import android.view.inputmethod.InputMethodManager
11+
import androidx.fragment.app.FragmentManager
12+
import androidx.fragment.app.FragmentTransaction
13+
import java.text.SimpleDateFormat
14+
import java.util.*
15+
16+
17+
object Utils {
18+
fun hideKeyboard(activity: Activity) {
19+
val view = activity.currentFocus
20+
if (view != null) {
21+
val iMM = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
22+
iMM.hideSoftInputFromWindow(view.windowToken, 0)
23+
}
24+
}
25+
26+
fun getSharedPreferences(context: Context): SharedPreferences {
27+
return context.getSharedPreferences(
28+
context.applicationContext.packageName,
29+
Context.MODE_PRIVATE
30+
)
31+
}
32+
33+
fun formatDateTime(epoch: Long): String {
34+
val date = Date(epoch)
35+
val sdf = SimpleDateFormat("h:mm a", Locale.ENGLISH)
36+
sdf.timeZone = TimeZone.getDefault()
37+
return sdf.format(date)
38+
}
39+
40+
fun clearSharedPreferences(context: Context) {
41+
getSharedPreferences(context).edit().clear().apply()
42+
}
43+
44+
fun startActivity(context: Context, activity: Class<*>) {
45+
val intent = Intent(context, activity)
46+
context.startActivity(intent)
47+
}
48+
49+
fun goToURL(context: Context, url: String) {
50+
val uri = Uri.parse(url)
51+
val intent = Intent(Intent.ACTION_VIEW, uri)
52+
context.startActivity(intent)
53+
}
54+
55+
fun isConnected(context: Context): Boolean {
56+
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
57+
return cm.activeNetworkInfo != null && cm.activeNetworkInfo!!.isConnected
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)