전체 글

공부, 일상
Android

Codelab으로 LiveData transform해보기 - 3. 정리(完)

Transforming LiveData LiveData에 transformation을 하고 싶다면 Transformations 클래스의 helper 메소드들을 사용한다. Transformations.map 메소드는 LiveData에 데이터를 변경하고 다른 LiveData 객체를 리턴해준다. Displaying the result of a transformation in a TextView 원본 데이터가 ViewModel의 LiveData로 정의되어 있는지 확인한다. 변수를 정의한다. 어떤 변수가 있다면 Transformations.map 메소드로 transformation을 수행하고 그 값을 변수에 리턴해준다. val newResult = Transformations.map(someLiveData) { ..

Android

Codelab으로 LiveData transform해보기 - 2. Add transformation for the LiveData

Transformations.map 메소드는 원본 LiveData의 데이터를 수정하고 결과 LiveData 객체를 리턴하는 방법을 제공한다. 이 transformations은 리턴된 옵저버가 LiveData 객체를 observe하고 있지 않으면 계산되지 않는다. - 주의사항 : Transformations.map 메소드에서 실행되는 람다식은 메인 스레드에서 실행되기 때문에 오래 실행되는 task를 포함하면 안된다. 이번에는 경과 시간의 LiveData 객체를 "MM:SS" 형식의 새 문자열 LiveData 객체 형태로 만든다. game_fragment.xml 레이아웃 파일은 이미 타이머 텍스트뷰를 가지고 있다. 지금까지 텍스트뷰에 보여줄 텍스트가 없었으므로 타이머 텍스트가 보이지 않았다. 1. GameV..

Android

Codelab으로 LiveData transform해보기 - 1. Add a Timer

이번에는 이전에 수정한 GuessTheWord 앱에 카운트다운을 위한 타이머를 추가한다. 단어 리스트가 비어있으면 게임이 끝나는 대신 타이머가 끝날 때 게임이 끝나도록 할 것이다. 이 때 사용될 유틸리티 클래스를 안드로이드에서는 제공하는데 CountDownTimer가 있다. 기기 설정 변경 도중 타이머가 destroy되지 않도록 ViewModel에 해당 로직을 추가한다. 프래그먼트는 타이머가 작동할 때마다 텍스트뷰를 업데이트하는 코드를 포함하고 있다. GameViewModel 클래스에 아래 과정을 따라 구현한다. 1. 타이머 상수를 갖고 있기 위해 companion object를 만든다. companion object { // Time when the game is over private const va..

BonusTrack02.dev
BonusTrack02.dev