From 8ec65f55f85c730e97d5521c1443819435b98208 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 3 Jun 2017 15:51:36 -0700 Subject: Add toolbar title listeners --- app/src/main/assets/core/login.compact.css | 18 ++++++++-------- .../kotlin/com/pitchedapps/frost/MainActivity.kt | 24 +++++++++++++++++++--- .../com/pitchedapps/frost/WebOverlayActivity.kt | 8 ++++++-- .../pitchedapps/frost/fragments/BaseFragment.kt | 5 ----- .../com/pitchedapps/frost/utils/ContextUtils.kt | 1 + .../com/pitchedapps/frost/utils/Interfaces.kt | 12 ----------- .../com/pitchedapps/frost/web/FrostChromeClient.kt | 13 ++++++++---- .../com/pitchedapps/frost/web/FrostWebView.kt | 2 ++ .../com/pitchedapps/frost/web/FrostWebViewCore.kt | 20 +++++++++++++----- app/src/main/res/layout/activity_web_overlay.xml | 10 +++++---- app/src/main/res/values/styles.xml | 2 +- 11 files changed, 70 insertions(+), 45 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/utils/Interfaces.kt (limited to 'app/src') diff --git a/app/src/main/assets/core/login.compact.css b/app/src/main/assets/core/login.compact.css index 4a99bbf3..436b1875 100644 --- a/app/src/main/assets/core/login.compact.css +++ b/app/src/main/assets/core/login.compact.css @@ -1,8 +1,8 @@ [data-sigil="m_login_upsell"] { display: none !important; } -body, #root, #header, .aclb, ._55wo, ._1upc, input, ._2f9r { background: #f00 !important; } +body, #root, #header, .aclb, ._55wo, ._1upc, input, ._2f9r { background: #000 !important; } -button::before, .touch ._56bt, ._56be::before, .btnS, .touch::before, ._5xo2, ._5u5a::before { background: rgba(255, 51, 51, 0.1) !important; } +button::before, .touch ._56bt, ._56be::before, .btnS, .touch::before, ._5xo2, ._5u5a::before { background: rgba(26, 26, 26, 0.1) !important; } ._56bf, .touch .btn { border-radius: 0 !important; border: 0 !important; } @@ -10,16 +10,16 @@ button::before, .touch ._56bt, ._56be::before, .btnS, .touch::before, ._5xo2, ._ .touch .btnS { box-shadow: none !important; } -input, ._43mh, .touch .btn, a, .fcg, button, ._52j9, ._52jb, h1, h2, h3, h4, h5, h6 { color: #f0f !important; } +input, ._43mh, .touch .btn, a, .fcg, button, ._52j9, ._52jb, h1, h2, h3, h4, h5, h6 { color: #fff !important; } -::-webkit-input-placeholder { color: #f0f !important; } +::-webkit-input-placeholder { color: #fff !important; } -:-moz-placeholder { color: #f0f !important; } +:-moz-placeholder { color: #fff !important; } -::-moz-placeholder { color: #f0f !important; } +::-moz-placeholder { color: #fff !important; } -:-ms-input-placeholder { color: #f0f !important; } +:-ms-input-placeholder { color: #fff !important; } -._43mh::before, ._43mh::after { background: #f0f !important; } +._43mh::before, ._43mh::after { background: #fff !important; } -._1rrd { border: 1px solid #f0f !important; color: #f0f !important; } +._1rrd { border: 1px solid #fff !important; color: #fff !important; } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index 047cedac..f4ffd2b1 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -11,6 +11,7 @@ import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.Menu import android.view.MenuItem +import android.view.ViewTreeObserver import butterknife.ButterKnife import co.zsmb.materialdrawerkt.builders.Builder import co.zsmb.materialdrawerkt.builders.accountHeader @@ -26,9 +27,9 @@ import com.pitchedapps.frost.events.FbAccountEvent import com.pitchedapps.frost.facebook.FbCookie.switchUser import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL -import com.pitchedapps.frost.fragments.BaseFragment import com.pitchedapps.frost.fragments.WebFragment import com.pitchedapps.frost.utils.* +import io.reactivex.disposables.Disposable import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode @@ -43,6 +44,7 @@ class MainActivity : AppCompatActivity() { lateinit var drawer: Drawer lateinit var drawerHeader: AccountHeader val cookies: ArrayList by lazy { cookies() } + var titleDisposable: Disposable? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -53,6 +55,11 @@ class MainActivity : AppCompatActivity() { viewPager.adapter = adapter viewPager.offscreenPageLimit = 5 viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + updateTitleListener() + } + override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { super.onPageScrolled(position, positionOffset, positionOffsetPixels) val delta: Float by lazy { positionOffset * (255 - 128).toFloat() } @@ -72,6 +79,17 @@ class MainActivity : AppCompatActivity() { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show() } + viewPager.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener{ + override fun onGlobalLayout() { + viewPager.viewTreeObserver.removeOnGlobalLayoutListener(this) + updateTitleListener() + } + }) + } + + fun updateTitleListener() { + titleDisposable?.dispose() + titleDisposable = currentFragment.web.addTitleListener({ toolbar.title = it }) } fun setupTabs() { @@ -144,8 +162,8 @@ class MainActivity : AppCompatActivity() { super.onBackPressed() } - val currentFragment: BaseFragment - get() = supportFragmentManager.findFragmentByTag("android:switcher:${R.id.container}:${viewPager.currentItem}") as BaseFragment + val currentFragment + get() = supportFragmentManager.findFragmentByTag("android:switcher:${R.id.container}:${viewPager.currentItem}") as WebFragment inner class SectionsPagerAdapter(fm: FragmentManager, val pages: List) : FragmentPagerAdapter(fm) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt index 9a071285..3ec47a10 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt @@ -15,14 +15,17 @@ import com.pitchedapps.frost.web.FrostWebView */ class WebOverlayActivity : AppCompatActivity() { - val toolbar: Toolbar by bindView(R.id.toolbar) - val frostWeb: FrostWebView by bindView(R.id.frost_webview) + val toolbar: Toolbar by bindView(R.id.overlay_toolbar) + val frostWeb: FrostWebView by bindView(R.id.overlay_frost_webview) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_web_overlay) ButterKnife.bind(this) setSupportActionBar(toolbar) + supportActionBar?.setDisplayShowHomeEnabled(true) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + toolbar.setNavigationOnClickListener { onBackPressed() } frostWeb.baseUrl = url() frostWeb.web.loadBaseUrl() SwipeBackHelper.onCreate(this) @@ -31,6 +34,7 @@ class WebOverlayActivity : AppCompatActivity() { .setSwipeSensitivity(0.5f) .setSwipeRelateEnable(true) .setSwipeRelateOffset(300) + frostWeb.web.addTitleListener({ toolbar.title = it }) } override fun onPostCreate(savedInstanceState: Bundle?) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt index cb3bb713..06484500 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/BaseFragment.kt @@ -1,12 +1,7 @@ package com.pitchedapps.frost.fragments -import android.content.Context import android.support.v4.app.Fragment -import com.pitchedapps.frost.utils.KeyPairObservable -import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.putInt -import io.reactivex.disposables.Disposable -import io.reactivex.functions.Consumer /** * Created by Allan Wang on 2017-05-29. diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt index 31055c4e..b87eaa8f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt @@ -30,6 +30,7 @@ fun Activity.cookies(): ArrayList { fun Context.launchWebOverlay(url: String) { val intent = Intent(this, WebOverlayActivity::class.java) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) intent.putExtra(ARG_URL, url) val bundle = ActivityOptionsCompat.makeCustomAnimation(this, R.anim.slide_in_right, R.anim.slide_out_right).toBundle() ContextCompat.startActivity(this, intent, bundle) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Interfaces.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Interfaces.kt deleted file mode 100644 index 1e42f59f..00000000 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Interfaces.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.pitchedapps.frost.utils - -import io.reactivex.subjects.Subject - -/** - * Created by Allan Wang on 2017-05-29. - */ -interface ObservableContainer { - val progressObservable: Subject -} - -interface KeyPairObservable : ObservableContainer> \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt index fe3c92e8..ff8637b1 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt @@ -4,20 +4,25 @@ import android.webkit.ConsoleMessage import android.webkit.WebChromeClient import android.webkit.WebView import com.pitchedapps.frost.utils.L +import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.Subject /** * Created by Allan Wang on 2017-05-31. */ -class FrostChromeClient(val progressObservable: Subject) : WebChromeClient() { +class FrostChromeClient(val progressObservable: Subject, val titleObservable: BehaviorSubject) : WebChromeClient() { + + override fun onConsoleMessage(consoleMessage: ConsoleMessage): Boolean { - L.d("Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}") + L.d("Chrome Console ${consoleMessage.lineNumber()}: ${consoleMessage.message()}") return super.onConsoleMessage(consoleMessage) } - override fun onReceivedTitle(view: WebView?, title: String?) { + override fun onReceivedTitle(view: WebView, title: String) { super.onReceivedTitle(view, title) - L.v("Title $title") + if (title.contains("http") || titleObservable.value == title) return +// L.v("Title $title") + titleObservable.onNext(title) } override fun onProgressChanged(view: WebView, newProgress: Int) { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt index 087e9174..d5fd6391 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebView.kt @@ -10,7 +10,9 @@ import android.widget.ProgressBar import butterknife.ButterKnife import com.pitchedapps.frost.R import com.pitchedapps.frost.utils.bindView +import io.reactivex.Scheduler import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable /** * Created by Allan Wang on 2017-06-01. diff --git a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt index b2260b6f..eaee60bc 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostWebViewCore.kt @@ -11,7 +11,9 @@ import android.view.MotionEvent import android.view.View import android.webkit.WebView import com.pitchedapps.frost.events.FbAccountEvent -import com.pitchedapps.frost.utils.ObservableContainer +import io.reactivex.Scheduler +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable import io.reactivex.subjects.BehaviorSubject import io.reactivex.subjects.Subject import org.greenrobot.eventbus.EventBus @@ -27,20 +29,25 @@ import org.greenrobot.eventbus.ThreadMode */ class FrostWebViewCore @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 -) : WebView(context, attrs, defStyleAttr), NestedScrollingChild, ObservableContainer { +) : WebView(context, attrs, defStyleAttr), NestedScrollingChild { private val childHelper = NestedScrollingChildHelper(this) private var lastY: Int = 0 private val scrollOffset = IntArray(2) private val scrollConsumed = IntArray(2) private var nestedOffsetY: Int = 0 - override val progressObservable: Subject //TODO see if we need this + val progressObservable: Subject + val titleObservable: Subject + + private val chromeClient: FrostChromeClient var baseUrl: String? = null var position: Int = -1 init { isNestedScrollingEnabled = true progressObservable = BehaviorSubject.create() + titleObservable = BehaviorSubject.create() + chromeClient = FrostChromeClient(progressObservable, titleObservable) setupWebview() } @@ -49,8 +56,8 @@ class FrostWebViewCore @JvmOverloads constructor( settings.javaScriptEnabled = true settings.domStorageEnabled = true setLayerType(View.LAYER_TYPE_HARDWARE, null) - setWebViewClient(FrostWebViewClient({position})) - setWebChromeClient(FrostChromeClient(progressObservable)) + setWebViewClient(FrostWebViewClient({ position })) + setWebChromeClient(chromeClient) } override fun loadUrl(url: String?) { @@ -60,6 +67,9 @@ class FrostWebViewCore @JvmOverloads constructor( fun loadBaseUrl() = loadUrl(baseUrl) + fun addTitleListener(subscriber: (title: String) -> Unit, scheduler: Scheduler = AndroidSchedulers.mainThread()): Disposable + = titleObservable.observeOn(scheduler).subscribe(subscriber) + override fun onTouchEvent(ev: MotionEvent): Boolean { val event = MotionEvent.obtain(ev) val action = MotionEventCompat.getActionMasked(event) diff --git a/app/src/main/res/layout/activity_web_overlay.xml b/app/src/main/res/layout/activity_web_overlay.xml index f962b396..ed6b8bb8 100644 --- a/app/src/main/res/layout/activity_web_overlay.xml +++ b/app/src/main/res/layout/activity_web_overlay.xml @@ -2,7 +2,7 @@ + android:layout_height="match_parent" + android:layout_marginTop="?attr/actionBarSize" /> diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index ded59278..40224e12 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -23,7 +23,7 @@