<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oğuz Şahin</title>
    <description>The latest articles on DEV Community by Oğuz Şahin (@oguzsahin).</description>
    <link>https://dev.to/oguzsahin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F870898%2Facd42142-f85a-40ca-83aa-ffc8a1da4eaa.jpeg</url>
      <title>DEV Community: Oğuz Şahin</title>
      <link>https://dev.to/oguzsahin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oguzsahin"/>
    <language>en</language>
    <item>
      <title>What Is Paging3 (MVVM, Flow, DataBinding, Hilt)?</title>
      <dc:creator>Oğuz Şahin</dc:creator>
      <pubDate>Wed, 01 Jun 2022 11:19:32 +0000</pubDate>
      <link>https://dev.to/oguzsahin/what-is-paging3-mvvm-flow-databinding-hilt-gbf</link>
      <guid>https://dev.to/oguzsahin/what-is-paging3-mvvm-flow-databinding-hilt-gbf</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WrsuewEA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw8w92ip0kk6qmle4q6z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WrsuewEA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sw8w92ip0kk6qmle4q6z.png" alt="developer.android.com" width="875" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Undoubtedly, Recyclerview is one of the most used components while developing an android app. We use Recylerview to show lists of data on one or more pages of applications. When the size of these datasets increases, we need to pay attention to the effective use of system resources and the smooth progress of UI performance. In this article, we will learn the effective and correct way to display large datasets in recylerview. The topics we will learn in the content of the article will be as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is pagination and why do we use it?&lt;/li&gt;
