Skip to content

Instantly share code, notes, and snippets.

View ivanalvarado's full-sized avatar

Ivan Alvarado ivanalvarado

  • Tinder
  • Los Angeles, CA
  • 14:55 (UTC -08:00)
  • LinkedIn in/ivalvara
View GitHub Profile
@ivanalvarado
ivanalvarado / scroll-up.js
Created August 18, 2015 17:55
Smooth Scroll Back to Top Button
/* Place this code in your JS file */
jQuery(document).ready(function($){
// browser window scroll (in pixels) after which the "back to top" link is shown
var offset = 300,
//browser window scroll (in pixels) after which the "back to top" link opacity is reduced
offset_opacity = 1200,
//duration of the top scrolling animation (in ms)
scroll_top_duration = 3500,
//grab the "back to top" link
$back_to_top = $('.cd-top');
@ivanalvarado
ivanalvarado / UserListActivity.kt
Last active May 10, 2019 04:36
Correct implementation of Swipe-to-Refresh with LiveData.
class UserListActivity : AppCompatActivity() {
@Inject internal lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var userListViewModel: UserListViewModel
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@ivanalvarado
ivanalvarado / UserListViewModel.kt
Last active May 11, 2022 07:53
Correct implementation of Swipe-to-Refresh with LiveData.
class UserListViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() {
private val reloadTrigger = MutableLiveData<Boolean>()
private val users: LiveData<List<UserModel>> = Transformations.switchMap(reloadTrigger) {
userRepository.getUsers()
}
init {
refreshUsers()
}
@ivanalvarado
ivanalvarado / UserListActivity.kt
Last active May 10, 2019 04:14
Naive implementation of Swipe-to-Refresh with LiveData
// DON'T DO THIS
class UserListActivity : AppCompatActivity() {
@Inject internal lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var userListViewModel: UserListViewModel
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
@ivanalvarado
ivanalvarado / UserListViewModel.kt
Created May 10, 2019 04:11
Naive implementation of Swipe-to-Refresh with LiveData.
class UserListViewModel @Inject constructor(private val userRepository: UserRepository) : ViewModel() {
init {
}
// DON'T DO THIS
fun getUsers(): LiveData<List<UserModel>> {
return userRepository.getUsers()
}
@ivanalvarado
ivanalvarado / UserDetailViewModel.kt
Last active June 10, 2019 23:18
Pass argument through public setter method
class UserDetailViewModel @Inject constructor(
private val userRepository: UserRepository
) : ViewModel() {
private lateinit var userId: String
private val reloadTrigger = MutableLiveData<Boolean>()
private val userDetail: LiveData<UserDetailModel> = Transformations.switchMap(reloadTrigger) {
userRepository.getUserDetail(userId, reloadTrigger.value!!)
}
@ivanalvarado
ivanalvarado / UserDetailActivity.kt
Created June 10, 2019 23:20
Activity that passes argument to ViewModel via public setter method instead of Constructor.
const val ARGUMENT_USER_ID = "ARGUMENT_USER_ID"
class UserDetailActivity : AppCompatActivity() {
@Inject
internal lateinit var viewModelFactory: ViewModelProvider.Factory
private lateinit var userDetailViewModel: UserDetailViewModel
override fun onCreate(savedInstanceState: Bundle?) {
// ...
@ivanalvarado
ivanalvarado / build.gradle
Created June 10, 2019 23:25
AssistedInject gradle dependency
// module level gradle file
dependencies {
/* Dagger 2 - Assisted Inject */
compileOnly 'com.squareup.inject:assisted-inject-annotations-dagger2:0.3.3'
kapt 'com.squareup.inject:assisted-inject-processor-dagger2:0.3.3'
}
@ivanalvarado
ivanalvarado / UserDetailViewModel.kt
Created June 10, 2019 23:28
Passing arguments to ViewModel with AssistedInject.
class UserDetailViewModel @AssistedInject constructor(
private val userRepository: UserRepository,
@Assisted private val userId: Int
) : ViewModel() {
private val reloadTrigger = MutableLiveData<Boolean>()
private val userDetail: LiveData<UserDetailModel> = Transformations.switchMap(reloadTrigger) {
userRepository.getUserDetail(userId.toString(), reloadTrigger.value!!)
}
@Component(modules = [AssistedInjectModule::class])
@Singleton
interface ApplicationComponent {
// ...
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent