Skip to content

Commit

Permalink
Added Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishMoral11 committed Aug 9, 2024
1 parent 2014c25 commit 3bcec8a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 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.

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
}


})
}
}

0 comments on commit 3bcec8a

Please sign in to comment.