/* * The MIT License (MIT) * * Copyright (c) 2012-2014 lacolaco.net * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package net.lacolaco.smileessence.view.page import android.app.AlertDialog import android.os.Bundle import android.text.Spannable import android.text.TextUtils import android.text.method.ArrowKeyMovementMethod import android.view.KeyEvent import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.inputmethod.EditorInfo import android.widget.EditText import android.widget.ImageButton import android.widget.ListView import android.widget.TextView import com.handmark.pulltorefresh.library.PullToRefreshBase import com.handmark.pulltorefresh.library.PullToRefreshListView import net.lacolaco.smileessence.R import net.lacolaco.smileessence.activity.MainActivity import net.lacolaco.smileessence.preference.InternalPreferenceHelper import net.lacolaco.smileessence.twitter.task.Searches import net.lacolaco.smileessence.util.SystemServiceHelper import net.lacolaco.smileessence.util.UIHandler import net.lacolaco.smileessence.view.adapter.TimelineAdapter import net.lacolaco.smileessence.view.dialog.ConfirmDialogFragment import twitter4j.Query import java.util.ArrayList class SearchFragment : CustomListFragment(), View.OnClickListener, View.OnLongClickListener, View.OnFocusChangeListener { // ------------------------------ FIELDS ------------------------------ private lateinit var queryString: String private var editText: EditText? = null // --------------------- GETTER / SETTER METHODS --------------------- override val refreshMode: PullToRefreshBase.Mode get() = PullToRefreshBase.Mode.BOTH // ------------------------ INTERFACE METHODS ------------------------ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) queryString = InternalPreferenceHelper.instance.get(R.string.key_last_used_search_query, "") adapter = TimelineAdapter(activity) refresh() } override fun refresh() { //TODO if (!TextUtils.isEmpty(queryString)) { startSearch(queryString) } } // --------------------- Interface OnClickListener --------------------- 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!!.text.toString() for (ss in world.savedSearches.values) { if (ss.query == text) { ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_delete_query), { Searches.DestroySavedSearchTask(world.account, ss.id) .onDoneUI { x -> world.notify(R.string.notice_search_query_deleted) world.refreshSavedSearches() } .onFailUI { x -> world.notify("unable to delete search query") } .execute() }, false) break } } return true } } return false } // --------------------- Interface OnFocusChangeListener --------------------- override fun onFocusChange(v: View, hasFocus: Boolean) { if (!hasFocus) { SystemServiceHelper.hideIM(activity, editText) } } // --------------------- Interface OnRefreshListener2 --------------------- override fun onPullDownToRefresh(refreshView: PullToRefreshBase) { if (TextUtils.isEmpty(queryString)) { UIHandler().post { notifyTextEmpty() refreshView.onRefreshComplete() } return } val query = Query() query.query = queryString query.count = 200 query.resultType = Query.RECENT if (adapter.count > 0) { query.sinceId = adapter.topID } runRefreshTask(query, { updateListViewWithNotice(refreshView.refreshableView, true) refreshView.onRefreshComplete() }) } override fun onPullUpToRefresh(refreshView: PullToRefreshBase) { if (TextUtils.isEmpty(queryString)) { UIHandler().post { notifyTextEmpty() refreshView.onRefreshComplete() } return } val query = Query() query.query = queryString query.count = 200 query.resultType = Query.RECENT if (adapter.count > 0) { query.maxId = adapter.lastID - 1 } runRefreshTask(query, { updateListViewWithNotice(refreshView.refreshableView, false) refreshView.onRefreshComplete() }) } // ------------------------ OVERRIDE METHODS ------------------------ override fun getListView(page: View): PullToRefreshListView { return page.findViewById(R.id.listview_search) as PullToRefreshListView } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View { val page = inflater.inflate(R.layout.fragment_search, container, false) val listView = getListView(page) listView.setAdapter(adapter) listView.setOnScrollListener(this) listView.setOnRefreshListener(this) listView.mode = refreshMode val buttonQueries = getQueriesButton(page) buttonQueries.setOnClickListener(this) val buttonExecute = getExecuteButton(page) buttonExecute.setOnClickListener(this) val buttonSave = getSaveButton(page) buttonSave.setOnClickListener(this) editText = getEditText(page) editText!!.onFocusChangeListener = this editText!!.setText(queryString) editText!!.setOnEditorActionListener { textView, i, keyEvent -> if (i == EditorInfo.IME_ACTION_SEARCH || keyEvent != null && keyEvent.action == KeyEvent.ACTION_DOWN && keyEvent.keyCode == KeyEvent.KEYCODE_ENTER) { search() } true } editText!!.movementMethod = object : ArrowKeyMovementMethod() { override fun right(widget: TextView, buffer: Spannable): Boolean { //Don't move page return widget.selectionEnd == widget.length() || super.right(widget, buffer) } override fun left(widget: TextView, buffer: Spannable): Boolean { //Don't move page return widget.selectionStart == 0 || super.left(widget, buffer) } } return page } private fun getEditText(page: View): EditText { return page.findViewById(R.id.edittext_search) as EditText } private fun getExecuteButton(page: View): ImageButton { return page.findViewById(R.id.button_search_execute) as ImageButton } private fun getQueriesButton(page: View): ImageButton { return page.findViewById(R.id.button_search_queries) as ImageButton } private fun getSaveButton(page: View): ImageButton { return page.findViewById(R.id.button_search_save) as ImageButton } private fun notifyTextEmpty() { 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() { val text = editText!!.text.toString() if (TextUtils.isEmpty(text)) { world.notifyError(R.string.notice_query_is_empty) } else { Searches.CreateSavedSearchTask(world.account, text).onDoneUI { cb -> world.notify(R.string.notice_query_saved) world.refreshSavedSearches() }.onFailUI { ex -> world.notifyError("Query is not saved") }.execute() } } private fun search() { if (editText != null) { val text = editText!!.text.toString() if (TextUtils.isEmpty(text)) { world.notifyError(R.string.notice_query_is_empty) } else { startSearch(text) SystemServiceHelper.hideIM(activity, editText) } } } fun startSearch(queryString: String) { InternalPreferenceHelper.instance.set(R.string.key_last_used_search_query, queryString) if (editText != null) editText!!.setText(queryString) this.queryString = queryString adapter.clear() adapter.updateForce() if (!TextUtils.isEmpty(queryString)) { val query = Query() query.query = queryString query.count = 200 query.resultType = Query.RECENT runRefreshTask(query) { adapter.updateForce() } } } private fun runRefreshTask(query: Query, onFinish: () -> Unit) { Searches.SearchTask(world.account, query) .onFail { x -> world.notifyError(R.string.notice_error_search) } .onDoneUI { tweets -> adapter.addAll(tweets.filter { t -> !t.isRetweet }) } .onFinishUI(onFinish) .execute() } }