Stream์์ ๋ฐํํ๋ ์ด๋ฒคํธ๋ฅผ ์ ๋ฌ๋ฐ์ ๊ทธ์ ์ด๋ฒคํธ์ ๋ฐ๋ผ ํ๋์ ํ๊ธฐ ์ํด์ , ๊ตฌ๋ ์๋ฅผ ๊ด์ฐฐ ๋์์ ๋ฑ๋ก ํด์ผ ํฉ๋๋ค.
์ด๋ฅผ ์ํด RxJava์์ subscribe๋ผ๋ operator๊ฐ ์์ผ๋ฉฐ, ์ด๋ฅผ operator๋ฅผ ํตํด ๊ด์ฐฐ ๋์๊ณผ ๊ด์ฐฐ์๋ฅผ ์ฐ๊ฒฐ ํ ์ ์์ต๋๋ค. ( docs )
์์ ์ดํด๋ณธ Observer Pattern ํฌ์คํ ์์๋, Custom UI์ Observer interface๋ฅผ ๊ตฌํํ๋ ๋ฐฉ์์ผ๋ก Observer ๊ตฌํ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์์ต๋๋ค.
public interface RamsObserver<T> {
void notifyDataIsChanged(T value);
}
package com.example.rxjavalecture.observerpattern.oberserverpattern.customui
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.example.rxjavalecture.R
import com.example.rxjavalecture.observerpattern.oberserverpattern.RamsObserver
import com.example.rxjavalecture.observerpattern.oberserverpattern.ObserverPatternActivity
class PercentTextView : AppCompatTextView, RamsObserver<Int> {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init {
// ObserverPatternActivity์ ์๋ progressSubject์ ๊ตฌ๋
์ ์ฒญ
ObserverPatternActivity.progressSubject.subscribe(this)
}
// Percent ๊ฐ์ด ๋ณ๊ฒฝ ๋์์ ๋ ํ ํ๋์ ์ ์
override fun notifyDataIsArrived(value: Int?) {
value?.let {
text = String.format(context.getString(R.string.first_week_observer_pattern_result_format), it)
}
}
}
์ด๋ ๊ฒ Observer๋ฅผ ์ง์ ๋ง๋ค์ด๋ ๋์ง๋ง, ์์ ํฌ์คํ ( Stream ๊ตฌํ์ฒด์ ์ข ๋ฅ ( Observable / Flowable / Single ... ) )์์๋, Stream์ ๊ตฌํ์ฒด ๋ง๋ค ๋ฐํํ๋ ์ด๋ฒคํธ๊ฐ ๋ฌ๋ผ ๊ด์ฐฐ์์ ํ์ํ ํจ์๊ฐ ๋ค๋ฅผ ์ ์์์ ์ดํด ๋ณด์์ต๋๋ค.