1
1
package com.shuyu.github.kotlin.module.main
2
2
3
+ import android.app.Activity
4
+ import android.content.Context
3
5
import android.support.v7.widget.Toolbar
4
6
import androidx.core.net.toUri
5
7
import com.mikepenz.materialdrawer.AccountHeaderBuilder
@@ -10,23 +12,28 @@ import com.mikepenz.materialdrawer.model.ProfileDrawerItem
10
12
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem
11
13
import com.orhanobut.dialogplus.DialogPlus
12
14
import com.shuyu.github.kotlin.R
15
+ import com.shuyu.github.kotlin.common.net.ResultCallBack
13
16
import com.shuyu.github.kotlin.common.utils.IssueDialogClickListener
17
+ import com.shuyu.github.kotlin.common.utils.getVersionName
14
18
import com.shuyu.github.kotlin.common.utils.showIssueEditDialog
15
19
import com.shuyu.github.kotlin.model.AppGlobalModel
16
20
import com.shuyu.github.kotlin.model.bean.Issue
21
+ import com.shuyu.github.kotlin.model.bean.Release
17
22
import com.shuyu.github.kotlin.repository.IssueRepository
18
23
import com.shuyu.github.kotlin.repository.LoginRepository
19
- import org.jetbrains.anko.alert
24
+ import com.shuyu.github.kotlin.repository.ReposRepository
25
+ import org.jetbrains.anko.*
20
26
21
27
/* *
22
28
* Created by guoshuyu
23
29
* Date: 2018-11-09
24
30
*/
25
31
26
32
27
- class MainDrawerController (private val activity : MainActivity , toolbar : Toolbar ,
33
+ class MainDrawerController (private val activity : Activity , toolbar : Toolbar ,
28
34
loginRepository : LoginRepository ,
29
35
private val issueRepository : IssueRepository ,
36
+ private val reposRepository : ReposRepository ,
30
37
globalModel : AppGlobalModel ) {
31
38
32
39
var drawer: Drawer ? = null
@@ -44,6 +51,14 @@ class MainDrawerController(private val activity: MainActivity, toolbar: Toolbar,
44
51
true
45
52
}
46
53
)
54
+ .addDrawerItems(
55
+ PrimaryDrawerItem ().withName(R .string.update)
56
+ .withTextColorRes(R .color.colorPrimary).withOnDrawerItemClickListener { view, position, drawerItem ->
57
+ checkUpdate(true )
58
+ unSelect(drawerItem)
59
+ true
60
+ }
61
+ )
47
62
.addDrawerItems(
48
63
PrimaryDrawerItem ().withName(R .string.about)
49
64
.withTextColorRes(R .color.colorPrimary).withOnDrawerItemClickListener { view, position, drawerItem ->
@@ -68,6 +83,9 @@ class MainDrawerController(private val activity: MainActivity, toolbar: Toolbar,
68
83
.withHeaderBackground(R .color.colorPrimary)
69
84
.withSelectionListEnabled(false )
70
85
.build()).build()
86
+
87
+
88
+ checkUpdate(false )
71
89
}
72
90
73
91
@@ -85,11 +103,9 @@ class MainDrawerController(private val activity: MainActivity, toolbar: Toolbar,
85
103
}
86
104
87
105
private fun showAboutDialog () {
88
- val manager = activity.packageManager.getPackageInfo(activity.packageName, 0 )
89
-
90
106
activity.alert {
91
107
this .title = activity.getString(R .string.app_name)
92
- this .message = activity.getString( R .string.version) + " : " + manager.versionName
108
+ this .message = activity.getVersionName()
93
109
this .show()
94
110
}
95
111
}
@@ -98,4 +114,32 @@ class MainDrawerController(private val activity: MainActivity, toolbar: Toolbar,
98
114
drawerItem.withSetSelected(false )
99
115
drawer?.adapter?.notifyAdapterDataSetChanged()
100
116
}
117
+
118
+ private fun checkUpdate (needTip : Boolean = false) {
119
+ reposRepository.checkoutUpDate(activity, object : ResultCallBack <Release > {
120
+ override fun onSuccess (result : Release ? ) {
121
+ result?.name?.apply {
122
+ showUpdateDialog(activity, this , result.body ? : " " , " www.baidu.com" )
123
+ return
124
+ }
125
+ if (needTip) {
126
+ activity.toast(R .string.newestVersion)
127
+ }
128
+ }
129
+ })
130
+ }
131
+
132
+ private fun showUpdateDialog (context : Context , version : String , message : String , url : String ) {
133
+ activity.alert {
134
+ this .title = activity.getString(R .string.app_name)
135
+ this .message = " $version : \n $message "
136
+ this .cancelButton {
137
+ it.dismiss()
138
+ }
139
+ this .okButton {
140
+ context.browse(url)
141
+ }
142
+ this .show()
143
+ }
144
+ }
101
145
}
0 commit comments