Android/Desgin Pattern

[kotlin] MVP패턴

lipnus 2018. 12. 10. 23:28
반응형


1. 기본구조



2. Contract Interface

interface MainContract{

interface View{
fun initWebView(startURL: String)

fun initWebViewClient()

fun showArticleActivity(aid: String, oid: String)
}


interface Presenter{
fun start()

fun isHaveOidAid(url: String): Boolean
}
}

viewpresenter를 설계한다.



3. Presenter Class

class MainPresenter: MainContract.Presenter {

private var view: MainContract.View

constructor(view: MainContract.View){
this.view = view
}

override fun start() {
val START_URL = "https://m.naver.com/?menu=ENT&contentOnly=YES&newMain=YES/"
view.initWebView(START_URL)
view.initWebViewClient()
}

override fun isHaveOidAid(url: String): Boolean {

//주소에 oid와 aid값이 있는지 확인하고 Redirect유무 반환
val uri = Uri.parse(url)
val aid=uri.getQueryParameter("aid")
val oid = uri.getQueryParameter(("oid"))

if( !aid.isNullOrEmpty() && !oid.isNullOrEmpty() ){
view.showArticleActivity(aid, oid)
return true
}
return false
}
}


참고로 생성자 부분은 아래와 같이 바꿀 수 있다.

class MainPresenter(var view: MainContract.View): MainContract.Presenter {




4. View (MainActivity.kt)

class MainActivity : AppCompatActivity(), MainContract.View {

//Presenter
private lateinit var presenter: MainContract.Presenter

private var isPageLoaded: Boolean = false

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

//Presenter초기화
presenter = MainPresenter(this)
presenter.start()

}

override fun initWebView(startURL: String){

// 웹뷰의 인스턴스를 가져온다
val settings = main_webview.settings

settings?.run{
// 자바스크립트 사용가능 유무
javaScriptEnabled = true

// cache유무
setAppCacheEnabled(true)
cacheMode = WebSettings.LOAD_DEFAULT
setAppCachePath(cacheDir.path)

// zoom
setSupportZoom(true)
builtInZoomControls = true
displayZoomControls = true

// 텍스트만 zoom
textZoom = 125

// 웹뷰에서 이미지 보지 않기설정
blockNetworkImage = false
// 웹뷰가 이미지의 리소스를 로드할 것인지 유무
loadsImagesAutomatically = true

// More web view settings
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
safeBrowsingEnabled = true // api 26
}

//settings.pluginState = WebSettings.PluginState.ON
useWideViewPort = true
loadWithOverviewMode = true
javaScriptCanOpenWindowsAutomatically = true
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.mediaPlaybackRequiresUserGesture = false
}

// More optional settings, you can enable it by yourself
domStorageEnabled = true
setSupportMultipleWindows(true)
loadWithOverviewMode = true
allowContentAccess = true
setGeolocationEnabled(true)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
allowUniversalAccessFromFileURLs = true
}

allowFileAccess = true
}


main_webview.run{
// webView settings
fitsSystemWindows = true

// SDK version 19이상이면 하드웨어가속, 그렇지 않으면 소프트웨어 가속
setLayerType(View.LAYER_TYPE_HARDWARE, null)

//웹뷰 로드
loadUrl(startURL)
}
}

override fun initWebViewClient(){

//터치한 url이 Article인 경우 ArticleActivity를 띄운다
main_webview.webViewClient = object : WebViewClient() {

//API 24이상
override fun shouldOverrideUrlLoading(view: WebView?, url: String): Boolean {
return presenter.isHaveOidAid(url)
}

//API 23이하
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
return presenter.isHaveOidAid(request?.url.toString())
}
}
}

override fun showArticleActivity(aid: String, oid: String){

//targetURL이 article인 경우 이곳을 통해 ArticleActivity를 띄운다
var iT = Intent(this, ArticleActivity::class.java)
iT.putExtra("OID", oid)
iT.putExtra("AID", aid)
startActivity(iT)
}

}


반응형

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

MVC, MVP, MVVM 비교  (0) 2019.05.10
MVP패턴 (+BaseActivity, BasePresenter, BaseView)  (0) 2019.05.07
Listener  (0) 2019.03.21
Callback(Listener) 예제  (0) 2018.12.11
싱글톤(Singleton) 패턴  (0) 2018.12.09