Git Product home page Git Product logo

coroutinetoyproject's Introduction

Coroutine Toy Project

  • coroutine을 이용하여 비동기 프로그래밍을 이해하고, 직접 코딩해보며 coroutine scope 에 대한 이해도 등을 올리기 위해 진행한 Toy Project
  • Firebase 와의 통신, 화면갱신 사이에 Coroutine 을 사용하여 비동기를 적용
  • 간단한 흐름 파악이 목적이기에 Dispathers.Main 수준만 사용
  • listener를 활용
  • CustomView, Ttablayout & Viewpager, FireStore, MVP Architecture

Coroutine

  • GlobalScope.launch(Dispatchers.Main) 의 사용 예시

In CustomFloatingActionButtonUI

  • 해당 버튼 click 시 anim() - animation 효과를 위해 비동기 사용
    fun handlingButtons() {
        GlobalScope.launch(Dispatchers.Main) {
            with(menuButton) {
                anim()
            }
        }
    }

In TodoPresenter & TodoModel & FireStoreAccessor

  • recycler view 를 초기화 하기 전 FireStore 로 부터 해당하는 데이터를 받아오기까지 join()await() 를 이용하여 대기하게 한뒤, 해당 Data가 다 받아오게 되면 그것을 화면에 다시 뿌려주는 부분에서의 비동기 사용
//TodoPresenter
override fun initialize() {
        GlobalScope.launch(Dispatchers.Main) {
            TodoRecyclerViewAdapter().let { adapter ->
                adapterView = adapter
                adapterView.itemListener = todoListItemListener
                view.setAdapter(adapter)
            }
            GlobalScope.launch(Dispatchers.Main) {
                datas = TodoModel.readTodoFromFireStore()!!
            }.join()
            adapterView.initItems(datas)
            adapterView.notifyAdapter()
        }
}

//TodoModel
suspend fun readTodoFromFireStore(): MutableList<TodoDTO>? {
        return FireStoreAccessor.readTodo()
}

//FireStoreAccessor   
suspend fun readTodo(): MutableList<TodoDTO> {
        val datas = mutableListOf<TodoDTO>()

        fireStore.collection(TODO)
            .whereEqualTo(type, TODO)
            .get()
            .addOnSuccessListener { items ->
                for (item in items) {
                    val postId = item.data[postId].toString()
                    val title = item.data[title].toString()
                    val content = item.data[content].toString()
                    val date = item.data[date].toString()
                    datas.add(
                        TodoDTO(
                            postId,
                            title,
                            content,
                            date,
                            TODO
                        )
                    )
                }
            }
            .await()

        return datas   
}

ScreenShoot

coroutinetoyproject's People

Contributors

jjjoonngg 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.