2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter.dialog
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.app.Dialog
|
2019-10-22 20:21:03 +02:00
|
|
|
import android.text.Spannable
|
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.text.style.RelativeSizeSpan
|
2019-02-15 02:51:22 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2018-01-04 19:52:25 +01:00
|
|
|
import android.view.Gravity
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.Button
|
2018-01-21 13:46:36 +01:00
|
|
|
import android.widget.LinearLayout
|
|
|
|
import android.widget.TextView
|
2020-02-11 18:01:15 +01:00
|
|
|
import androidx.core.content.ContextCompat
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.R
|
|
|
|
import jp.juggler.subwaytooter.table.AcctColor
|
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
2018-01-10 16:47:35 +01:00
|
|
|
import jp.juggler.subwaytooter.util.DialogInterfaceCallback
|
|
|
|
import jp.juggler.subwaytooter.util.SavedAccountCallback
|
2019-01-19 03:36:40 +01:00
|
|
|
import jp.juggler.util.dismissSafe
|
2020-02-11 18:01:15 +01:00
|
|
|
import jp.juggler.util.getAdaptiveRippleDrawable
|
|
|
|
import jp.juggler.util.getAdaptiveRippleDrawableRound
|
2018-12-01 00:02:18 +01:00
|
|
|
import jp.juggler.util.showToast
|
2019-01-20 22:04:57 +01:00
|
|
|
import org.jetbrains.anko.textColor
|
2018-12-01 00:02:18 +01:00
|
|
|
import java.util.*
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
object AccountPicker {
|
|
|
|
|
2018-01-13 07:15:52 +01:00
|
|
|
@SuppressLint("InflateParams")
|
2018-01-04 19:52:25 +01:00
|
|
|
fun pick(
|
2018-01-13 07:15:52 +01:00
|
|
|
activity : AppCompatActivity,
|
|
|
|
bAllowPseudo : Boolean = false,
|
2018-08-20 02:07:55 +02:00
|
|
|
bAllowMisskey : Boolean = true,
|
2018-08-23 04:20:10 +02:00
|
|
|
bAllowMastodon: Boolean = true,
|
2018-01-13 07:15:52 +01:00
|
|
|
bAuto : Boolean = false,
|
|
|
|
message : String? = null,
|
|
|
|
accountListArg : ArrayList<SavedAccount>? = null,
|
|
|
|
dismiss_callback : DialogInterfaceCallback? = null,
|
2019-02-13 08:17:49 +01:00
|
|
|
extra_callback : (LinearLayout,Int,Int)->Unit = {_,_,_->},
|
2018-01-13 07:15:52 +01:00
|
|
|
callback : SavedAccountCallback
|
2018-01-04 19:52:25 +01:00
|
|
|
) {
|
2018-08-20 02:07:55 +02:00
|
|
|
var removedMisskey =0
|
|
|
|
var removedPseudo =0
|
2018-08-23 04:20:10 +02:00
|
|
|
var removeMastodon = 0
|
2018-08-20 02:07:55 +02:00
|
|
|
val account_list : MutableList<SavedAccount> = accountListArg ?: {
|
|
|
|
val l = SavedAccount.loadAccountList(activity).filter { a->
|
|
|
|
var bOk = true
|
2018-08-23 04:20:10 +02:00
|
|
|
|
|
|
|
if( !bAllowMastodon && !a.isMisskey ){
|
|
|
|
++removeMastodon
|
|
|
|
bOk=false
|
|
|
|
}
|
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
if( !bAllowMisskey && a.isMisskey ){
|
|
|
|
++removedMisskey
|
|
|
|
bOk=false
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !bAllowPseudo && a.isPseudo ){
|
|
|
|
++removedPseudo
|
|
|
|
bOk=false
|
|
|
|
}
|
|
|
|
|
|
|
|
bOk
|
|
|
|
}.toMutableList()
|
2018-01-13 07:15:52 +01:00
|
|
|
SavedAccount.sort(l)
|
|
|
|
l
|
|
|
|
}()
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
if(account_list.isEmpty()) {
|
2018-08-20 02:07:55 +02:00
|
|
|
|
|
|
|
val sb=StringBuilder()
|
|
|
|
|
|
|
|
if( removedPseudo > 0 ){
|
|
|
|
sb.append(activity.getString(R.string.not_available_for_pseudo_account))
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
|
|
|
|
if( removedMisskey > 0 ){
|
|
|
|
if(sb.isNotEmpty() ) sb.append('\n')
|
|
|
|
sb.append(activity.getString(R.string.not_available_for_misskey_account))
|
|
|
|
}
|
2018-08-23 04:20:10 +02:00
|
|
|
if( removeMastodon > 0 ){
|
|
|
|
if(sb.isNotEmpty() ) sb.append('\n')
|
|
|
|
sb.append(activity.getString(R.string.not_available_for_mastodon_account))
|
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
|
|
|
|
if( sb.isEmpty() ){
|
|
|
|
sb.append(activity.getString(R.string.account_empty))
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
|
|
|
|
showToast(activity, false,sb.toString())
|
|
|
|
return
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-08-20 02:07:55 +02:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
if(bAuto && account_list.size == 1) {
|
|
|
|
callback(account_list[0])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
val viewRoot = activity.layoutInflater.inflate(R.layout.dlg_account_picker, null, false)
|
|
|
|
|
|
|
|
val dialog = Dialog(activity)
|
|
|
|
val isDialogClosed = AtomicBoolean(false)
|
|
|
|
|
2018-01-13 07:15:52 +01:00
|
|
|
dialog.setOnDismissListener {
|
|
|
|
if(dismiss_callback != null) dismiss_callback(it)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
dialog.setContentView(viewRoot)
|
2018-01-13 07:15:52 +01:00
|
|
|
if(message != null && message.isNotEmpty()) {
|
2018-01-04 19:52:25 +01:00
|
|
|
val tv = viewRoot.findViewById<TextView>(R.id.tvMessage)
|
|
|
|
tv.visibility = View.VISIBLE
|
|
|
|
tv.text = message
|
|
|
|
}
|
|
|
|
viewRoot.findViewById<View>(R.id.btnCancel).setOnClickListener {
|
|
|
|
isDialogClosed.set(true)
|
|
|
|
dialog.cancel()
|
|
|
|
}
|
|
|
|
dialog.setCancelable(true)
|
|
|
|
dialog.setCanceledOnTouchOutside(true)
|
|
|
|
dialog.setOnCancelListener { isDialogClosed.set(true) }
|
|
|
|
|
|
|
|
val density = activity.resources.displayMetrics.density
|
|
|
|
|
2018-08-20 02:07:55 +02:00
|
|
|
val llAccounts :LinearLayout= viewRoot.findViewById(R.id.llAccounts)
|
2018-01-04 19:52:25 +01:00
|
|
|
val pad_se = (0.5f + 12f * density).toInt()
|
|
|
|
val pad_tb = (0.5f + 6f * density).toInt()
|
|
|
|
|
|
|
|
for(a in account_list) {
|
|
|
|
val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
|
|
|
|
|
2020-02-02 07:25:08 +01:00
|
|
|
val ac = AcctColor.load(a)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
val b = Button(activity)
|
|
|
|
|
|
|
|
if(AcctColor.hasColorBackground(ac)) {
|
2020-02-11 18:01:15 +01:00
|
|
|
b.background = getAdaptiveRippleDrawableRound(activity,ac.color_bg,ac.color_fg)
|
2018-01-04 19:52:25 +01:00
|
|
|
} else {
|
2020-02-11 18:01:15 +01:00
|
|
|
b.setBackgroundResource(R.drawable.btn_bg_transparent_round6dp)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
if(AcctColor.hasColorForeground(ac)) {
|
2019-01-20 22:04:57 +01:00
|
|
|
b.textColor = ac.color_fg
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
b.setPaddingRelative(pad_se, pad_tb, pad_se, pad_tb)
|
|
|
|
b.gravity = Gravity.START or Gravity.CENTER_VERTICAL
|
2018-12-01 00:02:18 +01:00
|
|
|
b.isAllCaps = false
|
2018-01-04 19:52:25 +01:00
|
|
|
b.layoutParams = lp
|
|
|
|
b.minHeight = (0.5f + 32f * density).toInt()
|
2019-10-22 20:21:03 +02:00
|
|
|
|
2020-02-02 07:25:08 +01:00
|
|
|
val sb = SpannableStringBuilder(ac.nickname)
|
2019-10-22 20:21:03 +02:00
|
|
|
if( a.last_notification_error?.isNotEmpty() == true) {
|
|
|
|
sb.append("\n")
|
|
|
|
val start = sb.length
|
|
|
|
sb.append(a.last_notification_error)
|
|
|
|
val end = sb.length
|
|
|
|
sb.setSpan(RelativeSizeSpan(0.7f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
2019-12-17 18:58:49 +01:00
|
|
|
}else if(a.last_subscription_error?.isNotEmpty() == true) {
|
|
|
|
sb.append("\n")
|
|
|
|
val start = sb.length
|
|
|
|
sb.append(a.last_subscription_error)
|
|
|
|
val end = sb.length
|
|
|
|
sb.setSpan(RelativeSizeSpan(0.7f), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
|
2019-10-22 20:21:03 +02:00
|
|
|
}
|
|
|
|
b.text = sb
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
b.setOnClickListener {
|
|
|
|
isDialogClosed.set(true)
|
|
|
|
callback(a)
|
2019-01-19 03:36:40 +01:00
|
|
|
dialog.dismissSafe()
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
llAccounts.addView(b)
|
|
|
|
}
|
|
|
|
|
2019-02-13 08:17:49 +01:00
|
|
|
extra_callback(llAccounts,pad_se, pad_tb)
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
dialog.show()
|
|
|
|
}
|
|
|
|
}
|