GithubHelp home page GithubHelp logo

erolaksoy / compose-impression Goto Github PK

View Code? Open in Web Editor NEW
47.0 1.0 1.0 2.22 MB

A library provides a way to track impressions of items in a lazy list with Jetpack Compose.

License: Apache License 2.0

Kotlin 100.00%
android android-library compose impression jetpack-compose jetpack-compose-library jetpack-compose-tutorial lazycolumn lazyrow mvvm-android

compose-impression's Introduction

Compose Impression

This library provides a way to track whether items appear on the screen in lazy lists.

Lazy Column Lazy Row

Usage

To use the impression, first create an ImpressionState with rememberImpressionState. Then, use the impression modifier to track impressions for items in your list.

⚠️ The item key you use in the lazy list has to be the same as the key you give to the impression modifier.

    val lazyListState = rememberLazyListState()
    val impressionState = rememberImpressionState(lazyListState)

    LaunchedEffect(Unit) {
        impressionState.seenEvent.collectLatest {
            // collect impression events
        }
    }

    LazyRow(
        modifier = modifier.fillMaxSize(),
        state = lazyListState,
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        items(list, key = { it }) {
            CardItem(
                key = it,
                modifier = Modifier
                    .width(250.dp)
                    .height(120.dp)
                    .impression(it, impressionState),
            )
        }
    }

or

    val lazyListState = rememberLazyListState()
    val impressionState = rememberImpressionState(lazyListState)

    LazyRow(
        modifier = modifier.fillMaxSize(),
        state = lazyListState,
        contentPadding = PaddingValues(16.dp),
        horizontalArrangement = Arrangement.spacedBy(12.dp)
    ) {
        items(list, key = { it }) {
            CardItem(
                key = it,
                modifier = Modifier
                    .width(250.dp)
                    .height(120.dp)
                    .impression(
                        key = it,
                        impressionState = impressionState,
                        onImpressionHappened = {
                            // collect impression event
                        }
                    ),
            )
        }
    }

Validators

The VisibilityPercentImpressionValidator validates impressions based on the visibility percentage of an item.

val impressionState = rememberImpressionState(lazyListState) {
    addValidator(VisibilityPercentImpressionValidator(0.5f)) // set the visibility percentage threshold to 50%
}

With this configuration, when an item is 50% visible, the impression will be validated and happened.

You can create your own validator by extending from ImpressionValidator.

Including in your project

  1. Add the JitPack repository to your project-level build.gradle file:
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add the dependency to your app-level build.gradle file:
dependencies {
    implementation 'com.github.erolaksoy:compose-impression:0.1.1'
}
  1. Sync your project with Gradle files.

License

Copyright 2023 Erol Aksoy

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

compose-impression's People

Contributors

erolaksoy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

gorkemkarayel

compose-impression's Issues

I want to help track duration

Thanks for starting the project. It shows a good way to track an initial impression. I'd like to expand it by tracking the impression end event and save a timer. I'll see what I can do. If you have any ideas please let me know.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.