添加权限请求

pull/32/head
Invinciblelee 6 years ago
parent 17b59ad7de
commit f5c79a7c10
  1. 16
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 1
      app/src/main/java/io/legado/app/help/permission/PermissionActivity.kt
  3. 26
      app/src/main/java/io/legado/app/help/permission/PermissionsCompat.kt
  4. 21
      app/src/main/java/io/legado/app/ui/widget/TitleBar.kt

@ -39,20 +39,4 @@ abstract class BaseActivity<BD : ViewDataBinding, VM : ViewModel> : AppCompatAct
open fun onCompatOptionsItemSelected(item: MenuItem): Boolean { open fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
return true return true
} }
override fun setTitle(title: CharSequence?) {
supportActionBar?.title = title
}
override fun setTitle(titleId: Int) {
supportActionBar?.setTitle(titleId)
}
fun setSubTitle(subtitle: CharSequence?) {
supportActionBar?.subtitle = subtitle
}
fun setSubTitle(subtitleId: Int) {
supportActionBar?.setSubtitle(subtitleId)
}
} }

@ -14,7 +14,6 @@ class PermissionActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val intent = intent
when (intent.getIntExtra(KEY_INPUT_REQUEST_TYPE, Request.TYPE_REQUEST_PERMISSION)) { when (intent.getIntExtra(KEY_INPUT_REQUEST_TYPE, Request.TYPE_REQUEST_PERMISSION)) {
Request.TYPE_REQUEST_PERMISSION//权限请求 Request.TYPE_REQUEST_PERMISSION//权限请求

@ -6,35 +6,35 @@ import androidx.fragment.app.Fragment
class PermissionsCompat private constructor() { class PermissionsCompat private constructor() {
private var mRequest: Request? = null private var request: Request? = null
fun request() { fun request() {
RequestManager.pushRequest(mRequest) RequestManager.pushRequest(request)
} }
class Builder { class Builder {
private val mRequest: Request private val request: Request
constructor(activity: AppCompatActivity) { constructor(activity: AppCompatActivity) {
mRequest = Request(activity) request = Request(activity)
} }
constructor(fragment: Fragment) { constructor(fragment: Fragment) {
mRequest = Request(fragment) request = Request(fragment)
} }
fun addPermissions(vararg permissions: String): Builder { fun addPermissions(vararg permissions: String): Builder {
mRequest.addPermissions(*permissions) request.addPermissions(*permissions)
return this return this
} }
fun requestCode(requestCode: Int): Builder { fun requestCode(requestCode: Int): Builder {
mRequest.setRequestCode(requestCode) request.setRequestCode(requestCode)
return this return this
} }
fun onGranted(callback: (requestCode: Int) -> Unit): Builder { fun onGranted(callback: (requestCode: Int) -> Unit): Builder {
mRequest.setOnGrantedCallback(object : OnPermissionsGrantedCallback { request.setOnGrantedCallback(object : OnPermissionsGrantedCallback {
override fun onPermissionsGranted(requestCode: Int) { override fun onPermissionsGranted(requestCode: Int) {
callback(requestCode) callback(requestCode)
} }
@ -43,7 +43,7 @@ class PermissionsCompat private constructor() {
} }
fun onDenied(callback: (requestCode: Int, deniedPermissions: Array<String>) -> Unit): Builder { fun onDenied(callback: (requestCode: Int, deniedPermissions: Array<String>) -> Unit): Builder {
mRequest.setOnDeniedCallback(object : OnPermissionsDeniedCallback { request.setOnDeniedCallback(object : OnPermissionsDeniedCallback {
override fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array<String>) { override fun onPermissionsDenied(requestCode: Int, deniedPermissions: Array<String>) {
callback(requestCode, deniedPermissions) callback(requestCode, deniedPermissions)
} }
@ -52,24 +52,24 @@ class PermissionsCompat private constructor() {
} }
fun rationale(rationale: CharSequence): Builder { fun rationale(rationale: CharSequence): Builder {
mRequest.setRationale(rationale) request.setRationale(rationale)
return this return this
} }
fun rationale(@StringRes resId: Int): Builder { fun rationale(@StringRes resId: Int): Builder {
mRequest.setRationale(resId) request.setRationale(resId)
return this return this
} }
fun build(): PermissionsCompat { fun build(): PermissionsCompat {
val compat = PermissionsCompat() val compat = PermissionsCompat()
compat.mRequest = mRequest compat.request = request
return compat return compat
} }
fun request(): PermissionsCompat { fun request(): PermissionsCompat {
val compat = build() val compat = build()
compat.mRequest = mRequest compat.request = request
compat.request() compat.request()
return compat return compat
} }

@ -14,7 +14,6 @@ import androidx.appcompat.widget.Toolbar
import androidx.core.graphics.drawable.DrawableCompat import androidx.core.graphics.drawable.DrawableCompat
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import io.legado.app.R import io.legado.app.R
import kotlinx.android.synthetic.main.view_titlebar.view.*
class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, attrs) { class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, attrs) {
@ -22,6 +21,18 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
val menu: Menu val menu: Menu
get() = toolbar.menu get() = toolbar.menu
var title: CharSequence?
get() = toolbar.title
set(title) {
toolbar.title = title
}
var subtitle: CharSequence?
get() = toolbar.subtitle
set(subtitle) {
toolbar.subtitle = subtitle
}
init { init {
inflate(context, R.layout.view_titlebar, this) inflate(context, R.layout.view_titlebar, this)
toolbar = findViewById(R.id.toolbar) toolbar = findViewById(R.id.toolbar)
@ -81,18 +92,10 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
toolbar.setNavigationOnClickListener(clickListener) toolbar.setNavigationOnClickListener(clickListener)
} }
fun setTitle(title: CharSequence?) {
toolbar.title = title
}
fun setTitle(titleId: Int) { fun setTitle(titleId: Int) {
toolbar.setTitle(titleId) toolbar.setTitle(titleId)
} }
fun setSubTitle(subtitle: CharSequence?) {
toolbar.subtitle = subtitle
}
fun setSubTitle(subtitleId: Int) { fun setSubTitle(subtitleId: Int) {
toolbar.setSubtitle(subtitleId) toolbar.setSubtitle(subtitleId)
} }

Loading…
Cancel
Save