Coder Social home page Coder Social logo

abanoubmilad / nut Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 324 KB

A clean quick to use RXJava, Retrofit and View model

License: MIT License

Kotlin 100.00%
android module rxjava-android rxjava koltin retrofit2-rxjava retorfit2 view viewmodel livedata

nut's Introduction

Nut

A clean quick to use RXJava, Retrofit and View model

MIT License - Copyright (c) 2020 Abanoub Milad Nassief Hanna
[email protected]
@Linkedin
@Github

Nut (Ancient Egyptian: Nwt), also known by various other transcriptions, is the goddess of the sky, stars, cosmos, mothers, astronomy, and the universe in the ancient Egyptian religion. She was seen as a star-covered woman arching over the Earth.

Features

  • implemented in MVVM
  • supports RxJava
  • easily handles network calls, network failure or model parsing errors
  • easily fire parallel network calls
  • clean structure that avoids view model leakage
  • open source
  • available on jitpack
  • easy to integrate and extend

How to add

Add to project level build.gradle

allprojects {
    repositories {

        maven { url "https://jitpack.io" }
        
    }
}

Add to app level build.gradle

    dependencies {

        implementation 'com.github.abanoubmilad:nut:0.4'
        
    }

How to use

   /*
    *
    *   make your view model extends the BaseViewModel
    *
    */

class BookSearchViewModel : BaseViewModel {

   /*
    *
    *   use makeNetworkRequest to consume a single provided by the retrofit interface
    *   network call are handled by background threads
    *   callbacks run on UI thread
    *
    *
    */

    fun searchVolumes(keyword: String?, author: String?) {
        makeNetworkRequest(bookRepository.searchVolumes(keyword, author), {
            volumesResponseLiveData.postValue(it)

        }, {
            AppLogger.e("NetworkError", it.code.toString())
            AppLogger.e(
                "NetworkError",
                it.parseAs<ErrorResponse>()?.error?.message.orEmpty()
            )
        })
    }

   /*
    *
    *   use makeNetworkRequest providing three singles to consume each single provided by the retrofit interface
    *   in parallel
    *   network call are handled by background threads
    *   callbacks run on UI thread
    *
    *
    */

    fun searchVolumesThreeParallel(
        keyword: String?,
        author: String?
    ) {


        makeNetworkRequestsParallel(
            bookRepository.searchVolumes(keyword, author), {
                volumesResponseLiveData.postValue(it)

            }, {

                AppLogger.e("NetworkError", "first request" + it.code.toString())
                AppLogger.e(
                    "NetworkError",
                    it.parseAs<ErrorResponse>()?.error?.message.orEmpty()
                )

            }, bookRepository.searchVolumes(keyword, author), {
                volumesResponseLiveData.postValue(it)

            }, {
                AppLogger.e("NetworkError", "second request" + it.code.toString())
                AppLogger.e(
                    "NetworkError",
                    it.parseAs<ErrorResponse>()?.error?.message.orEmpty()
                )

            },
            bookRepository.searchVolumes(keyword, author), {
                volumesResponseLiveData.postValue(it)

            }, {
                AppLogger.e("NetworkError", "third request" + it.code.toString())
                AppLogger.e(
                    "NetworkError",
                    it.parseAs<ErrorResponse>()?.error?.message.orEmpty()
                )

            }, {
                AppLogger.e("NetworkError", it.code.toString())
                AppLogger.e(
                    "NetworkError",
                    it.parseAs<ErrorResponse>()?.error?.message.orEmpty()
                )
            })


    }
   /*
    *
    *   you can use Isync inside your Application class, service or work manager
    *   simply implement the Isync interface
    *   you need to provide the CompositeDisposable
    *   and calling disposeIsync() when your componenet's onDestroy() is called
    *
    */

@AndroidEntryPoint
class FirebaseMsgService : FirebaseMessagingService(), Isync{
    @Inject
    lateinit var authRepo: AuthRepo

    override val disposable = CompositeDisposable()

    override fun onNewToken(token: String) {
        super.onNewToken(token)
        makeNetworkRequest(authRepo.updateNotificationToken(token), {
          }, {
          })

    }

    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)
    }


    override fun onDestroy() {
        super.onDestroy()
        disposeIsync()
    }

}

}

nut's People

Contributors

abanoubmilad avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.