Skip to content

Commit

Permalink
Added Volley to Singleton
Browse files Browse the repository at this point in the history
  • Loading branch information
BudhirajaMadhav committed May 29, 2021
1 parent 330dc34 commit 506f569
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies {

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/androidmadhav/litmemes/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class MainActivity : AppCompatActivity() {


private fun loadMeme(){
val queue = Volley.newRequestQueue(this)
val url = "https://meme-api.herokuapp.com/gimme"
progressBar.visibility = View.VISIBLE
val jsonObjectRequest = JsonObjectRequest(
Expand Down Expand Up @@ -79,7 +78,8 @@ class MainActivity : AppCompatActivity() {
) { error ->
Toast.makeText(this, "Image Loading Failed! Please try after sometime.", Toast.LENGTH_SHORT).show()
}
queue.add(jsonObjectRequest)

MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)


}
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/java/com/androidmadhav/litmemes/MySIngleton.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.androidmadhav.litmemes

import android.content.Context
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.toolbox.Volley

class MySingleton constructor(context: Context) {
companion object {
@Volatile
private var INSTANCE: MySingleton? = null
fun getInstance(context: Context) =
INSTANCE ?: synchronized(this) {
INSTANCE ?: MySingleton(context).also {
INSTANCE = it
}
}
}
private val requestQueue: RequestQueue by lazy {
// applicationContext is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
Volley.newRequestQueue(context.applicationContext)
}
fun <T> addToRequestQueue(req: Request<T>) {
requestQueue.add(req)
}
}

0 comments on commit 506f569

Please sign in to comment.