Files
quik/presentation/src/main/java/com/moez/QKSMS/common/QkDialog.kt
T
2023-11-14 13:48:22 -07:00

68 lines
2.2 KiB
Kotlin

/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package dev.octoshrimpy.quik.common
import android.app.Activity
import android.content.Context
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import dev.octoshrimpy.quik.common.util.extensions.dpToPx
import dev.octoshrimpy.quik.common.util.extensions.setPadding
import dev.octoshrimpy.quik.injection.appComponent
import javax.inject.Inject
/**
* Wrapper around AlertDialog which makes it easier to display lists that use our UI
*/
class QkDialog @Inject constructor(private val context: Context, val adapter: MenuItemAdapter) {
var title: String? = null
init {
appComponent.inject(this)
}
fun show(activity: Activity) {
val recyclerView = RecyclerView(activity)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = adapter
recyclerView.setPadding(top = 8.dpToPx(context), bottom = 8.dpToPx(context))
val dialog = AlertDialog.Builder(activity)
.setTitle(title)
.setView(recyclerView)
.create()
val clicks = adapter.menuItemClicks
.subscribe { dialog.dismiss() }
dialog.setOnDismissListener {
clicks.dispose()
}
dialog.show()
}
fun setTitle(@StringRes title: Int) {
this.title = context.getString(title)
}
}