From e3ead28f5943578d9f7f6071d4c46db1270f809b Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 13 Jul 2018 18:19:09 +0300 Subject: [PATCH 1/3] Add the ability to stop sharing location from "My location" screen --- .../telegram/ui/MyLocationTabFragment.kt | 58 ++++++++++++++----- .../telegram/ui/SetTimeDialogFragment.kt | 9 ++- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt index f205c88646..137456d440 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MyLocationTabFragment.kt @@ -1,6 +1,7 @@ package net.osmand.telegram.ui import android.animation.* +import android.content.Intent import android.graphics.Paint import android.graphics.drawable.GradientDrawable import android.os.Build @@ -156,6 +157,13 @@ class MyLocationTabFragment : Fragment(), TelegramListener { outState.putLongArray(SELECTED_CHATS_KEY, selectedChats.toLongArray()) } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + if (requestCode == SetTimeDialogFragment.LOCATION_SHARED_REQUEST_CODE) { + clearSelection() + } + } + override fun onTelegramStatusChanged( prevTelegramAuthorizationState: TelegramHelper.TelegramAuthorizationState, newTelegramAuthorizationState: TelegramHelper.TelegramAuthorizationState @@ -195,12 +203,15 @@ class MyLocationTabFragment : Fragment(), TelegramListener { } fun onPrimaryBtnClick() { - activity?.supportFragmentManager?.also { - SetTimeDialogFragment.showInstance(it, selectedChats) - } + val fm = fragmentManager ?: return + SetTimeDialogFragment.showInstance(fm, selectedChats, this) } fun onSecondaryBtnClick() { + clearSelection() + } + + private fun clearSelection() { selectedChats.clear() adapter.notifyDataSetChanged() actionButtonsListener?.switchButtonsVisibility(false) @@ -344,27 +355,43 @@ class MyLocationTabFragment : Fragment(), TelegramListener { override fun onBindViewHolder(holder: ChatViewHolder, position: Int) { val chat = chats[position] val lastItem = position == itemCount - 1 + val live = app.settings.isSharingLocationToChat(chat.id) TelegramUiHelper.setupPhoto(app, holder.icon, chat.photo?.small?.local?.path) holder.title?.text = chat.title holder.description?.text = "Some description" // FIXME - holder.checkBox?.apply { - visibility = View.VISIBLE - setOnCheckedChangeListener(null) - isChecked = selectedChats.contains(chat.id) - setOnCheckedChangeListener { _, isChecked -> - if (isChecked) { - selectedChats.add(chat.id) - } else { - selectedChats.remove(chat.id) + if (live) { + holder.checkBox?.visibility = View.GONE + holder.textInArea?.visibility = View.VISIBLE + holder.textInArea?.text = getString(R.string.shared_string_live) + } else { + holder.textInArea?.visibility = View.GONE + holder.checkBox?.apply { + visibility = View.VISIBLE + setOnCheckedChangeListener(null) + isChecked = selectedChats.contains(chat.id) + setOnCheckedChangeListener { _, isChecked -> + if (isChecked) { + selectedChats.add(chat.id) + } else { + selectedChats.remove(chat.id) + } + actionButtonsListener?.switchButtonsVisibility(selectedChats.isNotEmpty()) } - actionButtonsListener?.switchButtonsVisibility(selectedChats.isNotEmpty()) } } holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE holder.itemView.setOnClickListener { - holder.checkBox?.apply { - isChecked = !isChecked + if (live) { + app.settings.shareLocationToChat(chat.id, false) + if (!app.settings.hasAnyChatToShareLocation()) { + app.shareLocationHelper.stopSharingLocation() + } + notifyItemChanged(position) + } else { + holder.checkBox?.apply { + isChecked = !isChecked + } } } } @@ -375,6 +402,7 @@ class MyLocationTabFragment : Fragment(), TelegramListener { val icon: ImageView? = view.findViewById(R.id.icon) val title: TextView? = view.findViewById(R.id.title) val description: TextView? = view.findViewById(R.id.description) + val textInArea: TextView? = view.findViewById(R.id.text_in_area) val checkBox: CheckBox? = view.findViewById(R.id.check_box) val bottomShadow: View? = view.findViewById(R.id.bottom_shadow) } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt index fff610c7d2..97c8a51e66 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SetTimeDialogFragment.kt @@ -3,6 +3,7 @@ package net.osmand.telegram.ui import android.app.TimePickerDialog import android.os.Bundle import android.support.v4.app.DialogFragment +import android.support.v4.app.Fragment import android.support.v4.app.FragmentManager import android.support.v7.widget.LinearLayoutManager import android.support.v7.widget.RecyclerView @@ -84,6 +85,9 @@ class SetTimeDialogFragment : DialogFragment() { settings.shareLocationToChat(chatId, true, livePeriod) } app.shareLocationHelper.startSharingLocation() + targetFragment?.also { + it.onActivityResult(targetRequestCode, LOCATION_SHARED_REQUEST_CODE, null) + } dismiss() } } @@ -235,12 +239,14 @@ class SetTimeDialogFragment : DialogFragment() { companion object { + const val LOCATION_SHARED_REQUEST_CODE = 0 + private const val TAG = "SetTimeDialogFragment" private const val CHATS_KEY = "chats_key" private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour private const val NO_VALUE = -1L - fun showInstance(fm: FragmentManager, chatIds: Set): Boolean { + fun showInstance(fm: FragmentManager, chatIds: Set, target: Fragment): Boolean { return try { val chats = mutableListOf() for (id in chatIds) { @@ -249,6 +255,7 @@ class SetTimeDialogFragment : DialogFragment() { } SetTimeDialogFragment().apply { arguments = Bundle().apply { putLongArray(CHATS_KEY, chats.toLongArray()) } + setTargetFragment(target, LOCATION_SHARED_REQUEST_CODE) show(fm, TAG) } true From 03db6d689798351b584c2154acd52fb2098457ce Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 13 Jul 2018 18:27:42 +0300 Subject: [PATCH 2/3] Remove redundant code from MainActivity --- .../net/osmand/telegram/ui/MainActivity.kt | 188 +----------------- 1 file changed, 6 insertions(+), 182 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 7456d13608..42df754822 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -1,22 +1,18 @@ package net.osmand.telegram.ui -import android.Manifest import android.app.Dialog import android.content.Intent import android.content.pm.PackageManager -import android.graphics.Bitmap -import android.graphics.drawable.Drawable import android.net.Uri import android.os.Bundle import android.support.design.widget.BottomNavigationView -import android.support.v4.app.* +import android.support.v4.app.DialogFragment +import android.support.v4.app.Fragment +import android.support.v4.app.FragmentManager +import android.support.v4.app.FragmentPagerAdapter import android.support.v7.app.AlertDialog import android.support.v7.app.AppCompatActivity -import android.support.v7.widget.AppCompatImageView -import android.support.v7.widget.AppCompatTextView -import android.support.v7.widget.RecyclerView -import android.support.v7.widget.SwitchCompat -import android.view.* +import android.view.View import android.widget.LinearLayout import android.widget.TextView import android.widget.Toast @@ -34,10 +30,6 @@ import java.lang.ref.WeakReference private const val PERMISSION_REQUEST_LOCATION = 1 -private const val LOGIN_MENU_ID = 0 -private const val LOGOUT_MENU_ID = 1 -private const val PROGRESS_MENU_ID = 2 - private const val MY_LOCATION_TAB_POS = 0 private const val LIVE_NOW_TAB_POS = 1 @@ -151,9 +143,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene super.onResume() paused = false - invalidateOptionsMenu() - updateTitle() - app.locationProvider.checkIfLastKnownLocationIsValid() if (AndroidUtils.isLocationPermissionAvailable(this)) { @@ -196,8 +185,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene TelegramAuthorizationState.READY -> LoginDialogFragment.dismiss(fm) else -> Unit } - invalidateOptionsMenu() - updateTitle() listeners.forEach { it.get()?.onTelegramStatusChanged(prevTelegramAuthorizationState, newTelegramAuthorizationState) @@ -269,18 +256,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene telegramHelper.init() } - fun logoutTelegram(silent: Boolean = false) { - if (telegramHelper.getTelegramAuthorizationState() == TelegramAuthorizationState.READY) { - telegramHelper.logout() - } else { - invalidateOptionsMenu() - updateTitle() - if (!silent) { - Toast.makeText(this, R.string.not_logged_in, Toast.LENGTH_SHORT).show() - } - } - } - fun closeTelegram() { telegramHelper.close() } @@ -291,71 +266,6 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } - override fun onOptionsItemSelected(item: MenuItem?): Boolean { - return when (item?.itemId) { - LOGIN_MENU_ID -> { - loginTelegram() - true - } - LOGOUT_MENU_ID -> { - logoutTelegram() - true - } - else -> super.onOptionsItemSelected(item) - } - } - - override fun onCreateOptionsMenu(menu: Menu?): Boolean { - if (menu != null) { - menu.clear() - when (telegramHelper.getTelegramAuthorizationState()) { - TelegramAuthorizationState.UNKNOWN, - TelegramAuthorizationState.WAIT_PARAMETERS, - TelegramAuthorizationState.WAIT_PHONE_NUMBER, - TelegramAuthorizationState.WAIT_CODE, - TelegramAuthorizationState.WAIT_PASSWORD, - TelegramAuthorizationState.LOGGING_OUT, - TelegramAuthorizationState.CLOSING -> createProgressMenuItem(menu) - TelegramAuthorizationState.READY -> createMenuItem(menu, LOGOUT_MENU_ID, R.string.shared_string_logout, - MenuItem.SHOW_AS_ACTION_WITH_TEXT or MenuItem.SHOW_AS_ACTION_ALWAYS) - TelegramAuthorizationState.CLOSED -> createMenuItem(menu, LOGIN_MENU_ID, R.string.shared_string_login, - MenuItem.SHOW_AS_ACTION_WITH_TEXT or MenuItem.SHOW_AS_ACTION_ALWAYS) - } - } - return super.onCreateOptionsMenu(menu) - } - - private fun createMenuItem(m: Menu, id: Int, titleRes: Int, menuItemType: Int): MenuItem { - val menuItem = m.add(0, id, 0, titleRes) - menuItem.setOnMenuItemClickListener { item -> onOptionsItemSelected(item) } - menuItem.setShowAsAction(menuItemType) - return menuItem - } - - private fun createProgressMenuItem(m: Menu): MenuItem { - - val menuItem = m.add(0, PROGRESS_MENU_ID, 0, "") - menuItem.actionView = layoutInflater.inflate(R.layout.action_progress_bar, null) - menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS) - return menuItem - } - - private fun updateTitle() { - title = when (telegramHelper.getTelegramAuthorizationState()) { - - TelegramAuthorizationState.UNKNOWN, - TelegramAuthorizationState.WAIT_PHONE_NUMBER, - TelegramAuthorizationState.WAIT_CODE, - TelegramAuthorizationState.WAIT_PASSWORD, - TelegramAuthorizationState.READY, - TelegramAuthorizationState.CLOSED -> getString(R.string.app_name) - - TelegramAuthorizationState.WAIT_PARAMETERS -> getString(R.string.initialization) + "..." - TelegramAuthorizationState.LOGGING_OUT -> getString(R.string.logging_out) + "..." - TelegramAuthorizationState.CLOSING -> getString(R.string.closing) + "..." - } - } - private fun showLoginDialog(telegramAuthenticationParameterType: TelegramAuthenticationParameterType) { when (telegramAuthenticationParameterType) { TelegramAuthenticationParameterType.PHONE_NUMBER -> LoginDialogFragment.showDialog(supportFragmentManager, LoginDialogType.ENTER_PHONE_NUMBER) @@ -396,7 +306,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene } } - fun showOsmandMissingDialog() { + private fun showOsmandMissingDialog() { OsmandMissingDialogFragment().show(supportFragmentManager, null) } @@ -423,90 +333,4 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene override fun getCount() = fragments.size } - - inner class ChatsAdapter : - RecyclerView.Adapter() { - - var chats: List = emptyList() - set(value) { - field = value - notifyDataSetChanged() - } - - inner class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) { - val icon: AppCompatImageView? = view.findViewById(R.id.icon) - val groupName: AppCompatTextView? = view.findViewById(R.id.name) - val shareLocationSwitch: SwitchCompat? = view.findViewById(R.id.share_location_switch) - val showOnMapSwitch: SwitchCompat? = view.findViewById(R.id.show_on_map_switch) - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context).inflate(R.layout.chat_list_item, parent, false) - return ViewHolder(view) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val chat = chats[position] - val chatId = chat.id - holder.groupName?.text = chat.title - - var drawable: Drawable? = null - var bitmap: Bitmap? = null - val chatPhoto = chat.photo?.small - if (chatPhoto != null && chatPhoto.local.path.isNotEmpty()) { - bitmap = app.uiUtils.getCircleBitmap(chatPhoto.local.path) - } - if (bitmap == null) { - drawable = app.uiUtils.getThemedIcon(R.drawable.ic_group) - } - if (bitmap != null) { - holder.icon?.setImageBitmap(bitmap) - } else { - holder.icon?.setImageDrawable(drawable) - } - holder.shareLocationSwitch?.setOnCheckedChangeListener(null) - holder.shareLocationSwitch?.isChecked = settings.isSharingLocationToChat(chatId) - holder.shareLocationSwitch?.setOnCheckedChangeListener { view, isChecked -> - settings.shareLocationToChat(chatId, isChecked) - if (settings.hasAnyChatToShareLocation()) { - if (!AndroidUtils.isLocationPermissionAvailable(view.context)) { - if (isChecked) { - AndroidUtils.requestLocationPermission(this@MainActivity) - } - } else { - app.shareLocationHelper.startSharingLocation() - } - } else { - app.shareLocationHelper.stopSharingLocation() - } - } - - holder.showOnMapSwitch?.setOnCheckedChangeListener(null) - holder.showOnMapSwitch?.isChecked = settings.isShowingChatOnMap(chatId) - holder.showOnMapSwitch?.setOnCheckedChangeListener { _, isChecked -> - settings.showChatOnMap(chatId, isChecked) - if (settings.hasAnyChatToShowOnMap()) { - if (osmandAidlHelper.isOsmandNotInstalled()) { - if (isChecked) { - showOsmandMissingDialog() - } - } else { - if (isChecked) { - app.showLocationHelper.showChatMessages(chatId) - } else { - app.showLocationHelper.hideChatMessages(chatId) - } - app.showLocationHelper.startShowingLocation() - } - } else { - app.showLocationHelper.stopShowingLocation() - if (!isChecked) { - app.showLocationHelper.hideChatMessages(chatId) - } - } - } - } - - override fun getItemCount() = chats.size - } } From 9571c883a3be6a6fd0b7075baf12704d797f96ce Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 13 Jul 2018 18:29:42 +0300 Subject: [PATCH 3/3] Remove redundant layout file --- OsmAnd-telegram/res/layout/chat_list_item.xml | 60 ------------------- 1 file changed, 60 deletions(-) delete mode 100644 OsmAnd-telegram/res/layout/chat_list_item.xml diff --git a/OsmAnd-telegram/res/layout/chat_list_item.xml b/OsmAnd-telegram/res/layout/chat_list_item.xml deleted file mode 100644 index fefedd3125..0000000000 --- a/OsmAnd-telegram/res/layout/chat_list_item.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - \ No newline at end of file