【kotlin設(shè)計(jì)與架構(gòu)】MVP的簡(jiǎn)單使用

//基類
/**
 * 一佣盒、GuardBasePresenter
 * abstract抽象類肥惭,區(qū)別interface务豺,abstract可以有方法實(shí)現(xiàn)笼沥。
 * 傳入一個(gè)泛型GuardBaseView,內(nèi)部進(jìn)行SoftReference(WeakReference)馆纳,防止循環(huán)引用出現(xiàn)內(nèi)存泄露鲁驶。
 */
abstract class GuardBasePresenter<V: GuardBaseView>(v: V) {
    var mView: SoftReference<V> = SoftReference(v)
    //自定義生命周期方法钥弯,在GuardBaseActivity調(diào)用,達(dá)到activity同樣的生命周期效果总处。
    fun onCreate() {
        mView.get()?.initView()
    }

    open fun onStart() {}
    open fun onResume() {}
    open fun onPause() {}
    open fun onStop() {}
    open fun onDestroy() {}
    open fun onCreateView(arguments: Bundle?) {}
}

/**
 * 二鹦马、GuardBaseView
 * interface GuardBaseView內(nèi)部獲得
 * 一個(gè)val mPresenter: GuardBasePresenter<out GuardBaseView>,Out (協(xié)變)將
 * 泛型作為內(nèi)部方法的返回,即mPresenter返回出去可以直接給實(shí)現(xiàn)GuardBaseView接口的類使用客冈。
 */

interface GuardBaseView {
    val mPresenter: GuardBasePresenter<out GuardBaseView>
    //ui創(chuàng)建方法
    fun initView()
    //自定義通用方法
    fun showProgressDialog(){

    }

    fun dismissProgressDialog(){

    }

    fun showToast(text:String,context: Context,time:Int=Toast.LENGTH_SHORT){
        Toast.makeText(context,text,time).show()
    }
}

/**
 * 三旭从、GuardBaseActivity
 * 在對(duì)應(yīng)生命周期方法中調(diào)用GuardBasePresenter自定義生命周期方法,實(shí)現(xiàn)效果场仲。
 */
abstract class GuardBaseActivity: Activity(), GuardBaseView {
    private val mAllPresenters = HashSet<GuardBasePresenter<*>>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        addPresenters()
        mAllPresenters.forEach { it.onCreate() }
    }

    private fun getPresenters():MutableList<GuardBasePresenter<*>> {
        return mutableListOf(mPresenter)
    }

    private fun addPresenters() {
        getPresenters().forEach{ mAllPresenters.add(it) }
    }

    override fun onStart() {
        super.onStart()
        mAllPresenters.forEach { it.onStart() }
    }

    override fun onResume() {
        super.onResume()
        mAllPresenters.forEach { it.onResume() }
    }

    override fun onPause() {
        super.onPause()
        mAllPresenters.forEach { it.onPause() }
    }

    override fun onStop() {
        super.onStop()
        mAllPresenters.forEach { it.onStop() }
    }

    override fun onDestroy() {
        super.onDestroy()
        mAllPresenters.forEach { it.onDestroy() }
    }

    override fun showProgressDialog() {
        super.showProgressDialog()
    }

    override fun dismissProgressDialog() {
        super.dismissProgressDialog()
    }
}

//使用部分

/**
 * 四遇绞、GuardContract
 * 提供mvp組合接口,以便activity中調(diào)用燎窘。
 */
interface GuardContract {
    interface GuardView: GuardBaseView {
        fun showResult(text: String)
    }

    interface GuardPresenter{
        fun textClick(id: String)
    }
}

/**
 * 五、GuardPresenter
 * 外部調(diào)用GuardPresenter會(huì)傳入GuardContract.GuardView
 * 實(shí)現(xiàn)GuardContract.GuardPresenter接口方法
 * mView從繼承的GuardBasePresenter獲得
 * 這一句 class GuardPresenter(view: GuardContract.GuardView): GuardContract.GuardPresenter, GuardBasePresenter<GuardContract.GuardView>(view)
 * 其中的GuardPresenter(view: GuardContract.GuardView)傳入的view提供給
 * GuardBasePresenter<GuardContract.GuardView>(view)使用
 * GuardBasePresenter必須傳入一個(gè)view進(jìn)行軟引用蹄咖,以便提供在activity中使用褐健。
 */