&lt;li&gt;What is Paging3 and what advantages does it offer?&lt;/li&gt;
&lt;li&gt;Understanding and implementing Paging3&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;What is pagination and why do we use it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets’s consider apps like Instagram, Twitter or Facebook. On the main stream pages, they show huge, even endless data on the list. Instead of loading this big data in one go,when the user scrolls to see more of the content displayed on the screen; they do it by loading the new data into the list. Here is the answer to what is pagination actually this logic. The process of loading this dataset in chunks, instead of loading the entire dataset at once, when displaying large datasets on a list.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So why do we need such a process or what does this method do for us?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Efficient use of your application’s network bandwidth and system resources&lt;/li&gt;
&lt;li&gt;Get data on the page faster&lt;/li&gt;
&lt;li&gt;Less memory usage&lt;/li&gt;
&lt;li&gt;Not consuming resources for useless data&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Understanding and implementing Paging3?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Paging3 is a jetpack library that allows us to easily load large datasets from the data source (local, remote, file..etc).It loads data gradually, reducing network and system resources usage. It is written in Kotlin and works in coordination with other Jetpack libraries. It supports Flow, LiveData and RxJava along with Kotlin Coroutine. It also provides support for many functions that you need to implement manually when you need to load data:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keeps track of the keys to be used for retrieving the next and previous page.&lt;/li&gt;
&lt;li&gt;Automatically requests the correct page when the user has scrolled to the end of the list.&lt;/li&gt;
&lt;li&gt;Ensures that multiple requests aren’t triggered at the same time.&lt;/li&gt;
&lt;li&gt;Allows you to cache data: if you’re using Kotlin, this is done in a CoroutineScope; if you're using Java, this can be done with LiveData.&lt;/li&gt;
&lt;li&gt;Tracks loading state and allows you to display it in a RecyclerView list item or elsewhere in your UI, and easily retry failed loads.&lt;/li&gt;
&lt;li&gt;Allows you to execute common operations like map or filter on the list that will be displayed, independently of whether you're using Flow, LiveData, or RxJava Flowable or Observable.&lt;/li&gt;
&lt;li&gt;Provides an easy way of implementing list separators.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Understanding and implementing Paging3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I created a sample repo for this episode. This repo gets user information from a &lt;strong&gt;&lt;a href="https://randomuser.me"&gt;free API that creates random users&lt;/a&gt;&lt;/strong&gt;. Through this repo, we will both get to know the components of Paging3 and learn how to use it.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;1. Adding Dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s start by implementing our necessary dependencies first. Paging 3 also has support for rxjava, guava and jetpack compose.&lt;br&gt;
If you wish, you can add your dependencies from the &lt;strong&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/paging/v3-overview#setup"&gt;link&lt;/a&gt;&lt;/strong&gt; according to your needs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dependencies {

    .....

    // OkHttp interceptor
    implementation "com.squareup.okhttp3:logging-interceptor:$okHttp_interceptor_version"

    // Retrofit
    implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
    implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"

    //Paging
    implementation "androidx.paging:paging-runtime-ktx:$paging_version"

    //Hilt
    implementation "com.google.dagger:hilt-android:$hilt_version"
    kapt "com.google.dagger:hilt-android-compiler:$hilt_version"

    //Glide
    implementation "com.github.bumptech.glide:glide:$glide_version"
    annotationProcessor "com.github.bumptech.glide:compiler:$glide_version"

    //Coroutines
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_core_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_android_version"

    //ktx
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_ktx_version"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_ktx_version"
    implementation "androidx.activity:activity-ktx:$activity_ktx_version"

    }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Network Operations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s start by retrofit process to get user information from the remote server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {

    private const val CONNECT_TIMEOUT = 20L
    private const val READ_TIMEOUT = 60L
    private const val WRITE_TIMEOUT = 120L


    @Provides
    @Singleton
    fun provideOkHttpClient(@ApplicationContext context: Context): OkHttpClient {
        val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
            level = if (BuildConfig.DEBUG) HttpLoggingInterceptor.Level.BODY
            else HttpLoggingInterceptor.Level.NONE
        }
        return OkHttpClient.Builder()
            .addInterceptor(httpLoggingInterceptor)
            .addInterceptor(NetworkConnectionInterceptor(context))
            .connectTimeout(CONNECT_TIMEOUT, SECONDS)
            .readTimeout(READ_TIMEOUT, SECONDS)
            .writeTimeout(WRITE_TIMEOUT, SECONDS)
            .build()
    }

    @Provides
    @Singleton
    fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
        return Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okHttpClient)
            .build()
    }


    @Provides
    @Singleton
    fun provideUserService(retrofit: Retrofit): UserService {
        return retrofit.create(UserService::class.java)
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s create the Retrofit and Okhttp classes as a singleton.&lt;br&gt;
By using the dependency injection method with Hilt, we will provide the required class dependencies in a SOLID way. Next, let’s define our service class and model classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface UserService {

    @GET(".")
    suspend fun getUsers(
        @Query("page") page: Int,
        @Query("results") results: Int,
    ): UserResponse

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here it is useful to understand the two parameters required by the api.&lt;br&gt;
It specifies the page number requested in each request and the number of items to be loaded at once. Logically, we will load the new page when the user reaches the end of each list, so here we will pull the new data according to the page number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data class UserResponse(
    val results: ArrayList&amp;lt;UserModel&amp;gt;
)

data class UserModel(
    val name: NameModel,
    val email: String,
    val phone: String,
    val picture: PictureModel
)

data class NameModel(
    val title: String,
    val first: String,
    val last: String
)

data class PictureModel(
    val thumbnail: String
)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3.PagingSource&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let’s start by preparing our PagingSource class for the process of pulling the data after we deal with the network side where we will get the user information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UserPagingDataSource(private val userService: UserService) :
    PagingSource&amp;lt;Int, UserModel&amp;gt;() {

    override suspend fun load(params: LoadParams&amp;lt;Int&amp;gt;): LoadResult&amp;lt;Int, UserModel&amp;gt; {
        val page = params.key ?: STARTING_PAGE_INDEX
        return try {
            val response = userService.getUsers(page, params.loadSize)
            LoadResult.Page(
                data = response.results,
                prevKey = if (page == STARTING_PAGE_INDEX) null else page.minus(1),
                nextKey = if (response.results.isEmpty()) null else page.plus(1)
            )
        } catch (exception: Exception) {
            return LoadResult.Error(exception)
        }
    }


    override fun getRefreshKey(state: PagingState&amp;lt;Int, UserModel&amp;gt;): Int? {
        return state.anchorPosition?.let { anchorPosition -&amp;gt;
            state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
                ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
        }
    }

    companion object {
        private const val STARTING_PAGE_INDEX = 1
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PagingSource class is a component that comes with Paging3. This class is an abstract generic class that is responsible for the source of the paginated data and how to retrieve data from that source. In this article, we use the PagingSource class because we only provide data from a single source (remote , local, file ..etc). If we were to work in a structure that is both remote and local based (where we mean the process of writing the data in the remote to locale and going through a single source), we would have to use RemoteMediator.&lt;br&gt;
Since we will not mention the concept of RemoteMediator in this article, you can access the necessary information from the &lt;strong&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/paging/v3-network-db"&gt;link&lt;/a&gt;&lt;/strong&gt; if you wish.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can use RxPagingSource with rxJava or ListenableFuturePagingSource for guava.&lt;/p&gt;

&lt;p&gt;The PaginSource class has two parameter types as generic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;key : **As a key, our API service is index-based, so we will specify it as Int.&lt;br&gt;
**value:&lt;/strong&gt; You need to give it as the type of data you will upload. For our example, we can specify it as UserModel.&lt;/p&gt;

&lt;p&gt;The Load function is the main function responsible for loading the data to us. This function, as soon as the user reaches the end of the list, takes the next key and sends the request for the new list asynchronously, and this is done automatically by the Paging library. In addition, it is a suspend function and provides a nice structure for us to make network requests in the background.&lt;/p&gt;

&lt;p&gt;It takes the LoadParams class named params as a parameter, which holds the page number to be loaded and the number of items to be loaded. From here, we will be able to easily submit our request with the correct page and the number of items to be loaded.During the first load, the key will be null(If you do not specify an inital value). In this case, we also specified STARTING_PAGE_INDEX . By default, Paging3 will load LOADSIZE*3 items during the first load. This way the user will see enough items when the list is loaded for the first time and it won’t trigger too many network requests unless the user scrolls through the page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UFiDLDpD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb72nm9184tgvnxy3b81.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UFiDLDpD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb72nm9184tgvnxy3b81.png" alt="official guide" width="372" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We also need to specify LoadResult as the return type. This class is a sealed class that holds the state of our request. If the request is successful, we can return the data as LoadResult.Page, and for subsequent requests, we can return the object by specifying the prevKey and nextKey. If there is a problem, we can return an error with LoadRestlt.Error.&lt;/p&gt;

&lt;p&gt;The getRefreshKey abstract method that we need to override. The refresh key is used for subsequent refresh calls to PagingSource.load() (the first call is initial load which uses initialKey provided by Pager). A refresh happens whenever the Paging library wants to load new data to replace the current list, e.g., on swipe to refresh or on invalidation due to database updates, config changes, process death, etc. Typically, subsequent refresh calls will want to restart loading data centered around PagingState.anchorPosition which represents the most recently accessed index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.Repository&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Singleton
class UserRepositoryImpl @Inject constructor(
    private val userService: UserService
) : UserRepository {
    override fun getUsers(): Flow&amp;lt;PagingData&amp;lt;UserModel&amp;gt;&amp;gt; {
        return Pager(
            config = PagingConfig(
                pageSize = NETWORK_PAGE_SIZE
            ),
            pagingSourceFactory = { UserPagingDataSource(userService) }
        ).flow
    }


    companion object {
        const val NETWORK_PAGE_SIZE = 20
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After writing the PagingSource class that will obtain the data, we need a Pager that will provide the data here as a flow. Data returned from PagingSource returns with the PagingData container. We need to specify to the Pager class from which source and how the data will be retrieved. It expects 3 parameters from us:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ESfH_Avz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndjhoi7z2szyp9dms8xz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ESfH_Avz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndjhoi7z2szyp9dms8xz.PNG" alt="Image description" width="866" height="255"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;config&lt;/strong&gt; : PagingConfigclass sets options regarding how to load content from a PagingSource such as how far ahead to load, the size request for the initial load, and others. The only mandatory parameter you have to define is the page size—how many items should be loaded in each page. By default, Paging will keep in memory all the pages you load. To ensure that you're not wasting memory as the user scrolls, set the maxSize parameter in PagingConfig. By default Paging will return null items as a placeholder for content that is not yet loaded if Paging can count the unloaded items and if the enablePlaceholders config flag is true.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;initalKey&lt;/strong&gt;: You can give an initial key for the first request to be made when the PagingSource is initial. In our example, since we do not specify an inital value, it will be defined as null. But on the PagingSource side, we handle the null state and give it the initial value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;pagingSourceFactory&lt;/strong&gt;: A function that defines how to create the PagingSource. In our case, we’ll create a new UserPagingDataSource.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, we can say .flow to provide the PagedData flow of the Pager class. Thus, as soon as the user reaches the end of the list, PagingSource will automatically and asynchronously send the request for the next page, and then the paginatedData will be presented to us as a flow each time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; we have 4 type to pass the PagingData to other layers of our app:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kotlin Flow - use Pager.flow.&lt;/li&gt;
&lt;li&gt;LiveData - use Pager.liveData.&lt;/li&gt;
&lt;li&gt;RxJava Flowable - use Pager.flowable.&lt;/li&gt;
&lt;li&gt;RxJava Observable - use Pager.observable.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5.ViewModel&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@HiltViewModel
class UserViewModel @Inject constructor(userRepository: UserRepository) : ViewModel() {
    val userItemsUiStates = userRepository.getUsers()
        .map { pagingData -&amp;gt;
            pagingData.map { userModel -&amp;gt; UserItemUiState(userModel) }
        }.cachedIn(viewModelScope)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After successfully creating the side that will get the data, now let’s move on to the ui side.First, let’s request the users data from the repository on the viewModel side. The returned structure will be a PagingData and let’s map it to a required model class in every item we will use in the recylerview. Since we return to Flow, the Paging library provides us flexibility here as well, and we can perform operations such as filtering, mapping ..etc.&lt;/p&gt;

&lt;p&gt;The cachedIn() operator makes the data stream shareable and caches the loaded data with the provided CoroutineScope. In any configuration change, it will provide the existing data instead of getting the data from scratch. It will also prevent memory leak.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.Adapter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the components that Paging3 offers us is PagingDataAdapter. This Adapter is a special inherited class from RecyclerView.Adapter to show Paging Data based on it. It has a structure very similar to ListAdapter and adds it to the recylerview by calculating the new incoming list asynchronously on the background side with DiffUtil.ItemCallback in the most optimized way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class UsersAdapter @Inject constructor() :
    PagingDataAdapter&amp;lt;UserItemUiState, UserViewHolder&amp;gt;(Comparator) {

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        getItem(position)?.let { userItemUiState -&amp;gt; holder.bind(userItemUiState) }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {

        val binding = inflate&amp;lt;ItemUserBinding&amp;gt;(
            LayoutInflater.from(parent.context),
            R.layout.item_user,
            parent,
            false
        )

        return UserViewHolder(binding)
    }

    object Comparator : DiffUtil.ItemCallback&amp;lt;UserItemUiState&amp;gt;() {
        override fun areItemsTheSame(oldItem: UserItemUiState, newItem: UserItemUiState): Boolean {
            return oldItem.getPhone() == newItem.getPhone()
        }

        override fun areContentsTheSame(
            oldItem: UserItemUiState,
            newItem: UserItemUiState
        ): Boolean {
            return oldItem == newItem
        }
    }

}

class UserViewHolder(private val binding: ItemUserBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bind(userItemUiState: UserItemUiState) {
        binding.executeWithAction {
            this.userItemUiState = userItemUiState
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;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"&amp;gt;

    &amp;lt;data&amp;gt;

        &amp;lt;variable
            name="userItemUiState"
            type="com.huawei.pagingexampleproject.ui.UserItemUiState" /&amp;gt;
    &amp;lt;/data&amp;gt;

    &amp;lt;com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="4dp"&amp;gt;


        &amp;lt;androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"&amp;gt;

            &amp;lt;com.google.android.material.imageview.ShapeableImageView
                android:id="@+id/ivPhoto"
                imageUrl="@{userItemUiState.imageUrl}"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="8dp"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="@+id/glStart"
                app:layout_constraintTop_toTopOf="@+id/glTop"
                app:shapeAppearanceOverlay="@style/circle"
                app:srcCompat="@drawable/ic_launcher_background" /&amp;gt;

            &amp;lt;com.google.android.material.textview.MaterialTextView
                android:id="@+id/tvName"
                style="@style/user_card_text_style"
                android:text="@{userItemUiState.name}"
                app:layout_constraintBottom_toTopOf="@+id/tvMail"
                app:layout_constraintEnd_toEndOf="@id/glEnd"
                app:layout_constraintStart_toEndOf="@id/ivPhoto"
                app:layout_constraintTop_toTopOf="@+id/ivPhoto"
                tools:text="Jhon Doe" /&amp;gt;

            &amp;lt;com.google.android.material.textview.MaterialTextView
                android:id="@+id/tvMail"
                style="@style/user_card_text_style"
                android:text="@{userItemUiState.mail}"
                app:layout_constraintBottom_toTopOf="@+id/tvPhone"
                app:layout_constraintEnd_toEndOf="@id/glEnd"
                app:layout_constraintStart_toEndOf="@id/ivPhoto"
                app:layout_constraintTop_toBottomOf="@+id/tvName"
                tools:text="jon.doe@gmail.com" /&amp;gt;

            &amp;lt;com.google.android.material.textview.MaterialTextView
                android:id="@+id/tvPhone"
                style="@style/user_card_text_style"
                android:text="@{userItemUiState.phone}"
                app:layout_constraintBottom_toBottomOf="@+id/ivPhoto"
                app:layout_constraintEnd_toEndOf="@id/glEnd"
                app:layout_constraintStart_toEndOf="@id/ivPhoto"
                app:layout_constraintTop_toBottomOf="@+id/tvMail"
                tools:text="0532 123 12 12" /&amp;gt;


            &amp;lt;androidx.constraintlayout.widget.Guideline
                android:id="@+id/glStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_begin="8dp" /&amp;gt;


            &amp;lt;androidx.constraintlayout.widget.Guideline
                android:id="@+id/glEnd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_end="8dp" /&amp;gt;

            &amp;lt;androidx.constraintlayout.widget.Guideline
                android:id="@+id/glTop"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_begin="8dp" /&amp;gt;

        &amp;lt;/androidx.constraintlayout.widget.ConstraintLayout&amp;gt;

    &amp;lt;/com.google.android.material.card.MaterialCardView&amp;gt;
&amp;lt;/layout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data class UserItemUiState(private val userModel: UserModel) : BaseUiState() {

    fun getImageUrl() = userModel.picture.thumbnail

    fun getName() = "${userModel.name.first} ${userModel.name.last}"

    fun getPhone() = userModel.phone

    fun getMail() = userModel.email

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun &amp;lt;T : ViewDataBinding&amp;gt; T.executeWithAction(action: T.() -&amp;gt; Unit) {
    action()
    executePendingBindings()
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we move to the Adapter part, we mapped our UserModel data coming to the viewmodel side as UserItemUiState for each item in the recylerview. Therefore, we specify the type of PagingData that will come to the Adapter as UserItemUiState. Comprator object for changes too. The executeWithAction function on the ViewHolder side is also an extension function defined to not write executePendingBindings every time. It’s essentially assigning variables for dataBinding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Render Data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After preparing the adapter, it is time to send the incoming data to the adapter and complete the UI rendering.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@AndroidEntryPoint
class UserActivity : AppCompatActivity() {
    private lateinit var binding: ActivityUserBinding
    private val viewModel: UserViewModel by viewModels()

    @Inject
    lateinit var userAdapter: UsersAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setBinding()
        setListener()
        setAdapter()
        collectLast(viewModel.userItemsUiStates, ::setUsers)
    }

    private fun setBinding() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_user)
    }

    private fun setListener() {
        binding.btnRetry.setOnClickListener { userAdapter.retry() }
    }


    private fun setAdapter() {
        collect(flow = userAdapter.loadStateFlow
            .distinctUntilChangedBy { it.source.refresh }
            .map { it.refresh },
            action = ::setUsersUiState
        )
        binding.rvUsers.adapter = userAdapter.withLoadStateFooter(FooterAdapter(userAdapter::retry))
    }

    private fun setUsersUiState(loadState: LoadState) {
        binding.executeWithAction {
            usersUiState = UsersUiState(loadState)
        }
    }

    private suspend fun setUsers(userItemsPagingData: PagingData&amp;lt;UserItemUiState&amp;gt;) {
        userAdapter.submitData(userItemsPagingData)
    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
fun &amp;lt;T&amp;gt; LifecycleOwner.collectLast(flow: Flow&amp;lt;T&amp;gt;, action: suspend (T) -&amp;gt; Unit) {
    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            flow.collectLatest(action)
        }
    }
}


fun &amp;lt;T&amp;gt; LifecycleOwner.collect(flow: Flow&amp;lt;T&amp;gt;, action: suspend (T) -&amp;gt; Unit) {
    lifecycleScope.launch {
        repeatOnLifecycle(Lifecycle.State.STARTED) {
            flow.collect {
                action.invoke(it)
            }
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First of all, we need to collect the flow that will come from the viewmodel. We have defined LifecycleOwner extensions to be lifecycle aware and reusable. You can get information about the concepts here for flow from this &lt;a href="https://kotlinlang.org/docs/flow.html"&gt;link&lt;/a&gt; and for repeatOnLifecyle from the &lt;a href="https://medium.com/androiddevelopers/repeatonlifecycle-api-design-story-8670d1a7d333"&gt;link&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to handle the loading status of the data on the UI side, Paging library tracks the state of load requests for paged data and exposes it through the LoadState class. Your app can register a listener with the PagingDataAdapter to receive information about the current state and update the UI accordingly. These states are provided from the adapter because they are synchronous with the UI. This means that your listener receives updates when the page load has been applied to the UI.&lt;/p&gt;

&lt;p&gt;LoadState can be in 3 states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;LoadState.NotLoading:&lt;/strong&gt; If there is no active load operation and no error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LoadState.Loading:&lt;/strong&gt; If there is an active load operation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LoadState.Error:&lt;/strong&gt; If there is an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The loadStateFlow or addLoadStateListener() provided via PagingDataAdapter can be used to be aware of the loading status. These mechanisms provide access to a CombinedLoadStates object that includes information about the LoadState behavior for each load type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;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"&amp;gt;

    &amp;lt;data&amp;gt;

        &amp;lt;variable
            name="usersUiState"
            type="com.huawei.pagingexampleproject.ui.UsersUiState" /&amp;gt;

    &amp;lt;/data&amp;gt;

    &amp;lt;androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.UserActivity"&amp;gt;

        &amp;lt;com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topAppbar"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:title="@string/user_list" /&amp;gt;

        &amp;lt;androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvUsers"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="@{usersUiState.listVisibility}"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/topAppbar"
            tools:listitem="@layout/item_user" /&amp;gt;

        &amp;lt;ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{usersUiState.progressBarVisibility}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/topAppbar" /&amp;gt;


        &amp;lt;Button
            android:id="@+id/btnRetry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/retry"
            android:visibility="@{usersUiState.errorVisibility}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/topAppbar" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/tvError"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@{usersUiState.getErrorMessage(context)}"
            android:visibility="@{usersUiState.errorVisibility}"
            app:layout_constraintEnd_toEndOf="@+id/btnRetry"
            app:layout_constraintStart_toStartOf="@+id/btnRetry"
            app:layout_constraintTop_toBottomOf="@+id/btnRetry"
            tools:text="Internet Connection Failed" /&amp;gt;

    &amp;lt;/androidx.constraintlayout.widget.ConstraintLayout&amp;gt;
&amp;lt;/layout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data class UsersUiState(
    private val loadState: LoadState
) : BaseUiState() {

    fun getProgressBarVisibility() = getViewVisibility(isVisible = loadState is LoadState.Loading)

    fun getListVisibility() = getViewVisibility(isVisible = loadState is LoadState.NotLoading)

    fun getErrorVisibility() = getViewVisibility(isVisible = loadState is LoadState.Error)

    fun getErrorMessage(context: Context) = if (loadState is LoadState.Error) {
        loadState.error.localizedMessage ?: context.getString(R.string.something_went_wrong)
    } else ""
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s handle the loading, success or error situations by observing these changes in the ui part. First, we can use the source parameter that CombinedState gives us to listen to the paging source status. So we can handle the loading state when the first request is thrown. Let’s set the state we are listening through PagingDataAdapter with databinding over UsersUiState model class.&lt;/p&gt;

&lt;p&gt;Another adapter component offered by the paging library is LoadStateAdapter. This adapter provides access to the current list’s loading state. When the user reaches the end of the list by using a custom ViewHolder, let’s take action according to the loading status. Here we can add it as a header or footer. We can add both together. You can find detailed information from the &lt;strong&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/paging/load-state#header-footer"&gt;link&lt;/a&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class FooterAdapter(
    private val retry: () -&amp;gt; Unit
) : LoadStateAdapter&amp;lt;FooterViewHolder&amp;gt;() {
    override fun onBindViewHolder(holder: FooterViewHolder, loadState: LoadState) {
        holder.bind(loadState)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        loadState: LoadState
    ): FooterViewHolder {
        val itemPagingFooterBinding = inflate&amp;lt;ItemPagingFooterBinding&amp;gt;(
            LayoutInflater.from(parent.context),
            R.layout.item_paging_footer,
            parent,
            false
        )
        return FooterViewHolder(itemPagingFooterBinding, retry)
    }

}


class FooterViewHolder(
    private val binding: ItemPagingFooterBinding,
    retry: () -&amp;gt; Unit
) : RecyclerView.ViewHolder(binding.root) {

    init {
        binding.btnRetry.setOnClickListener { retry.invoke() }
    }

    fun bind(loadState: LoadState) {
        binding.executeWithAction {
            footerUiState = FooterUiState(loadState)
        }
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data class FooterUiState(private val loadState: LoadState) : BaseUiState() {

    fun getLoadingVisibility() = getViewVisibility(isVisible = loadState is LoadState.Loading)

    fun getErrorVisibility() = getViewVisibility(isVisible = loadState is LoadState.Error)

    fun getErrorMessage(context: Context) = if (loadState is LoadState.Error) {
        loadState.error.localizedMessage ?: context.getString(R.string.something_went_wrong)
    } else ""
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="utf-8"?&amp;gt;
&amp;lt;layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"&amp;gt;

    &amp;lt;data&amp;gt;

        &amp;lt;variable
            name="footerUiState"
            type="com.huawei.pagingexampleproject.common.FooterUiState" /&amp;gt;
    &amp;lt;/data&amp;gt;

    &amp;lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dp"&amp;gt;

        &amp;lt;ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="@{footerUiState.loadingVisibility}" /&amp;gt;

        &amp;lt;Button
            android:id="@+id/btnRetry"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/retry"
            android:visibility="@{footerUiState.errorVisibility}" /&amp;gt;

        &amp;lt;TextView
            android:id="@+id/tvError"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="4dp"
            android:text="@{footerUiState.getErrorMessage(context)}"
            android:visibility="@{footerUiState.errorVisibility}"
            tools:text="Internet Connection Failed" /&amp;gt;

    &amp;lt;/LinearLayout&amp;gt;
&amp;lt;/layout&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After preparing this structure, we can set our recylerview adapter using the withLoadStateFooter() function.In fact, this structure returns us a ConcatAdapter. It notifies the LoadStateAdapter of the status of adding to the list via the PagingDataAdapter, and we can show the loading status of the newly added paginatedData when the user reaches the end of the list in a custom way via the FooterAdapter we have created. In addition, with the ConcatAdapter structure, we show multiviewtype on the recylerview.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CM9WdrCp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9vl2v5j73syye06sgfh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CM9WdrCp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9vl2v5j73syye06sgfh.PNG" alt="Image description" width="853" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;withLoadStateFooter expects a parameter of type LoadStateAdapter from us. Here we give the FooterAdapter that we created ourselves.FooterAdapter also expects retry function in constructure. This structure is also provided through the pagingDataAdapter. If there is an error in loading when the end of the page is reached, the retry button will appear with the error message. It is enough to call the retry function over the pagingDataAdapater we created so that we can send the page request to be loaded again. This is again provided to us by the paging3 library.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, I tried to explain the pagination method used when working with large data sets and Paging3, which is offered to android developers to easily implement this method. With the advantages it provides, Paging3 provides us with great convenience and enables us to show large data sets to the user in the most optimized way. The components of the Paging library are designed to fit into the &lt;strong&gt;&lt;a href="https://developer.android.com/topic/architecture"&gt;recommended Android app architecture&lt;/a&gt;&lt;/strong&gt;, integrate cleanly with other &lt;strong&gt;&lt;a href="https://developer.android.com/jetpack"&gt;Jetpack components&lt;/a&gt;&lt;/strong&gt;, and provide first-class Kotlin support. See you in the next article. 👋👋&lt;/p&gt;




&lt;p&gt;To review the repo we wrote:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/oguz-sahin/PagingExampleProject"&gt;https://github.com/oguz-sahin/PagingExampleProject&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/paging/v3-overview"&gt;https://developer.android.com/topic/libraries/architecture/paging/v3-overview&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.android.com/codelabs/android-paging#"&gt;https://developer.android.com/codelabs/android-paging#&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>paging3</category>
      <category>mvvm</category>
    </item>
    <item>
      <title>All About Kotlin Inline(Value) Class</title>
      <dc:creator>Oğuz Şahin</dc:creator>
      <pubDate>Wed, 01 Jun 2022 10:51:34 +0000</pubDate>
      <link>https://dev.to/oguzsahin/all-about-kotlin-inlinevalue-class-26nc</link>
      <guid>https://dev.to/oguzsahin/all-about-kotlin-inlinevalue-class-26nc</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KLkW3H1L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ib11t3gfzxirv0d924e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KLkW3H1L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ib11t3gfzxirv0d924e.jpg" alt="Image description" width="875" height="492"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hello, All ✋. In this article, we will try to learn everything about Kotlin Inline Class in detail.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What Is The Inline Class❓&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In some cases, we create wrappers to provide certain business logic. As such, these additional classes also cause a performance loss when they create additional loads during runtime. If it is a primitive type you wrap, the usage here can become terrible in terms of performance. To solve this situation, the kotlin team introduced a new class named Inline Class.&lt;/p&gt;

&lt;p&gt;In this case, the inline class not only provides type safety by carrying a class feature but also acts as a primitive type on the side of the compiled code and saves unnecessary performance loss. It also offers a much cleaner, understandable and readable code structure.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Why is there an extra performance loss?&lt;/strong&gt; 🤔&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WDj0jLom--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2dujumbi074h3rgnrax.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WDj0jLom--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2dujumbi074h3rgnrax.png" alt="from https://www.javatpoint.com/stack-vs-heap-java" width="550" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we create a primitive type variable, it is stored in the stack of the JVM memory, but when we create a new object, it is stored in a region called a heap. A more expensive operation is going around the back when storing and using objects. You can check the link to go into more detail about this heap and stack event. The losses we are talking about are immeasurably small. But if we consider the job of creating an object created in a whole code, it is obvious that it will create an extra overhead. In addition, as programmers, should not forget that we need to write the code more cleanly, clearly, and with performance. But when a comparison is made, defining a primitive type is more performance efficient than defining an object. For this reason, if we are writing a wrapper class for a primitive type, by choosing the Inline class, we will not only benefit from the benefits of the wrapper class such as more flexible, readable, and type safety, but also we will not experience performance loss by acting as a primitive type on the compiled code side of the inline class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2eKW1uUY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y9djvi97wz3ol7vxtr2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2eKW1uUY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7y9djvi97wz3ol7vxtr2.png" alt="from https://quickbirdstudios.com/blog/kotlin-value-classes/" width="875" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In addition, there is a comparison of how long the process will cost us when the same process is done in 4 different ways.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Usage and Limits&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4I4tjdeR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dcfkei8cbbvo6san86t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4I4tjdeR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dcfkei8cbbvo6san86t.png" alt="Inline Class Definition" width="708" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔹 The value keyword is sufficient to define an inline class.&lt;/p&gt;

&lt;p&gt;🔹 If you are writing a kotlin code for the JVM, we also need to add the @JvmInline annotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KG_pL-Y1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzxuykht2difq1k8nvma.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KG_pL-Y1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzxuykht2difq1k8nvma.png" alt="Image description" width="875" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔹 We can use a single primary constructor.&lt;/p&gt;

&lt;p&gt;🔹 It is mandatory to define a property such as data class.&lt;/p&gt;

&lt;p&gt;🔹 We can define only one read-only property in the primary constructor (var is not accepted. Only Val keyword)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EKIRnNvQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n07xo1qpjvqvt78phe22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EKIRnNvQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n07xo1qpjvqvt78phe22.png" alt="Image description" width="875" height="690"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔹 We can define member read-only properties and their values ​​can be from singletons, top-level objects, or constants (not allowed in backing file).&lt;/p&gt;

&lt;p&gt;🔹 The init block can be used here as well as in a regular class.&lt;/p&gt;

&lt;p&gt;🔹 Member functions can be defined.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--El044qfb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i10ch9b9j1a2s497yvca.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--El044qfb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i10ch9b9j1a2s497yvca.png" alt="Image description" width="875" height="686"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔹 They cannot inherit from any class or any class cannot inherit from an inline class.&lt;/p&gt;

&lt;p&gt;🔹 They can implement an interface.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Mangling&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WISDNt8z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5z0redyiwgzj9fl2yi6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WISDNt8z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p5z0redyiwgzj9fl2yi6.png" alt="Mangling" width="837" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are two functions in the above attachment. One of them takes a parameter of type value class, and the other takes a parameter of type int. There is a little change in the code on the side where this code is compiled. As you can see, the value class is first compiled as a primitive type. This can lead to certain errors. For example, consider the two functions above. Here, there will be a conflict as the distance type will be int type on the JVM side when compiling. To avoid these errors, the function name is shuffled when compiling the inline class. When you look at the compiled code on the right, it can be seen that it is compiled as getPrice.&lt;/p&gt;

&lt;p&gt;If we want to call the first function from java, we need to disable the mangling event in it. We can do this by adding the “@JvmName” annotation. By adding this annotation, the name argument we give will be the function name in the compiled code. We can also call from java classes over this function name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_lZGTb0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twwhf8kzgvsd9j5lpo76.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_lZGTb0b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/twwhf8kzgvsd9j5lpo76.png" alt="Image description" width="837" height="327"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Type Alias vs Inline Class&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can say that the use of inline class is very similar to the use of type alias. But there are also some differences. Although both seem to create a new type, what we do with type alias is to replace the name of the type. For example, we can think of assigning a different name for the String type. In the use of Inline class, we create a new type. This provides us with a clearer and more understandable code structure, as well as type safety.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nVkbL16s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1kc9m5hpp0773sp1v6y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nVkbL16s--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w1kc9m5hpp0773sp1v6y.png" alt="Image description" width="753" height="626"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, we tried to learn every detail about the Inline class. The use of inline class promises us good things in terms of writing the code more cleanly and legibly, with the advantage it provides in terms of performance and the type safety it provides. &lt;br&gt;
See you in the next articles 👋👋.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;References :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://kotlinlang.org/docs/inline-classes.html"&gt;https://kotlinlang.org/docs/inline-classes.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.geeksforgeeks.org/kotlin-inline-classes/"&gt;https://www.geeksforgeeks.org/kotlin-inline-classes/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://typealias.com/guides/introduction-to-inline-classes/"&gt;https://typealias.com/guides/introduction-to-inline-classes/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://quickbirdstudios.com/blog/kotlin-value-classes/"&gt;https://quickbirdstudios.com/blog/kotlin-value-classes&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>inline</category>
      <category>class</category>
    </item>
  </channel>
</rss>
