Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Bintang Putera bintangputera

🏠
Working from home
View GitHub Profile
View DataPostsPagedListAdapter.kt
class DataPostPagedListAdapter :
PagedListAdapter<Posts, DataPostPagedListAdapter.DataPostViewHolder>(DIFF_CALLBACK) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): DataPostPagedListAdapter.DataPostViewHolder {
val binding = PostItemLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return DataPostViewHolder(binding)
}
View PostsFragment.kt
class PostsFragment : Fragment() {
private lateinit var dataPostPagedListAdapter: DataPostPagedListAdapter
private val postsViewmodel by lazy {
val factory = ViewModelFactory.getInstance()
ViewModelProvider(this, factory).get(PostsViewModel::class.java)
}
private var _bindingPostsFragment: FragmentPostsBinding? = null
View shimmer_placeholder_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/tx_title"
View fragment_posts.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.facebook.shimmer.ShimmerFrameLayout
View post_item_layout.xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
View activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
View MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
View ViewModelFactory.kt
@Suppress("UNCHECKED_CAST")
class ViewModelFactory(
private val postsRepository: PostsRepository,
private val compositeDisposable: CompositeDisposable
) : ViewModelProvider.NewInstanceFactory() {
companion object {
@Volatile
private var instance: ViewModelFactory? = null
fun getInstance(): ViewModelFactory =
View Injection.kt
object Injection {
fun providePostsRepository() : PostsRepository = PostsRepository.getInstance()
fun provideCompositeDisposable(): CompositeDisposable = CompositeDisposable()
}
View PostsDataSourceFactory.kt
class PostsDataSourceFactory(
private val apiService: PostsService,
private val compositeDisposable: CompositeDisposable
): DataSource.Factory<Int, Posts>(){
val postsLiveDataSource = MutableLiveData<PostsDataSource>()
override fun create(): DataSource<Int, Posts> {
val postsDataSource = PostsDataSource(apiService, compositeDisposable)