class GuardPresenter(view: GuardContract.GuardView): GuardContract.GuardPresenter, GuardBasePresenter<GuardContract.GuardView>(view) {
    override fun textClick(id: String) {
        //發(fā)送網(wǎng)絡(luò)請(qǐng)求
        GuardModel.requestData(id) {
            //得到處理后的數(shù)據(jù),通過數(shù)據(jù)判斷澜汤,決定調(diào)用GuardView哪些接口方法蚜迅。
            //失敗則彈出提示
            //mView.get()?.showToast(...)
            //成功比如設(shè)置數(shù)據(jù)
            mView.get()?.showResult(it + "presenter->")
        }
    }
}

/**
 * 六、GuardView俊抵,可以獨(dú)立出來徽诲,也可以直接寫在activity中偷溺。
 * anko擴(kuò)展封裝
 * lateinit var text: TextView為暴露的控件,方便activity設(shè)置值
 */

inline fun ViewManager.guardView() = guardView() {}
inline fun ViewManager.guardView(init: GuardView.() -> Unit): GuardView {
    return ankoView({ GuardView(it) },0,init)
}

class GuardView: FrameLayout {
    lateinit var text: TextView
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int): super(context, attrs, defStyleAttr) {
        verticalLayout {
            text = textView {
                text = "我在guardView中"
            }
        }
    }
}

/**
 * 七褒傅、GuardActivity
 * GuardBaseActivity里面包括GuardBaseView杠河,GuardBaseView里面包括了了GuardBasePresenter
 */

class GuardActivity : GuardBaseActivity(), GuardContract.GuardView {
    private lateinit var guardView: GuardView

    //創(chuàng)建Presenter唾戚。
    override val mPresenter: GuardPresenter
        get() = GuardPresenter(this)

    //通過model獲取到的值設(shè)置給UI控件。方法來自GuardContract.GuardView
    override fun showResult(text: String) {
        guardView.text.text = text + "activity實(shí)現(xiàn)GuardContract.GuardView的方法"
    }

    //GuardBaseView方法,創(chuàng)建UI方法。
    override fun initView() {
        verticalLayout {
            guardView = guardView {
                onClick { mPresenter.textClick("activity中initView方法內(nèi)view點(diǎn)擊->") }
            }
        }
    }
}

/**
 * 八、GuardModel
 * 進(jìn)行數(shù)據(jù)請(qǐng)求启泣,并且進(jìn)行數(shù)據(jù)處理后返回出去,以便Presenter使用纱耻。
 */
object GuardModel {
    fun requestData(userId: String ,callback: (String) -> Unit) {
        callback(userId + "model中數(shù)據(jù)請(qǐng)求->")
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子旱幼,更是在濱河造成了極大的恐慌柏卤,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弛车,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡双妨,警方通過查閱死者的電腦和手機(jī)浩姥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門眯分,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事纺座。” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵蹭秋,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任洒嗤,我火速辦了婚禮,結(jié)果婚禮上睬魂,老公的妹妹穿的比我還像新娘商佛。我一直安慰自己,他們只是感情好税课,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開白布击狮。 她就那樣靜靜地躺著寞焙,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛麻削,可吹牛的內(nèi)容都是我干的扫责。 我是一名探鬼主播苏揣,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起吞加,我...
    開封第一講書人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤码党,失蹤者是張志新(化名)和其女友劉穎兽狭,沒想到半個(gè)月后斩熊,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體渣叛,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡靴跛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片刊侯。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡辜羊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出纳本,到底是詐尸還是另有隱情观蓄,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布袭祟,位于F島的核電站,受9級(jí)特大地震影響胆绊,放射性物質(zhì)發(fā)生泄漏压状。R本人自食惡果不足惜种冬,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望跟匆。 院中可真熱鬧烤蜕,春花似錦堪藐、人聲如沸枫绅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽寓搬。三九已至,卻和暖如春县耽,著一層夾襖步出監(jiān)牢的瞬間句喷,已是汗流浹背镣典。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留唾琼,地道東北人兄春。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像锡溯,于是被迫代替她去往敵國和親赶舆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容