diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2017-10-11 21:51:43 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2017-10-11 21:51:43 +0900 |
commit | 3c15fb7dd7f0d722478217c11a9e20eee7992045 (patch) | |
tree | 2a12f240cf10284170105c0e74cd0a276148ad0e /app/src/main/java | |
parent | c225a3bdf691d7e6c677e424035b57ab0b0ee230 (diff) | |
download | SmileEssence-3c15fb7dd7f0d722478217c11a9e20eee7992045.tar.gz |
popup refactoring
Diffstat (limited to 'app/src/main/java')
15 files changed, 238 insertions, 332 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/EditExtractionActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/EditExtractionActivity.kt index e83bd5b9..317d782b 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/EditExtractionActivity.kt +++ b/app/src/main/java/net/lacolaco/smileessence/activity/EditExtractionActivity.kt @@ -1,6 +1,7 @@ package net.lacolaco.smileessence.activity import android.app.Activity +import android.app.AlertDialog import android.os.Bundle import android.support.v4.app.NavUtils import android.text.TextUtils @@ -9,11 +10,11 @@ import android.widget.AbsListView import android.widget.ListView import kotlinx.android.synthetic.main.layout_edit_list.* import kotlinx.android.synthetic.main.list_item_simple_text.view.* +import kotlinx.android.synthetic.main.part_edittext.view.* import net.lacolaco.smileessence.R import net.lacolaco.smileessence.data.ExtractionWord import net.lacolaco.smileessence.logging.Logger import net.lacolaco.smileessence.view.adapter.UnorderedCustomListAdapter -import net.lacolaco.smileessence.view.dialog.EditTextDialogFragment class EditExtractionActivity : Activity() { private val adapter by lazy { @@ -77,18 +78,23 @@ class EditExtractionActivity : Activity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_edit_list_add -> { - val dialogFragment = object : EditTextDialogFragment() { - override fun onTextInput(text: String) { - if (TextUtils.isEmpty(text.trim { it <= ' ' })) { - return + val view = layoutInflater.inflate(R.layout.part_edittext, null) + view.part_edittext.setText("") + val dialog = AlertDialog.Builder(this) + .setTitle(R.string.dialog_title_add) + .setView(view) + .setPositiveButton(R.string.alert_dialog_ok) { _, _ -> + val text = view.part_edittext.text.toString() + if (TextUtils.isEmpty(text.trim { it <= ' ' })) { + return@setPositiveButton + } + val extractionWord = ExtractionWord.add(text) + adapter.add(extractionWord) + adapter.update() } - val extractionWord = ExtractionWord.add(text) - adapter.add(extractionWord) - adapter.update() - } - } - dialogFragment.setParams(getString(R.string.dialog_title_add), "") - dialogFragment.show(fragmentManager, "EDIT_TEXT") + .setNegativeButton(R.string.alert_dialog_cancel) { _, _ -> } + .create() + dialog.show() } android.R.id.home -> { NavUtils.navigateUpFromSameTask(this) diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt index 437ec803..9a2f398a 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt +++ b/app/src/main/java/net/lacolaco/smileessence/activity/MainActivity.kt @@ -27,7 +27,7 @@ import net.lacolaco.smileessence.util.getMainActivityOrCancel import net.lacolaco.smileessence.util.launchUi import net.lacolaco.smileessence.view.DialogHelper import net.lacolaco.smileessence.view.adapter.PageListAdapter -import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment +import net.lacolaco.smileessence.view.confirm import net.lacolaco.smileessence.view.dialog.StatusDetailDialogFragment import net.lacolaco.smileessence.view.dialog.UserDetailDialogFragment import net.lacolaco.smileessence.view.page.* @@ -170,7 +170,7 @@ class MainActivity : Activity(), ViewPager.OnPageChangeListener { if (viewPager.currentItem != homeIndex) { viewPager.setCurrentItem(homeIndex, true) } else { - ConfirmDialogFragment.show(this, getString(R.string.dialog_confirm_finish_app)) { super.finish() } + confirm(R.string.dialog_confirm_finish_app) { super.finish() } } } diff --git a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt index 6765b02c..80eb671f 100644 --- a/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt +++ b/app/src/main/java/net/lacolaco/smileessence/activity/ManageAccountsActivity.kt @@ -23,7 +23,7 @@ import net.lacolaco.smileessence.World import net.lacolaco.smileessence.data.Account import net.lacolaco.smileessence.data.ImageCache import net.lacolaco.smileessence.logging.Logger -import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment +import net.lacolaco.smileessence.view.confirm import org.jetbrains.anko.coroutines.experimental.bg import org.jetbrains.anko.startActivityForResult import java.util.* @@ -61,10 +61,10 @@ class ManageAccountsActivity : Activity() { if (adapter.count > 1) { // remove account from application val account = adapter.getItem(i) - ConfirmDialogFragment.show(this, getString(R.string.dialog_confirm_clear_account, account.user.screenName), { + confirm(R.string.dialog_confirm_clear_account, account.user.screenName) { adapter.removeAt(i) Account.unregister(account.userId) - }, false) + } true } else { Application.toast(R.string.notice_cant_remove_last_account) diff --git a/app/src/main/java/net/lacolaco/smileessence/util/UIHelpers.kt b/app/src/main/java/net/lacolaco/smileessence/util/UIHelpers.kt index 21887a62..99fd60eb 100644 --- a/app/src/main/java/net/lacolaco/smileessence/util/UIHelpers.kt +++ b/app/src/main/java/net/lacolaco/smileessence/util/UIHelpers.kt @@ -1,18 +1,28 @@ package net.lacolaco.smileessence.util +import android.util.Log import kotlinx.coroutines.experimental.* import kotlinx.coroutines.experimental.android.UI import net.lacolaco.smileessence.World +private val exceptionHandler = CoroutineExceptionHandler { c, throwable -> + val tag = "CoroutineContext:$c" + Log.e(tag, "Uncaught exception ${throwable::class}; re-throwing...") + throw throwable +} + +val uiContext = UI + exceptionHandler +val bgContext = CommonPool + exceptionHandler + fun launchUi(block: suspend CoroutineScope.() -> Unit) { - launch(UI, CoroutineStart.DEFAULT, block) + launch(uiContext, CoroutineStart.DEFAULT, block) } fun <T> bg(block: suspend CoroutineScope.() -> T) = - async(CommonPool, CoroutineStart.DEFAULT, block) + async(bgContext, CoroutineStart.DEFAULT, block) fun launchBg(block: suspend CoroutineScope.() -> Unit) = - launch(CommonPool, CoroutineStart.DEFAULT, block) + launch(bgContext, CoroutineStart.DEFAULT, block) fun World.getMainActivityOrCancel() = getMainActivity() ?: throw CancellationException("MainActivity is gone") diff --git a/app/src/main/java/net/lacolaco/smileessence/view/Confirmation.kt b/app/src/main/java/net/lacolaco/smileessence/view/Confirmation.kt new file mode 100644 index 00000000..6d25db2e --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/Confirmation.kt @@ -0,0 +1,18 @@ +package net.lacolaco.smileessence.view + +import android.app.Activity +import android.app.AlertDialog +import android.app.Fragment +import net.lacolaco.smileessence.R + +fun Activity.confirm(resId: Int, vararg rest: Any, onOk: () -> Unit) { + val dialog = AlertDialog.Builder(this) + .setTitle(getString(resId, *rest)) + .setPositiveButton(R.string.alert_dialog_ok) { _, _ -> onOk() } + .setNegativeButton(R.string.alert_dialog_cancel) { _, _ -> } + .create() + dialog.show() +} + +fun Fragment.confirm(resId: Int, vararg rest: Any, onOk: () -> Unit) = + activity.confirm(resId, rest = *rest, onOk = onOk) diff --git a/app/src/main/java/net/lacolaco/smileessence/view/PopupMenu.kt b/app/src/main/java/net/lacolaco/smileessence/view/PopupMenu.kt new file mode 100644 index 00000000..794f77fd --- /dev/null +++ b/app/src/main/java/net/lacolaco/smileessence/view/PopupMenu.kt @@ -0,0 +1,29 @@ +package net.lacolaco.smileessence.view + +import android.content.Context +import android.view.MenuItem +import android.view.View + +class PopupMenu(context: Context, anchor: View) { + private val popupMenu = android.widget.PopupMenu(context, anchor) + private val menuItems = LinkedHashMap<MenuItem, (MenuItem) -> Unit>() + + init { + popupMenu.setOnMenuItemClickListener { it: MenuItem -> + menuItems[it]!!(it) + true + } + } + + fun add(title: String, cb: (MenuItem) -> Unit) = apply { + val item = popupMenu.menu.add(title) + menuItems.put(item, cb) + } + + fun add(titleRes: Int, cb: (MenuItem) -> Unit) = apply { + val item = popupMenu.menu.add(titleRes) + menuItems.put(item, cb) + } + + fun show() = popupMenu.show() +} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/SettingFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/SettingFragment.kt index af69e40f..670ca049 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/SettingFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/SettingFragment.kt @@ -91,10 +91,8 @@ class SettingFragment : PreferenceFragment(), OnSharedPreferenceChangeListener, contentView.versionTextView.text = BuildConfig.VERSION_NAME + " (rev: " + BuildConfig.VERSION_CODE + "; upstream: " + getString(R.string.app_version_full) + ")" AlertDialog.Builder(activity) - .setTitle(R.string.dialog_title_about) .setView(contentView) - .setPositiveButton(R.string.alert_dialog_ok) { dialog, which -> dialog.dismiss() } - .create().show() + .show() } private fun setSummaryCurrentValue() { diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/ConfirmDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/ConfirmDialogFragment.kt deleted file mode 100644 index 2a57a195..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/ConfirmDialogFragment.kt +++ /dev/null @@ -1,74 +0,0 @@ -package net.lacolaco.smileessence.view.dialog - -import android.app.Activity -import android.app.AlertDialog -import android.app.Dialog -import android.content.DialogInterface -import android.os.Bundle -import net.lacolaco.smileessence.R -import net.lacolaco.smileessence.preference.UserPreferenceHelper -import net.lacolaco.smileessence.view.DialogHelper - -abstract class ConfirmDialogFragment : StackableDialogFragment() { - private val text: String by lazy { arguments.getString(ARG_TEXT) ?: throw IllegalStateException("ARG_TEXT not set") } - - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - return AlertDialog.Builder(activity).setTitle(text) - .setCancelable(false) - .setPositiveButton(R.string.alert_dialog_ok, { dialog, which -> - onButtonClick(which) - dialog.dismiss() - }) - .setNegativeButton(R.string.alert_dialog_cancel, { dialog, which -> - onButtonClick(which) - dialog.dismiss() - }) - .create() - } - - // -------------------------- OTHER METHODS -------------------------- - - protected abstract fun onButtonClick(which: Int) - - companion object { - private val ARG_TEXT = "text" - - fun show(activity: Activity, text: String, onYes: () -> Unit) { - show(activity, text, onYes, null, true) - } - - fun show(activity: Activity, text: String, onYes: () -> Unit, ignorable: Boolean) { - show(activity, text, onYes, null, ignorable) - } - - fun show(activity: Activity, text: String, onOK: (() -> Unit)?, onCancel: (() -> Unit)?, ignorable: Boolean) { - val confirm = UserPreferenceHelper.instance[R.string.key_setting_show_confirm_dialog, true] - if (!confirm && ignorable) { - onOK!!() - return - } - val fragment = object : ConfirmDialogFragment() { - public override fun onButtonClick(which: Int) { - when (which) { - DialogInterface.BUTTON_POSITIVE -> { - if (onOK != null) { - dismiss() - onOK() - } - } - DialogInterface.BUTTON_NEGATIVE -> { - if (onCancel != null) { - dismiss() - onCancel() - } - } - } - } - } - val args = Bundle() - args.putString(ARG_TEXT, text) - fragment.arguments = args - DialogHelper.showDialog(activity, fragment) - } - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/EditTextDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/EditTextDialogFragment.kt deleted file mode 100644 index f35d6ba5..00000000 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/EditTextDialogFragment.kt +++ /dev/null @@ -1,41 +0,0 @@ -package net.lacolaco.smileessence.view.dialog - -import android.app.AlertDialog -import android.app.Dialog -import android.app.DialogFragment -import android.os.Bundle -import kotlinx.android.synthetic.main.part_edittext.view.* -import net.lacolaco.smileessence.R - -abstract class EditTextDialogFragment : DialogFragment() { - private val title by lazy { arguments.getString(KEY_TITLE) } - private val text by lazy { arguments.getString(KEY_TEXT) } - - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { - val view = activity.layoutInflater.inflate(R.layout.part_edittext, null) - view.part_edittext.setText(text) - return AlertDialog.Builder(activity) - .setTitle(title) - .setView(view) - .setPositiveButton(R.string.alert_dialog_ok) { dialog, which -> - onTextInput(view.part_edittext.text.toString()) - dialog.dismiss() - } - .setNegativeButton(R.string.alert_dialog_cancel) { dialog, which -> dialog.dismiss() } - .create() - } - - abstract fun onTextInput(text: String) - - fun setParams(title: String, text: String) { - val args = Bundle() - args.putString(KEY_TITLE, title) - args.putString(KEY_TEXT, text) - arguments = args - } - - companion object { - private val KEY_TITLE = "title" - private val KEY_TEXT = "text" - } -} diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.kt index 89c4473a..4c34f82c 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.kt @@ -1,6 +1,5 @@ package net.lacolaco.smileessence.view.dialog -import android.app.AlertDialog import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.view.LayoutInflater @@ -22,8 +21,10 @@ import net.lacolaco.smileessence.util.SystemServiceHelper import net.lacolaco.smileessence.util.launchBg import net.lacolaco.smileessence.view.DialogHelper import net.lacolaco.smileessence.view.Partials +import net.lacolaco.smileessence.view.PopupMenu import net.lacolaco.smileessence.view.adapter.MessageListAdapter import net.lacolaco.smileessence.view.adapter.UnorderedCustomListAdapter +import net.lacolaco.smileessence.view.confirm import java.util.* class MessageDetailDialogFragment : StackableDialogFragment() { @@ -52,7 +53,7 @@ class MessageDetailDialogFragment : StackableDialogFragment() { DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(message.sender)) } view.button_status_detail_delete.setOnClickListener { - ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { + confirm(R.string.dialog_confirm_commands) { launchBg { try { world.destroyMessageAsync(message.id).await() @@ -66,19 +67,12 @@ class MessageDetailDialogFragment : StackableDialogFragment() { } // -- menu dialog view.button_status_detail_menu.setOnClickListener { - val builder = AlertDialog.Builder(activity) - builder.setTitle("@" + message.sender.screenName + ": " + message.text) - .setItems(R.array.message_commands) { dialog, which -> - when (which) { - 0 -> { - SystemServiceHelper.copyToClipboard(activity, "message text", message.text) - world.notify(R.string.notice_copy_clipboard) - } - else -> throw IllegalStateException() - } - } - val dialog = builder.create() - dialog.show() + val popup = PopupMenu(activity, view.button_status_detail_menu) + popup.add(R.string.command_status_copy_text_to_clipboard) { + SystemServiceHelper.copyToClipboard(activity, "message text", message.text) + world.notify(R.string.notice_copy_clipboard) + } + popup.show() } // -- menu embedded in dialog diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.kt index f02e179a..eb2d2c5a 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/SendMessageDialogFragment.kt @@ -1,7 +1,5 @@ package net.lacolaco.smileessence.view.dialog -import android.app.AlertDialog -import android.app.Dialog import android.os.Bundle import android.support.v4.content.ContextCompat import android.text.Editable diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt index 757d0a0a..418ef9d3 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt @@ -1,8 +1,5 @@ package net.lacolaco.smileessence.view.dialog -import android.app.AlertDialog -import android.app.Dialog -import android.app.DialogFragment import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.view.LayoutInflater @@ -26,8 +23,10 @@ import net.lacolaco.smileessence.util.launchBg import net.lacolaco.smileessence.util.launchUi import net.lacolaco.smileessence.view.DialogHelper import net.lacolaco.smileessence.view.Partials +import net.lacolaco.smileessence.view.PopupMenu import net.lacolaco.smileessence.view.adapter.TimelineAdapter import net.lacolaco.smileessence.view.adapter.UnorderedCustomListAdapter +import net.lacolaco.smileessence.view.confirm import org.jetbrains.anko.browse import java.util.* @@ -185,30 +184,27 @@ class StatusDetailDialogFragment : StackableDialogFragment() { }) } view.button_status_detail_menu.setOnClickListener { - val builder = AlertDialog.Builder(activity) - builder.setTitle("@" + tweet.user.screenName + ": " + tweet.text) - .setItems(R.array.status_commands) { dialog, which -> - when (which) { - 0 -> { - val text = String.format("@%s ", tweet.originalTweet.user.screenName) - world.postState.beginTransaction().insertText(0, text).moveCursor(text.length).commit() - world.notify(R.string.notice_add_to_reply) - } - 1 -> DialogHelper.showDialog(activity, TalkChainDialogFragment.newInstance(tweet)) - 2 -> browse(tweet.originalTweet.twitterUrl) - 3 -> { - SystemServiceHelper.copyToClipboard(activity, "tweet text", tweet.originalTweet.text) - world.notify(R.string.notice_copy_clipboard) - } - 4 -> { - SystemServiceHelper.copyToClipboard(activity, "tweet url", tweet.originalTweet.twitterUrl) - world.notify(R.string.notice_copy_clipboard) - } - else -> throw IllegalStateException() - } - } - val dialog = builder.create() - dialog.show() + val popup = PopupMenu(activity, view.button_status_detail_menu) + popup.add(R.string.command_status_add_to_reply) { + val text = String.format("@%s ", tweet.originalTweet.user.screenName) + world.postState.beginTransaction().insertText(0, text).moveCursor(text.length).commit() + world.notify(R.string.notice_add_to_reply) + } + popup.add(R.string.command_status_open_talk_view) { + DialogHelper.showDialog(activity, TalkChainDialogFragment.newInstance(tweet)) + } + popup.add(R.string.command_status_open_in_browser) { + browse(tweet.originalTweet.twitterUrl) + } + popup.add(R.string.command_status_copy_text_to_clipboard) { + SystemServiceHelper.copyToClipboard(activity, "tweet text", tweet.originalTweet.text) + world.notify(R.string.notice_copy_clipboard) + } + popup.add(R.string.command_status_copy_url_to_clipboard) { + SystemServiceHelper.copyToClipboard(activity, "tweet url", tweet.originalTweet.twitterUrl) + world.notify(R.string.notice_copy_clipboard) + } + popup.show() } } @@ -249,7 +245,7 @@ class StatusDetailDialogFragment : StackableDialogFragment() { } private fun confirm(onYes: () -> Unit) { - ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands), onYes) + confirm(R.string.dialog_confirm_commands, onOk = onYes) } companion object { diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt index f2eaa6de..838911cf 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt @@ -1,6 +1,5 @@ package net.lacolaco.smileessence.view.dialog -import android.app.AlertDialog import android.os.Bundle import android.text.Html import android.text.TextUtils @@ -9,75 +8,80 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection +import kotlinx.android.synthetic.main.dialog_user_detail.* import kotlinx.android.synthetic.main.dialog_user_detail.view.* import net.lacolaco.smileessence.R import net.lacolaco.smileessence.data.ImageCache import net.lacolaco.smileessence.entity.User import net.lacolaco.smileessence.twitter.TwitterTaskException import net.lacolaco.smileessence.twitter.task.* -import net.lacolaco.smileessence.util.launchBg import net.lacolaco.smileessence.util.launchUi import net.lacolaco.smileessence.view.DialogHelper +import net.lacolaco.smileessence.view.PopupMenu import net.lacolaco.smileessence.view.ThreeStateButton import net.lacolaco.smileessence.view.adapter.TimelineAdapter +import net.lacolaco.smileessence.view.confirm import org.jetbrains.anko.browse class UserDetailDialogFragment : StackableDialogFragment() { private val adapter by lazy { TimelineAdapter(activity, world) } private lateinit var user: User - private fun showPopupMenu(v: View) { - val builder = AlertDialog.Builder(activity) - builder.setTitle("@" + user.screenName) - .setItems(R.array.user_commands) { _, which -> - when (which) { - 0 -> { - val text = String.format("@%s ", user.screenName) - world.postState.beginTransaction().insertText(0, text).moveCursor(text.length).commit() - world.notify(R.string.notice_add_to_reply) - } - 1 -> DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(user)) - 2 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { - launchUi { - try { - world.blockAsync(user.id).await() - world.notify(R.string.notice_block_succeeded) - updateRelationship(v) - } catch (e: TwitterTaskException) { - world.notifyError(R.string.notice_block_failed) - } - } - } - 3 -> launchBg { - try { - world.unblockAsync(user.id).await() - world.notify(R.string.notice_unblock_succeeded) - updateRelationship(v) - } catch (e: TwitterTaskException) { - world.notifyError(R.string.notice_unblock_failed) - } - } - 4 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { - launchBg { - try { - world.reportSpamAsync(user.id).await() - world.notify(R.string.notice_r4s_succeeded) - updateRelationship(v) - } catch (e: TwitterTaskException) { - world.notifyError(R.string.notice_r4s_failed) - } - } - } - 5 -> browse(user.aclogTimelineURL) - else -> throw IllegalStateException() + private fun showPopupMenu() { + val popup = PopupMenu(activity, imageview_user_detail_menu) + popup.add(R.string.command_user_add_to_reply) { + val text = String.format("@%s ", user.screenName) + world.postState.beginTransaction().insertText(0, text).moveCursor(text.length).commit() + world.notify(R.string.notice_add_to_reply) + } + popup.add(R.string.command_user_send_message) { + DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(user)) + } + popup.add(R.string.command_user_block) { + confirm(R.string.dialog_confirm_commands) { + launchUi { + try { + world.blockAsync(user.id).await() + world.notify(R.string.notice_block_succeeded) + updateRelationship() + } catch (e: TwitterTaskException) { + world.notifyError(R.string.notice_block_failed) + } + } + } + } + popup.add(R.string.command_user_unblock) { + launchUi { + try { + world.unblockAsync(user.id).await() + world.notify(R.string.notice_unblock_succeeded) + updateRelationship() + } catch (e: TwitterTaskException) { + world.notifyError(R.string.notice_unblock_failed) + } + } + } + popup.add(R.string.command_user_r4s) { + confirm(R.string.dialog_confirm_commands) { + launchUi { + try { + world.reportSpamAsync(user.id).await() + world.notify(R.string.notice_r4s_succeeded) + updateRelationship() + } catch (e: TwitterTaskException) { + world.notifyError(R.string.notice_r4s_failed) } } - val dialog = builder.create() - dialog.show() + } + } + popup.add(R.string.command_user_open_aclog) { + browse(user.aclogTimelineURL) + } + popup.show() } private fun toggleFollowing(v: View) { - ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { + confirm(R.string.dialog_confirm_commands) { val isFollowing = v.button_user_detail_follow.state == ThreeStateButton.STATE_ON v.button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED launchUi { @@ -89,7 +93,7 @@ class UserDetailDialogFragment : StackableDialogFragment() { world.followAsync(user.id).await() world.notify(R.string.notice_follow_succeeded) } - updateRelationship(v) + updateRelationship() } catch (e: TwitterTaskException) { world.notifyError(if (isFollowing) R.string.notice_unfollow_failed @@ -113,7 +117,7 @@ class UserDetailDialogFragment : StackableDialogFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val v = inflater.inflate(R.layout.dialog_user_detail, container, false) - v.imageview_user_detail_menu.setOnClickListener { showPopupMenu(v) } + v.imageview_user_detail_menu.setOnClickListener { showPopupMenu() } v.textview_user_detail_screenname.setOnClickListener { browse(user.userHomeURL) } v.textview_user_detail_description.movementMethod = LinkMovementMethod.getInstance() v.textview_user_detail_tweet_count.setOnClickListener { browse(user.userHomeURL) } @@ -154,11 +158,15 @@ class UserDetailDialogFragment : StackableDialogFragment() { v.listview_user_detail_timeline.adapter = adapter executeUserTimelineTask(v, adapter) - updateRelationship(v) return v } + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + updateRelationship() + } + private fun executeUserTimelineTask(v: View, adapter: TimelineAdapter) { launchUi { v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.GONE @@ -220,21 +228,23 @@ class UserDetailDialogFragment : StackableDialogFragment() { } } - private fun updateRelationship(v: View) { + private fun updateRelationship() { val account = world.account if (user === account.user) { - v.textview_user_detail_followed.setText(R.string.user_detail_followed_is_me) - v.button_user_detail_follow.visibility = View.GONE + textview_user_detail_followed.setText(R.string.user_detail_followed_is_me) + button_user_detail_follow.visibility = View.GONE } else { - v.button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED + button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED launchUi { - v.textview_user_detail_followed.setText(R.string.user_detail_loading) + textview_user_detail_followed.setText(R.string.user_detail_loading) try { val relationship = world.getRelationshipAsync(user.id).await() val isFollowing = relationship.isSourceFollowingTarget val isFollowed = relationship.isSourceFollowedByTarget - v.button_user_detail_follow.state = if (isFollowing) ThreeStateButton.STATE_ON else ThreeStateButton.STATE_OFF - v.textview_user_detail_followed.setText(if (isFollowed) R.string.user_detail_followed else R.string.user_detail_not_followed) + button_user_detail_follow.state = + if (isFollowing) ThreeStateButton.STATE_ON + else ThreeStateButton.STATE_OFF + textview_user_detail_followed.setText(if (isFollowed) R.string.user_detail_followed else R.string.user_detail_not_followed) } catch (e: TwitterTaskException) { world.notifyError("Failed to fetch relationship") } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/page/SearchFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/page/SearchFragment.kt index 2e751bc8..c57d92fb 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/page/SearchFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/page/SearchFragment.kt @@ -1,6 +1,5 @@ package net.lacolaco.smileessence.view.page -import android.app.AlertDialog import android.os.Bundle import android.text.Spannable import android.text.TextUtils @@ -23,12 +22,13 @@ import net.lacolaco.smileessence.twitter.task.doSearch import net.lacolaco.smileessence.util.SystemServiceHelper import net.lacolaco.smileessence.util.UIHandler import net.lacolaco.smileessence.util.launchUi +import net.lacolaco.smileessence.view.PopupMenu import net.lacolaco.smileessence.view.adapter.TimelineAdapter -import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment +import net.lacolaco.smileessence.view.confirm import twitter4j.Query import java.util.* -class SearchFragment : CustomListFragment<TimelineAdapter>(), View.OnClickListener, View.OnLongClickListener, View.OnFocusChangeListener { +class SearchFragment : CustomListFragment<TimelineAdapter>() { override val adapter by lazy { TimelineAdapter(activity, world) } private lateinit var queryString: String @@ -44,52 +44,6 @@ class SearchFragment : CustomListFragment<TimelineAdapter>(), View.OnClickListen } } - override fun onClick(v: View) { - when (v.id) { - R.id.button_search_queries -> { - openSearchQueryDialog() - } - R.id.button_search_execute -> { - search() - } - R.id.button_search_save -> { - saveQuery() - } - } - } - - override fun onLongClick(v: View): Boolean { - when (v.id) { - R.id.button_search_save -> { - val text = edittext_search.text.toString() - for (ss in world.savedSearches.values) { - if (ss.query == text) { - ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_delete_query), { - launchUi { - try { - world.destroySavedSearchAsync(ss.id).await() - world.notify(R.string.notice_search_query_deleted) - world.refreshSavedSearches() - } catch (e: TwitterTaskException) { - world.notifyError("unable to delete search query") - } - } - }, false) - break - } - } - return true - } - } - return false - } - - override fun onFocusChange(v: View, hasFocus: Boolean) { - if (!hasFocus) { - SystemServiceHelper.hideIM(activity, edittext_search) - } - } - override fun onSwipeDown(view: SwipyRefreshLayout) { if (TextUtils.isEmpty(queryString)) { UIHandler().post { @@ -139,10 +93,42 @@ class SearchFragment : CustomListFragment<TimelineAdapter>(), View.OnClickListen override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - button_search_queries.setOnClickListener(this) - button_search_execute.setOnClickListener(this) - button_search_save.setOnClickListener(this) - edittext_search.onFocusChangeListener = this + button_search_queries.setOnClickListener { + val popup = PopupMenu(activity, button_search_queries) + val sss = ArrayList(world.savedSearches.values) + for (ss in sss) { + popup.add(ss.query) { (activity as MainActivity).openSearchPage(ss.query) } + } + popup.show() + } + button_search_execute.setOnClickListener { + search() + } + button_search_save.setOnClickListener { + saveQuery() + } + button_search_save.setOnLongClickListener { + val text = edittext_search.text.toString() + val ss = world.savedSearches.values.find { it.query == text } + if (ss != null) { + confirm(R.string.dialog_confirm_delete_query) { + launchUi { + try { + world.destroySavedSearchAsync(ss.id).await() + world.notify(R.string.notice_search_query_deleted) + world.refreshSavedSearches() + } catch (e: TwitterTaskException) { + world.notifyError("unable to delete search query") + } + } + } + } + true + } + edittext_search.setOnFocusChangeListener { _, hasFocus -> + if (!hasFocus) + SystemServiceHelper.hideIM(activity, edittext_search) + } edittext_search.setText(queryString) edittext_search.setOnEditorActionListener { textView, i, keyEvent -> if (i == EditorInfo.IME_ACTION_SEARCH || keyEvent != null && @@ -171,17 +157,6 @@ class SearchFragment : CustomListFragment<TimelineAdapter>(), View.OnClickListen world.notifyError(R.string.notice_search_text_empty) } - private fun openSearchQueryDialog() { - val sss = ArrayList(world.savedSearches.values) - val builder = AlertDialog.Builder(activity) - builder.setItems(sss.map { it.query }.toTypedArray()) { dialog, which -> - val ss = sss[which] - (activity as MainActivity).openSearchPage(ss.query) - } - val dialog = builder.create() - dialog.show() - } - private fun saveQuery() = launchUi { val text = edittext_search.text.toString() if (TextUtils.isEmpty(text)) { diff --git a/app/src/main/java/net/lacolaco/smileessence/view/page/UserListFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/page/UserListFragment.kt index 585c8fe3..2bedf71d 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/page/UserListFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/page/UserListFragment.kt @@ -1,6 +1,5 @@ package net.lacolaco.smileessence.view.page -import android.app.AlertDialog import android.os.Bundle import android.text.TextUtils import android.view.LayoutInflater @@ -17,9 +16,10 @@ import net.lacolaco.smileessence.twitter.TwitterTaskException import net.lacolaco.smileessence.twitter.task.getListTimelineAsync import net.lacolaco.smileessence.util.UIHandler import net.lacolaco.smileessence.util.launchUi +import net.lacolaco.smileessence.view.PopupMenu import net.lacolaco.smileessence.view.adapter.TimelineAdapter -class UserListFragment : CustomListFragment<TimelineAdapter>(), View.OnClickListener { +class UserListFragment : CustomListFragment<TimelineAdapter>() { override val adapter by lazy { TimelineAdapter(activity, world) } private var listFullName: String? = null @@ -36,15 +36,6 @@ class UserListFragment : CustomListFragment<TimelineAdapter>(), View.OnClickList } } - override fun onClick(v: View) { - val id = v.id - when (id) { - R.id.button_userlist_lists -> { - openUserListsDialog() - } - } - } - override fun onSwipeDown(view: SwipyRefreshLayout) { if (listFullName == null) { UIHandler().post { @@ -79,27 +70,23 @@ class UserListFragment : CustomListFragment<TimelineAdapter>(), View.OnClickList override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - button_userlist_lists.setOnClickListener(this) - textview_userlist_name.text = if (listFullName != null) listFullName else "<none>" + button_userlist_lists.setOnClickListener { + val popup = PopupMenu(activity, button_userlist_lists) + for (name in world.listSubscriptions) { + popup.add(name) { + textview_userlist_name.text = name + (activity as MainActivity).openUserListPage(name) + } + } + popup.show() + } + textview_userlist_name.text = if (listFullName != null) listFullName else "<none selected>" } private fun notifyTextEmpty() { world.notifyError(R.string.notice_userlist_not_selected) } - private fun openUserListsDialog() { - val ary = world.listSubscriptions.toTypedArray<String>() - val builder = AlertDialog.Builder(activity) - builder.setTitle(R.string.dialog_title_select_userlist) - .setItems(ary) { dialog, which -> - val activity = activity as MainActivity - activity.openUserListPage(ary[which]) - textview_userlist_name.text = ary[which] - } - val dialog = builder.create() - dialog.show() - } - fun startUserList(listFullName: String) { UserPreferenceHelper.instance[R.string.key_last_used_user_list] = listFullName val adapter = adapter |