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 --- .../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 +++++++++++++----- 8 files changed, 54 insertions(+), 31 deletions(-) delete mode 100644 app/src/main/kotlin/com/pitchedapps/frost/utils/Interfaces.kt (limited to 'app/src/main/kotlin/com') 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) -- cgit v1.2.3