Skip to content

Commit

Permalink
Merge pull request #6 from TanishMoral11/main
Browse files Browse the repository at this point in the history
Added comments
  • Loading branch information
TanishMoral11 authored Aug 9, 2024
2 parents d37f08b + 629d7db commit 5eeda17
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 32 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

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

10 changes: 9 additions & 1 deletion .idea/material_theme_project_new.xml

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

11 changes: 0 additions & 11 deletions .idea/other.xml

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

4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:roundIcon="@drawable/logo"
android:supportsRtl="true"
android:theme="@style/Theme.Randommemes"
tools:targetApi="31">
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/com/example/randommemes/ApiInterface.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package com.example.randommemes
import retrofit2.Call
import retrofit2.http.GET

// Interface to define the API endpoints
interface ApiInterface {

// A GET request to the endpoint "gimme" which returns a Call object containing a response of type `responseDataclass`
@GET("gimme")
fun getData() : Call<responseDataclass>
}
fun getData(): Call<responseDataclass>
}
29 changes: 19 additions & 10 deletions app/src/main/java/com/example/randommemes/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,51 +15,60 @@ import retrofit2.Response

class MainActivity : AppCompatActivity() {

//https://meme-api.com/gimme
// Declaring a lateinit variable for View Binding
lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
enableEdgeToEdge()
setContentView(binding.root)

// Handling window insets for edge-to-edge display
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

// Fetch the first meme on activity creation
getData()

// Set an onClickListener to fetch a new meme when the button is clicked
binding.btnNewMeme.setOnClickListener {
getData()
}
}

// Function to fetch meme data using Retrofit
private fun getData() {

val progressDialog = ProgressDialog(this)
progressDialog.setMessage("Please wait...")
progressDialog.show()


// Making a network request using Retrofit
RetrofitInstance.apiInterface.getData().enqueue(object : Callback<responseDataclass?> {
override fun onResponse(
call: Call<responseDataclass?>,
response: Response<responseDataclass?>
) {

binding.memeAuthor.text = response.body()?.author
// Set the meme author and title text views
binding.memeAuthor.text = response.body()?.author
binding.memeTitle.text = response.body()?.title
Glide.with(this@MainActivity).load(response.body()?.url).into(binding.memeImage);
// Load the meme image using Glide
Glide.with(this@MainActivity)
.load(response.body()?.url)
.into(binding.memeImage)

// Dismiss the progress dialog
progressDialog.dismiss()
}

override fun onFailure(call: Call<responseDataclass?>, t: Throwable) {
Toast.makeText(this@MainActivity, "${t.localizedMessage}", Toast.LENGTH_SHORT)
.show()
// Show a toast message if the request fails
Toast.makeText(this@MainActivity, "${t.localizedMessage}", Toast.LENGTH_SHORT).show()
progressDialog.dismiss() // Dismiss the progress dialog on failure as well
}


})
}
}
13 changes: 7 additions & 6 deletions app/src/main/java/com/example/randommemes/RetrofitInstance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import retrofit2.converter.gson.GsonConverterFactory

object RetrofitInstance {

//lazy meaning that it is not initialized until it is accessed for the first time
// Lazy initialization of Retrofit instance. It's created when accessed for the first time
private val retrofit by lazy {
Retrofit.Builder().baseUrl("https://meme-api.com/")
.addConverterFactory(GsonConverterFactory.create())
Retrofit.Builder()
.baseUrl("https://meme-api.com/") // Base URL for the API
.addConverterFactory(GsonConverterFactory.create()) // Convert JSON data to Kotlin objects using Gson
.build()
}

val apiInterface by lazy{
// Lazy initialization of the ApiInterface
val apiInterface by lazy {
retrofit.create(ApiInterface::class.java)
}

}
}
Binary file added app/src/main/res/drawable/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5eeda17

Please sign in to comment.