æ¬è¨äºã¯
ã¢ãã¤ã«ã¢ããªWeek
2æ¥ç®ã®è¨äºã§ãã
ð²
1æ¥ç®
â¶â¶ æ¬è¨äº â¶â¶
3æ¥ç®
ð²
ã¯ããã«
ãããã³ã ã®ããã°ãåºæ¥ã¦ããã¯ããã¦ã®ããã°å·çã«ãªãã¾ããã¯ã©ã¦ãé¨ã®è¥¿æã§ãï¼
ç§ã¯å»å¹´ã®å¤ããå³ãå·¦ãåãããªãã¾ã¾Androidéçºã«æºããããã«ãªããåæã®é ã¯MVVMã«å¯¾ãã¦ãèªåã®ä½ã£ã¦ãããã®ãMVVMã¨ãããã®ãæ¡ç¨ãã¦ãããããï¼ããããã®ããªãã¼ãããã¨ããèªèããããã¾ããã§ããã
ã§ããAndroidã¨ã³ã¸ãã¢ã¨ãã¦ã®1å¹´ç®ãããããçµãããããªä»ãMVVMãããç¨åº¦ç解ããå ãã¦ç解度ã«æ¯ä¾ãã¦éçºå¹çãä¸ãã£ããªãã¨ããå®æãããã®ã§ãä»åæ´çã¨åå¿ã®ããã«ããã°ã«ãã¾ãã
MVVM ã¨ã¯
MVVMã¨ã¯ãModel - View - ViewModel ããæ§æãããã¢ã¼ããã¯ãã£ãã¿ã¼ã³ã§ãåè¦ç´ ã¯ä»¥ä¸ã®ãããªå½¹å²ãæ
ã£ã¦ãã¾ãã
Model: ãã¸ãã¹ãã¸ãã¯æ å½
View: UIæ å½
ViewModel: View ã¨Modelã®æ©æ¸¡ãæ å½
ãã®ããã«åè¦ç´ ã§é¢å¿ãåé¢ããåä¸æ¹åã®ä¾åé¢ä¿ãä¿ã¡ãããã«ãã¼ã¿ããã¼ãã·ã³ãã«ã«ãªãããæ´ãããã¨ã§ãã©ãã«ä½ã®å¦çãæ¸ãã¦ãããã追ããããéçºå¹çãä¸ãããã¨ãã§ããã®ãMVVMã®æ¨å³ã§ãã
以ä¸ã§ã¯åè¦ç´ éã®ããåããåãåãã¦è¦ã¦ããã¾ãã
Viewã¨ViewModel
Viewã¨ViewModelã¯ãããã以ä¸ã®ãããªå½¹å²ãæ ãã¾ãã
Viewã®å½¹å²
- UIã®æç»
- UIããåãåã£ãã¦ã¼ã¶ã¼æä½ãViewModelã«ä¼ãã
- ViewModelãä¿æãã¦ããUIã®æç»ã«å¿ è¦ãªãã¼ã¿ãç£è¦ãã
ViewModelã®å½¹å²
- UIã®æç»ã«å¿ è¦ãªãã¼ã¿ã®ä¿æ
- ä¸è¨ã®æºå(ãã¼ã¿ã®å å·¥)ã¨æ´æ°ã
ããã§UIã®æç»ã«å¿ è¦ãªãã¼ã¿ã¨å¼ãã§ãããã®ã¯è¦ããã«ç»é¢ã®ç¶æ ã®ãã¨ã§ã表示ãããããã¹ããå¯è¦ä¸å¯è¦ãªã©ãUIã«åæ ããããç¶æ ã§ãã¼ã¿ã¨ãã¦æã£ããã®ã§ãã
MVVMã§ã¯View â ViewModelã®åä¸æ¹åã®ä¾åé¢ä¿ãä¿ã¡ããã®ã§ãViewModelã§ä¿æãããã¼ã¿ãå ã«Viewã§UIã®æç»ããããã¨ãã«ãViewModelããViewãæä½ããã®ã¯NGã§ãã
ã§ã¯ã©ã®ããã«ãã¼ã¿ã®ããåããããã®ãã¨ããã¨ãViewModelã«ä¿æãã¦ããUIæç»ç¨ã®ãã¼ã¿ãViewããåç §ãã¦UIãæç»ãã¾ãã ããã¦ããããAndroidã¢ããªã§å®ç¾ããããã®æããªã¼ã½ããã¯ã¹ãªæ¹æ³ããDataBinding + LiveDataã§ãã
DataBinding
DataBindingã¯ã¬ã¤ã¢ã¦ããã¡ã¤ã«ã«UIã®æç»ç¨ã®ãã¼ã¿ããã¤ã³ãã£ã³ã°ããAndroid Jetpackã®ã©ã¤ãã©ãªã§ãã
ãã¼ã¿ ãã¤ã³ãã£ã³ã° ã©ã¤ãã©ãª | Android ãããããã¼ | Android Developers
ã¬ã¤ã¢ã¦ããã¡ã¤ã«ãDataBindingç¨ã®å½¢ã§æ¸ãã¦ããã°ãããã«åããã¦ãã¼ã¿ããã¤ã³ãããããã®ã¯ã©ã¹ãèªåã§çæãã¦ãããã®ã§ã ãã¨ã¯UIã®æç»ã«å¿ è¦ãªãã¼ã¿ã渡ãã¦ããã°ã¬ã¤ã¢ã¦ãã«åæã«åæ ãã¦ããã¾ãã
ããã®ä½ãè¯ããã¨è¨ãã¨ãDataBindingã使ç¨ããªãå ´åã¯Viewè¦ç´ (TextViewãªã©)ã«ãããããidãæ¯ã£ã¦ãããidãå ã«findViewByIdã§Viewè¦ç´ ãç¹å®ãã¦ã表示ããããã¹ããæå®ããããªã¹ãã¼ãã¤ãããããã®ããããã話ã§ãããDataBindingã使ãã°ãã®éç¨ãå ¨ã¦çç¥ãããã¨ãã§ãã¾ãã
ãã¡ãããã®éç¨ãçç¥ãããã¨ã§ã³ã¼ããæ¸ãã®ã§ãActivityãFragmentã¨ãã£ãViewã«è©²å½ããã¯ã©ã¹ã®ãã¸ãã¯ãã·ã³ãã«ã«ä¿ã¤ãã¨ãã§ããåè¿°ããUIã®æç»ãªã©ã®Viewã®é¢å¿äºã«å°å¿µãããã¨ãã§ããã®ãæ¨å³ã§ãã
LiveData
DataBindingã使ãã°ã·ã³ãã«ã«ãã¼ã¿ãUIã«åæ ã§ããããã«ãªãã¾ãããUIã«åæ ããããã¼ã¿ãæ´æ°ãããã¨ãã«ã©ãããã°è¯ãã®ããæ°ã«ãªãã¾ããããã§ä½¿ãã®ãLiveDataã§ãã
LiveDataã¯Android Jetpackãæä¾ãããã¼ã¿ãã«ãã¼ã¯ã©ã¹ã§ãLiveDataã§ä¿æãããã¼ã¿ã¯ãViewã®ã©ã¤ããµã¤ã¯ã«ãæèããªããç£è¦ãè¡ããã¨ãã§ãã¾ãã
LiveData ã®æ¦è¦ | Android ãããããã¼ | Android Developers
LiveDataã§ä¿æãããã¼ã¿ã«ã¯ãã¼ã¿ãç£è¦ãã¦å¤æ´ãæ¤ç¥ããã¡ã½ãã(observer)ããããããã«lifecycleOwnerã渡ãã¦ããã¨Viewå´ã®ã¯ã©ã¹ã®ã©ã¤ããµã¤ã¯ã«ãçãã¦ããéã¯LiveDataã§ä¿æãããã¼ã¿ã®ç£è¦ã¨å¤æ´ã®æ¤ç¥ããã¦ãããã®ã§ãå¤æ´ãæ¤ç¥ããã¿ã¤ãã³ã°ã§UIæç»ç¨ã®ãã¼ã¿ãDataBindingã§ãã¤ã³ããç´ãã°UIãæ´æ°ããã¾ãã
ActivityãFragmentãçãã¦ããªãéã¯å¤æ´ã®æ¤ç¥ãããªãã®ã§ããã¼ã¿ããã¤ã³ãããåã«Activity/Fragmentãçãã¦ãããã©ãããä¸ã ãã§ãã¯ããå¿ è¦ããªããã©ã¤ããµã¤ã¯ã«ç®¡çã楽ã«ãªãã¾ãã
(è£è¶³)lifecycleOwner
Viewãæ ãã¯ã©ã¹ã®ActivityãFragmentã¯lifecycleOwnerãæã£ã¦ãããlifecycleOwnerãViewModelã«æ¸¡ããã¨ã§Viewã®ã©ã¤ããµã¤ã¯ã«ã¤ãã³ãã«å¿ããå¦çãViewModelã§ãå®è¡ã§ããããã«ãªãã¾ãã
UIã®æç»ã«å¿ è¦ãªãã¼ã¿ãViewModelã§æã£ã¦ããã¨ä½ãè¯ããï¼
ViewModelã®ã©ã¤ããµã¤ã¯ã«ã¯ActivityãFragmentãããé·ããUIã®æç»ã«å¿ è¦ãªãã¼ã¿ãViewModeã«æã£ã¦ãããã¨ã§ãç»é¢å転ã§Activity/Fragmentãç ´æ£ãããå ´åã§ãããã«UIã®åæç»ãå¯è½ã§ãã
éã«ãViewModelã§ã¯ãªãActivityãFragmentã§ãã¼ã¿ãä¿æãã¦ããå ´åããAPIãå©ãã¦ãã¼ã¿ãåã£ã¦ãã¦ããããããUIç¨ã«ãã¼ã¿ãå¤æãã¦ããããã¨ãããããªãã¯ããã¦UIãæç»ããã®ã«å¿ è¦ãªãã¹ã¦ã®å¦çãå度å®è¡ããå¿ è¦ãåºã¦ãã¾ãã
ã©ããã¦ãViewModelããViewãæä½ãããå ´å
ViewModel - Modelã®éã®å¦çã«å¿ãã¦ãViewModelããViewã«å¯¾ãã¦ä½ãããã®æç»æ示ãã©ããã¦ãåºãããå ´åããããã¨æãã¾ãã
ä¾ãã°ã¨ã©ã¼çºçæã«ã¨ã©ã¼ãã¤ã¢ãã°ãåºããããä½ãããã®å¦çãå®äºããã¿ã¤ãã³ã°ã§ã¹ããã¯ãã¼ãåºãããããå ´åã§ãã
View â ViewModelã«å¯¾ããã¡ãã»ã¼ã¸ã³ã°ãå®ç¾ãããå ´åãLiveDataãç¨ãã¦ViewModelããã¤ãã³ããéç¥(ãã¦ãã風ã«)ãããã¨ãã§ãã¾ãã
èªåã§å®è£ ãã¦ãè¯ãã§ãããLiveDataã®ä»çµã¿ã使ã£ã¦ViewModelããViewã«å¯¾ãã¦ã¤ãã³ãã®éç¥ãè¡ããLiveEventãªã©ã®ã©ã¤ãã©ãªãå©ç¨ãã¦ç°¡åã«å®è£ ã§ãã¾ãã
ãã®ããã«ãMVVMã§Androidéçºãããå ´åãUIæç»ã«å¿ è¦ãªãã¼ã¿(ç»é¢ã®ç¶æ )ãLiveDataã§ViewModelã«ä¿æãããããViewã§ç£è¦ãã¦é©å®DataBindingã§UIã«åæ ããããã¨ã§View â ViewModelã®åä¸æ¹åã®ä¾åé¢ä¿ãå®ç¾ã§ãã¾ãã
ViewModelã¨Model
ViewModelã¨Modelã¯å ·ä½çã«ã¯ãããã以ä¸ã®ãããªå½¹å²ãæ ãã¾ãã
ViewModelã®å½¹å²
- ãã¸ãã¹ãã¸ãã¯ã®å ¥åå¤ã®å¤æãå®è¡æ示ãè¿å´å¤ã®åãåã
- Modelãä¿æããå¤ã®åç §
Modelã®å½¹å²
- ãã¸ãã¹ãã¸ãã¯(APIããæ å ±ã®åå¾ãDBã®æä½ãªã©)ã®å®è¡
- data class ãå®ç¾©ãåå¾ãããã¼ã¿ãã¢ããªå ã§åãåãã®è¯ãç¶æ ã«å¤æ
ViewModel â Modelã®å ´åããView â ViewModelã¨åæ§ãViewModelããModelã®å¦çãå®è¡ãã¦ãã¼ã¿ãåå¾ããããViewModelã§å¿ è¦ãªæ å ±ãModelã§ä¿æãããã¼ã¿ãåç §ãã¦åå¾ããããã¾ãã ãããããã¨ã§ããã¡ããViewModel â Modelã®åä¸æ¹åã®ä¾åé¢ä¿ã«ãªãã¾ãã
ã¾ã¨ã
MVVMã§ã¯ããããã®è¦ç´ ã«å¯¾ãã¦ä¸è¨ã®ããã«é¢å¿ã®åé¢ãè¡ãã¾ãã
View
- UIã®æç»
- UIã®æä½ãåãåã£ã¦ViewModelã«ä¼ãã
- ViewModelã§ä¿æãã¦ããUIæç»ã«å¿ è¦ãªãã¼ã¿ãç£è¦ãå¤æ´ãæ¤ç¥ãã
ViewModel
- UIæç»ã«å¿ è¦ãªãã¼ã¿ã®ä¿æã¨æ´æ°
- Modelã«å¯¾ãã¦ãã¸ãã¹ãã¸ãã¯ã®å®è¡ãæ示
- Modelã®ä¿æãããã¼ã¿ã®åç §
- Viewã¨Modelã®éã§å¿ è¦ãªãã¼ã¿ã®å¤æ
Model
- ãã¸ãã¹ãã¸ãã¯ã®å®è¡
- ã¢ããªå ã§åãåããã¼ã¿ã®å®ç¾©
ã¾ãããã¼ã¿ããã¼ã®ä¸æµå´ãä¸æµå´ã«ä¾åé¢ä¿ãæã¡ãä¸æµå´ã®è¦ç´ ã®æä½ã¨ãã¼ã¿ã®åç §ãè¡ããã¨ã§åä¸æ¹åã®ä¾åé¢ä¿ã¨ã·ã³ãã«ãªãã¼ã¿ããã¼ãå®ç¾ãå®ç¾ã§ãã¾ãã
ç¹ã«Androidã®å ´åã¯View - ViewModeléã«ããã¦DataBindingã¨LiveDataãçµã¿åããã¦ä½¿ç¨ãããã¨ã§ãViewã®ãã¸ãã¯ãæ¸ããã¤ã¤åä¸æ¹åã®ä¾åé¢ä¿ãç°¡åã«å®ç¾ã§ãã¾ãã
çµããã«
MVVMãç解ããåã¯ã©ã®ã¯ã©ã¹ã«ä½ãå®è£ ãã¹ããè¿·ããã¨ãå¤ããç¹ã«Viewã«ã¡ãã£ã¨ãããã¸ãã¯ãæ¸ãã¦ãã¾ããã¨ããViewModelããFragmentã®æä½ããã¦ãã¾ããã¨ãå¤ãã£ãã§ãã ã§ãããModel - View - ViewModelã§ã©ã®ããã«é¢å¿ã®åé¢ã¨åä¸æ¹åã®ä¾åé¢ä¿ã®å®ç¾ããããã«ä¼´ã£ã¦ã©ã®ãããªãã¼ã¿ããã¼ã«ãªãããç解ãããã¨ã§å®è£ ã«è¿·ãããªããªãã¾ããã
ã¾ããä»ã®éçºè ã®å®è£ ãã³ã¼ããè¦ãªãã¦ããªãã¨ãªãæ³åã§ããã®ã§ãã©ãã«ã©ãããå¦çãæããã¦ãããè¦ã¤ãããããªãããã§ã«ããã³ã¼ãã®ä¿®æ£ããèªåãããããå®è£ ã®åèã«ãªãã³ã¼ãã®çºè¦ãã³ã¼ãã¬ãã¥ã¼ã®ã¹ãã¼ããä¸ãã£ãå®æãããã¾ãã
Modelå ã®è©±ã¯ã¾ã ã¾ã åãããªããã¨ãå¤ãã®ã§ãããããç解ãæ·±ãã¦ããããã§ã!
ã¼ãã
Androidéçºã«æºããããã«ãªãåã¯Vue.jsã§Webã¢ããªã±ã¼ã·ã§ã³ãä½ã£ã¦ããã®ã§ãããVue.jsãMVVMãåæã¨ãããã¬ã¼ã ã¯ã¼ã¯ãªã®ã§ãMVVMããªãã¨ãªãç解ããä»æ¯ãè¿ãã¨ãã£ã¨ããããã°ããã£ããªãã¨ããããã¯ããããæå³ã ã£ããã ãªãã¨æã£ãããã¾ãã