This repository has been archived on 2020-11-15. You can view files and clone it, but cannot push or open issues or pull requests.
hashTagItNow/app/src/main/java/com/eleith/hashtagit/SubmissionFragment.kt

103 lines
3.5 KiB
Kotlin
Raw Normal View History

package com.eleith.hashtagit
2019-01-22 00:04:50 +00:00
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.app.Activity
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
2019-01-22 00:28:05 +00:00
import kotlinx.android.synthetic.main.fragment_submission.*
2019-01-22 00:04:50 +00:00
class SubmissionFragment : Fragment() {
// going from portrait to landscape will cause things to be called twice
// so need to make check with orientation
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2019-01-22 00:28:05 +00:00
return inflater.inflate(R.layout.fragment_submission, container, false)
2019-01-22 00:04:50 +00:00
}
override fun onStart() {
super.onStart()
(activity as? HashTagItActivity)?.showToolbar(false)
}
override fun onStop() {
super.onStop()
(activity as? HashTagItActivity)?.showToolbar(true)
}
2019-01-22 00:04:50 +00:00
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if (activity?.requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE) {
arguments?.apply {
val hash = "#"
val submission = SubmissionFragmentArgs.fromBundle(this).writtenText
textView.text = String.format("%s%s", hash, submission)
2019-01-25 13:14:53 +00:00
textView.measure(0, 0)
2019-01-22 00:04:50 +00:00
putIntoLeanBackMode()
val difference = getTextViewWidthDifferenceFromParent(textView, activity)
when (Bounds.toBounds(difference)) {
Bounds.OUT_OF_BOUNDS -> {
val abs = Math.abs(difference)
2019-01-25 13:14:53 +00:00
ObjectAnimator.ofInt(horizontalScroll, "scrollX", abs).apply {
// how big the word will easy determines length of animation
// for readability
duration = (10.0 * abs).toLong()
repeatMode = ValueAnimator.REVERSE
repeatCount = ValueAnimator.INFINITE
start()
}
2019-01-22 00:04:50 +00:00
}
Bounds.IN_BOUNDS -> {
}
}
}
} else {
activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
}
}
// https://developer.android.com/training/system-ui/immersive#leanback
private fun putIntoLeanBackMode() {
2019-01-25 13:14:53 +00:00
activity?.window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN
2019-01-22 00:04:50 +00:00
}
private fun getTextViewWidthDifferenceFromParent(tv: TextView, act: Activity?): Int {
val displayMetrics = DisplayMetrics()
act?.windowManager?.defaultDisplay?.getMetrics(displayMetrics)
val width = displayMetrics.widthPixels
return width - tv.measuredWidth
}
enum class Bounds {
IN_BOUNDS,
OUT_OF_BOUNDS;
companion object {
fun toBounds(int: Int): Bounds {
if (int < 0) {
return Bounds.OUT_OF_BOUNDS
}
return Bounds.IN_BOUNDS
}
}
}
}