Direction arrow for "Live now" screen in progress

This commit is contained in:
Alex Sytnyk 2018-07-06 15:05:19 +03:00
parent 3162283ce0
commit f59176caec
3 changed files with 106 additions and 19 deletions

View file

@ -48,10 +48,53 @@
app:typeface="@string/font_roboto_regular"
tools:text="Share location"/>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/description"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/location_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="@+id/direction_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="4dp"
android:layout_marginRight="4dp"
tools:src="@drawable/ic_direction_arrow"
tools:tint="@color/ctrl_active_light"/>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/distance_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textSize="@dimen/list_item_description_text_size"
app:typeface="@string/font_roboto_medium"
tools:text="213 m"
tools:textColor="@color/ctrl_active_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:text="•"
android:textColor="?attr/android:textColorSecondary"
android:textSize="@dimen/list_item_description_text_size"/>
</LinearLayout>
<net.osmand.telegram.ui.views.TextViewEx
android:id="@+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:textColor="?attr/android:textColorSecondary"
@ -61,6 +104,8 @@
</LinearLayout>
</LinearLayout>
<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"

View file

@ -16,13 +16,14 @@ import android.widget.TextView
import net.osmand.Location
import net.osmand.telegram.R
import net.osmand.telegram.TelegramApplication
import net.osmand.telegram.TelegramLocationProvider.TelegramCompassListener
import net.osmand.telegram.TelegramLocationProvider.TelegramLocationListener
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.helpers.TelegramHelper.*
import net.osmand.telegram.helpers.TelegramUiHelper
import net.osmand.telegram.helpers.TelegramUiHelper.LocationItem
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandFormatter
import net.osmand.telegram.utils.UiUtils.UpdateLocationViewCache
import net.osmand.util.MapUtils
import org.drinkless.td.libcore.telegram.TdApi
@ -30,7 +31,7 @@ private const val CHAT_VIEW_TYPE = 0
private const val LOCATION_ITEM_VIEW_TYPE = 1
class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessagesListener,
TelegramLocationListener {
TelegramLocationListener, TelegramCompassListener {
private val app: TelegramApplication
get() = activity?.application as TelegramApplication
@ -40,8 +41,11 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private val settings get() = app.settings
private lateinit var adapter: LiveNowListAdapter
private lateinit var locationViewCache: UpdateLocationViewCache
private var location: Location? = null
private var heading: Float? = null
private var locationUiUpdateAllowed: Boolean = true
override fun onCreateView(
inflater: LayoutInflater,
@ -53,12 +57,19 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
mainView.findViewById<RecyclerView>(R.id.recycler_view).apply {
layoutManager = LinearLayoutManager(context)
adapter = this@LiveNowTabFragment.adapter
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
locationUiUpdateAllowed = newState == RecyclerView.SCROLL_STATE_IDLE
}
})
}
return mainView
}
override fun onResume() {
super.onResume()
locationViewCache = app.uiUtils.getUpdateLocationViewCache()
updateList()
telegramHelper.addIncomingMessagesListener(this)
startLocationUpdate()
@ -124,17 +135,33 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
}
}
override fun updateCompassValue(value: Float) {
// 99 in next line used to one-time initialize arrows (with reference vs. fixed-north direction)
// on non-compass devices
val lastHeading = heading ?: 99f
heading = value
if (Math.abs(MapUtils.degreesDiff(lastHeading.toDouble(), value.toDouble())) > 5) {
updateLocationUi()
} else {
heading = lastHeading
}
}
fun startLocationUpdate() {
app.locationProvider.addLocationListener(this)
app.locationProvider.addCompassListener(this)
updateLocationUi()
}
fun stopLocationUpdate() {
app.locationProvider.removeLocationListener(this)
app.locationProvider.removeCompassListener(this)
}
private fun updateLocationUi() {
adapter.notifyDataSetChanged()
if (locationUiUpdateAllowed) {
app.runInUIThread { adapter.notifyDataSetChanged() }
}
}
private fun updateList() {
@ -219,14 +246,19 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
TelegramUiHelper.setupPhoto(app, holder.icon, item.photoPath, item.placeholderId)
holder.title?.text = item.name
if (location != null) {
val dist = MapUtils.getDistance(
location!!.latitude, location!!.longitude,
item.lat, item.lon
).toFloat()
holder.description?.text = OsmandFormatter.getFormattedDistance(dist, app)
holder.locationViewContainer?.visibility = View.VISIBLE
// TODO: locationViewCache.outdatedLocation
app.uiUtils.updateLocationView(
holder.directionIcon,
holder.distanceText,
location!!.latitude,
location!!.longitude,
locationViewCache
)
} else {
holder.description?.text = "Current location is not available"
holder.locationViewContainer?.visibility = View.GONE
}
holder.description?.text = "Some description"
holder.bottomShadow?.visibility = if (lastItem) View.VISIBLE else View.GONE
}
}
@ -286,6 +318,9 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
inner class ContactViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val icon: ImageView? = view.findViewById(R.id.icon)
val title: TextView? = view.findViewById(R.id.title)
val locationViewContainer: View? = view.findViewById(R.id.location_view_container)
val directionIcon: ImageView? = view.findViewById(R.id.direction_icon)
val distanceText: TextView? = view.findViewById(R.id.distance_text)
val description: TextView? = view.findViewById(R.id.description)
val bottomShadow: View? = view.findViewById(R.id.bottom_shadow)
}

View file

@ -58,6 +58,7 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
private val listeners: MutableList<WeakReference<TelegramListener>> = mutableListOf()
private var myLocationTabFragment: MyLocationTabFragment? = null
private var liveNowTabFragment: LiveNowTabFragment? = null
private lateinit var buttonsBar: LinearLayout
private lateinit var bottomNav: BottomNavigationView
@ -82,6 +83,10 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
R.id.action_live_now -> pos = LIVE_NOW_TAB_POS
}
if (pos != -1 && pos != viewPager.currentItem) {
when (pos) {
MY_LOCATION_TAB_POS -> liveNowTabFragment?.stopLocationUpdate()
LIVE_NOW_TAB_POS -> liveNowTabFragment?.startLocationUpdate()
}
viewPager.currentItem = pos
return@setOnNavigationItemSelectedListener true
}
@ -137,6 +142,8 @@ class MainActivity : AppCompatActivity(), TelegramListener, ActionButtonsListene
}
if (fragment is MyLocationTabFragment) {
myLocationTabFragment = fragment
} else if (fragment is LiveNowTabFragment) {
liveNowTabFragment = fragment
}
}