Skip to content

Instantly share code, notes, and snippets.

View malwinder-s's full-sized avatar
💭
Enjoying Coding

Malwinder Singh malwinder-s

💭
Enjoying Coding
View GitHub Profile
@malwinder-s
malwinder-s / GenericRecyclerViewAdapter.kt
Last active June 5, 2020 15:03
Generic Recycler View Adapter
package com.malwinder.kotlindemo.recyclerview
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
@malwinder-s
malwinder-s / ModelPreferencesManager.kt
Last active May 24, 2020 04:00
Saving object of the model class in Preferences
package com.malwinder.kotlindemo.preference
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import com.google.gson.GsonBuilder
/**
* Singleton class for managing preferences for POJO or model class's object.
* @author Malwinder Singh
@malwinder-s
malwinder-s / StudentListActivity.kt
Last active April 4, 2018 14:35
Example of code with boilerplate removed
private fun launchStudentDetailsActivity(id: String, name: String, totalScore: Int) {
val intent = Intent(this, StudentDetailsActivity::class.java)
intent.replaceExtras(getExtras(id, name, totalScore))
startActivity(intent)
}
private fun launchStudentEditorActivity(id: String, name: String, totalScore: Int) {
val intent = Intent(this, StudentEditorActivity::class.java)
intent.replaceExtras(getExtras(id, name, totalScore))
startActivity(intent)
@malwinder-s
malwinder-s / StudentListActivity.kt
Last active April 3, 2018 21:38
Example of non boilerplate code
private fun launch(id: String, name: String, totalScore: Int, className: Class) {
val intent = Intent(this, className)
intent.replaceExtras(getExtras(id, name, totalScore))
startActivity(intent)
}
private fun getExtras(id: String, name: String, totalScore: Int): Bundle {
val extras = Bundle()
extras.putString(EXTRA_STUDENT_ID, id)
extras.putString(EXTRA_STUDENT_NAME, name)
@malwinder-s
malwinder-s / StudentListActivity.kt
Last active April 4, 2018 14:36
Example of Boiler plate code.
private fun launchStudentDetailsActivity(id: String, name: String, totalScore : Int) {
val intent = Intent(this, StudentDetailsActivity::class.java)
intent.putExtra(EXTRA_STUDENT_ID, id)
intent.putExtra(EXTRA_STUDENT_NAME, name)
//Suppose "totalScore" is score of student in 5 subjects.
val totalPercentage = totalScore / 5
intent.putExtra(EXTRA_STUDENT_PERCENTAGE, totalPercentage)
startActivity(intent)