From 1ae19368bc6819fbb1f7a6337a5003dad44f7556 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 3 Jun 2017 14:00:04 -0700 Subject: move activity instance creators to ContextUtils --- .../kotlin/com/pitchedapps/frost/LoginActivity.kt | 16 +++++++-- .../kotlin/com/pitchedapps/frost/MainActivity.kt | 25 +++----------- .../kotlin/com/pitchedapps/frost/StartActivity.kt | 5 +-- .../com/pitchedapps/frost/WebOverlayActivity.kt | 20 ++--------- .../com/pitchedapps/frost/utils/ContextUtils.kt | 40 ++++++++++++++++++++++ .../com/pitchedapps/frost/web/FrostChromeClient.kt | 2 +- 6 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt (limited to 'app/src') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt index 410e2e1d..8fca271a 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/LoginActivity.kt @@ -1,9 +1,11 @@ package com.pitchedapps.frost +import android.app.Activity import android.content.Context import android.content.Intent import android.graphics.drawable.Drawable import android.os.Bundle +import android.os.Handler import android.support.v4.app.ActivityOptionsCompat import android.support.v4.content.ContextCompat import android.support.v4.widget.SwipeRefreshLayout @@ -24,6 +26,8 @@ import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.facebook.PROFILE_PICTURE_URL import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.bindView +import com.pitchedapps.frost.utils.cookies +import com.pitchedapps.frost.utils.launchNewTask import com.pitchedapps.frost.views.fadeIn import com.pitchedapps.frost.views.fadeOut import com.pitchedapps.frost.views.setTextWithFade @@ -55,11 +59,14 @@ class LoginActivity : AppCompatActivity() { val usernameObservable = SingleSubject.create()!! companion object { - fun newInstance(context: Context) { + const val EXTRA_COOKIES = "extra_cookies" + fun newInstance(context: Context, cookies: ArrayList = arrayListOf()) { val intent = Intent(context, LoginActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.putExtra(EXTRA_COOKIES, cookies) val bundle = ActivityOptionsCompat.makeCustomAnimation(context, R.anim.slide_in_right, R.anim.slide_out_right).toBundle() ContextCompat.startActivity(context, intent, bundle) + if (context is Activity) context.finish() } } @@ -101,6 +108,11 @@ class LoginActivity : AppCompatActivity() { L.d("Zip done") if (!foundImage) L.e("Could not get profile photo; Invalid id?\n\t$cookie") textview.setTextWithFade(String.format(getString(R.string.welcome), name), duration = 500) + Handler().postDelayed({ + val cookies = cookies() + cookies.add(cookie) + launchNewTask(MainActivity::class.java, cookies) + }, 1000) } loadProfile(cookie.id) loadUsername(cookie) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt index 223384c1..536deab9 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/MainActivity.kt @@ -1,7 +1,5 @@ package com.pitchedapps.frost -import android.app.Activity -import android.content.Intent import android.os.Bundle import android.support.design.widget.FloatingActionButton import android.support.design.widget.Snackbar @@ -30,9 +28,7 @@ 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.Changelog -import com.pitchedapps.frost.utils.bindView -import com.pitchedapps.frost.utils.toDrawable +import com.pitchedapps.frost.utils.* import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode @@ -46,24 +42,13 @@ class MainActivity : AppCompatActivity() { val tabs: TabLayout by bindView(R.id.tabs) lateinit var drawer: Drawer lateinit var drawerHeader: AccountHeader - lateinit var cookies: ArrayList - - companion object { - const val EXTRA_COOKIES = "extra_cookies" - fun launch(activity: Activity, cookies: List) { - val intent = (Intent(activity, MainActivity::class.java)) - intent.putParcelableArrayListExtra(EXTRA_COOKIES, ArrayList(cookies)) - activity.startActivity(intent) - activity.finish() - } - } + val cookies = cookies() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) ButterKnife.bind(this) setSupportActionBar(toolbar) - cookies = intent.extras.getParcelableArrayList(EXTRA_COOKIES) adapter = SectionsPagerAdapter(supportFragmentManager, loadFbTabs()) viewPager.adapter = adapter viewPager.offscreenPageLimit = 5 @@ -108,7 +93,7 @@ class MainActivity : AppCompatActivity() { } onProfileChanged { _, profile, current -> if (current) WebOverlayActivity.newInstance(this@MainActivity, FbTab.PROFILE) - else switchUser(profile.name.text, -1) + else switchUser(profile.name.text) false } } @@ -128,7 +113,7 @@ class MainActivity : AppCompatActivity() { iicon = item.icon identifier = item.titleId.toLong() onClick { _ -> - WebOverlayActivity.newInstance(this@MainActivity, item.url) + launchWebOverlay(item.url) false } } @@ -145,7 +130,7 @@ class MainActivity : AppCompatActivity() { // finish() } R.id.action_changelog -> Changelog.show(this) - R.id.action_call -> LoginActivity.newInstance(this) + R.id.action_call -> launchNewTask(LoginActivity::class.java) R.id.action_db -> adapter.pages.saveAsync(this) R.id.action_restart -> { finish(); diff --git a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt index 5d7d145f..a10372d4 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/StartActivity.kt @@ -5,6 +5,7 @@ import android.support.v7.app.AppCompatActivity import com.pitchedapps.frost.dbflow.loadFbCookiesAsync import com.pitchedapps.frost.utils.L import com.pitchedapps.frost.utils.Prefs +import com.pitchedapps.frost.utils.launchNewTask /** * Created by Allan Wang on 2017-05-28. @@ -18,13 +19,13 @@ class StartActivity : AppCompatActivity() { loadFbCookiesAsync { cookies -> L.d("Cookies loaded ${System.currentTimeMillis()} $cookies") - val sorted = cookies.toMutableList() + val sorted = ArrayList(cookies) val current = cookies.filter { it.id == Prefs.userId } if (current.isNotEmpty()) { sorted.remove(current[0]) sorted.add(0, current[0]) } - MainActivity.launch(this, sorted) + launchNewTask(MainActivity::class.java, sorted) } } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt index 9f6169cf..b7062ead 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/WebOverlayActivity.kt @@ -1,16 +1,12 @@ package com.pitchedapps.frost -import android.content.Context -import android.content.Intent import android.os.Bundle -import android.support.v4.app.ActivityOptionsCompat -import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import butterknife.ButterKnife import com.jude.swipbackhelper.SwipeBackHelper -import com.pitchedapps.frost.facebook.FbTab import com.pitchedapps.frost.utils.bindView +import com.pitchedapps.frost.utils.url import com.pitchedapps.frost.web.FrostWebView @@ -22,24 +18,12 @@ class WebOverlayActivity : AppCompatActivity() { val toolbar: Toolbar by bindView(R.id.toolbar) val frostWeb: FrostWebView by bindView(R.id.frost_webview) - companion object { - private const val ARG_URL = "arg_url" - fun newInstance(context: Context, url: String) { - val intent = Intent(context, WebOverlayActivity::class.java) - intent.putExtra(ARG_URL, url) - val bundle = ActivityOptionsCompat.makeCustomAnimation(context, R.anim.slide_in_right, R.anim.slide_out_right).toBundle() - ContextCompat.startActivity(context, intent, bundle) - } - - fun newInstance(context: Context, url: FbTab) = newInstance(context, url.url) - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_web_overlay) ButterKnife.bind(this) setSupportActionBar(toolbar) - frostWeb.baseUrl = intent.extras.getString(ARG_URL) + frostWeb.baseUrl = url() SwipeBackHelper.onCreate(this) SwipeBackHelper.getCurrentPage(this) .setSwipeBackEnable(true) diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt new file mode 100644 index 00000000..1f0eb5d3 --- /dev/null +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/ContextUtils.kt @@ -0,0 +1,40 @@ +package com.pitchedapps.frost.utils + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.support.v4.app.ActivityOptionsCompat +import android.support.v4.content.ContextCompat +import com.pitchedapps.frost.R +import com.pitchedapps.frost.WebOverlayActivity +import com.pitchedapps.frost.dbflow.CookieModel +import com.pitchedapps.frost.facebook.FbTab + +/** + * Created by Allan Wang on 2017-06-03. + */ +private const val EXTRA_COOKIES = "extra_cookies" +private const val ARG_URL = "arg_url" + +fun Activity.launchNewTask(clazz: Class, cookieList: ArrayList = arrayListOf()) { + val intent = (Intent(this, clazz)) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK) + intent.putParcelableArrayListExtra(EXTRA_COOKIES, cookieList) + startActivity(intent) + finish() +} + +fun Activity.cookies(): ArrayList { + return intent.extras?.getParcelableArrayList(EXTRA_COOKIES) ?: arrayListOf() +} + +fun Context.launchWebOverlay(url: String) { + val intent = Intent(this, WebOverlayActivity::class.java) + 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) +} + +fun WebOverlayActivity.url(): String { + return intent.extras?.getString(ARG_URL) ?: FbTab.FEED.url +} \ 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 4cd90d7b..fe3c92e8 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/web/FrostChromeClient.kt @@ -17,7 +17,7 @@ class FrostChromeClient(val progressObservable: Subject) : WebChromeClient( override fun onReceivedTitle(view: WebView?, title: String?) { super.onReceivedTitle(view, title) - L.d("Title $title") + L.v("Title $title") } override fun onProgressChanged(view: WebView, newProgress: Int) { -- cgit v1.2.3