Android/Desgin Pattern

MVP패턴 (+BaseActivity, BasePresenter, BaseView)

lipnus 2019. 5. 7. 15:14
반응형


MVP패턴 (+BaseActivity, BasePresenter, BaseView)

NumPicker Sample App 만들 때 사용: https://github.com/lipnus/picker

Google Kotlin MVP todo Sample:https://github.com/googlesamples/android-architecture/tree/todo-mvp-kotlin/



BaseActivity.kt

open class BaseActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}


//프레퍼런스에서 불러오기
open fun getStringPref(key: String):String {
return PreferenceHelper.customPrefs(applicationContext, getString(R.string.PREF)).getString(key, "empty")
}

//프레퍼런스에 저장
open fun saveStringPref(key:String, value:String) {
PreferenceHelper.customPrefs(applicationContext, getString(R.string.PREF)).edit().putString(key, value).apply()
}

}


BasePresenter.kt

interface BasePresenter{

fun start()
}



BaseView.kt

interface BaseView<T>{

var presenter: T

fun initLayout()

fun getStringPref(key: String):String
fun saveStringPref(key:String, value:String)

}



MainContract.kt

interface MainContract{

interface View: BaseView<Presenter> {

fun initViewPager()

fun setPageIndex(index: Int)

}


interface Presenter: BasePresenter {

fun initDatabase()
fun isDatabaseInited():Boolean

}
}



MainPresenter.kt

class MainPresenter(private val mainView: MainContract.View): MainContract.Presenter{


override fun start() {

mainView.initLayout()
mainView.initViewPager()

initDatabase()
}


//최초실행 시 Database를 초기화한다.
override fun initDatabase() {

if( ! isDatabaseInited()){


}
}


override fun isDatabaseInited():Boolean {

if(mainView.getStringPref("DATABASE_INIT") == "empty"){
mainView.saveStringPref("DATABASE_INIT", "inited")
return false
}
return true
}
}



MainActivity.kt

class MainActivity :
BaseActivity(),
MainContract.View,
FirstFragment.OnFragmentInteractionListener{


override lateinit var presenter: MainContract.Presenter


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


presenter = MainPresenter(this)
presenter.start()

}


override fun initLayout(){
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)

}

override fun initViewPager() {

val mainAdapter = MainPagerAdapter(supportFragmentManager)

var firstFragment: FirstFragment = FirstFragment.newInstance("파라미터: 첫번재")
var secondFragment: SecondFragment = SecondFragment.newInstance("파라미터: 두번째")
var thirdFragment: ThirdFragment = ThirdFragment.newInstance("파라미터: 세번째")


mainAdapter.run {
addFragement(firstFragment,"첫번째")
addFragement(secondFragment,"두번째")
addFragement(thirdFragment,"세번째")
}
viewpager.adapter = mainAdapter

viewpager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

override fun onPageScrollStateChanged(state: Int) {
}

override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

}
override fun onPageSelected(position: Int) {
navigation.menu.getItem(position).setChecked(true)
}

})

}


override fun setPageIndex(index: Int) {
viewpager?.currentItem = index
}


private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_first -> {
setPageIndex(0)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_second -> {
setPageIndex(1)
return@OnNavigationItemSelectedListener true
}
R.id.navigation_third -> {
setPageIndex(2)
return@OnNavigationItemSelectedListener true
}
}
false
}


override fun onFragmentInteraction(msg: String) {

}


반응형

'Android > Desgin Pattern' 카테고리의 다른 글

MVC, MVP, MVVM 비교  (0) 2019.05.10
Listener  (0) 2019.03.21
Callback(Listener) 예제  (0) 2018.12.11
[kotlin] MVP패턴  (0) 2018.12.10
싱글톤(Singleton) 패턴  (0) 2018.12.09