From 25451a8531b73e66cea924572e7cc51b8c2c1801 Mon Sep 17 00:00:00 2001 From: geckour Date: Thu, 10 Aug 2017 18:44:59 +0900 Subject: [PATCH 1/2] =?UTF-8?q?WIP=20-=20=E3=82=A2=E3=82=AB=E3=82=A6?= =?UTF-8?q?=E3=83=B3=E3=83=88=E5=88=87=E6=9B=BF=E6=99=82=E3=81=AE=E6=8C=99?= =?UTF-8?q?=E5=8B=95=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/geckour/egret/util/Common.kt | 9 ++- .../egret/view/activity/MainActivity.kt | 68 +++++++++---------- .../egret/view/fragment/TimelineFragment.kt | 14 ++-- 3 files changed, 49 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/geckour/egret/util/Common.kt b/app/src/main/java/com/geckour/egret/util/Common.kt index 9e26a76..ed7a6c8 100644 --- a/app/src/main/java/com/geckour/egret/util/Common.kt +++ b/app/src/main/java/com/geckour/egret/util/Common.kt @@ -66,7 +66,12 @@ class Common { fun getCurrentAccessToken(): AccessToken? { val accessTokens = OrmaProvider.db.selectFromAccessToken().isCurrentEq(true) - return if (accessTokens == null || accessTokens.isEmpty) null else accessTokens.last() + return accessTokens?.lastOrNull() + } + + fun getInstanceName(): String? { + val accessToken = getCurrentAccessToken() ?: return null + return OrmaProvider.db.selectFromInstanceAuthInfo().idEq(accessToken.instanceId).last()?.instance } fun resetAuthInfo(): String? { @@ -296,7 +301,7 @@ class Common { } }) - fun getStoreContentsKey(category: TimelineFragment.Category) = "${TimelineFragment.STATE_ARGS_KEY_CONTENTS}:${category.name}" + fun getStoreContentsKey(category: TimelineFragment.Category) = "${getInstanceName()}:${getCurrentAccessToken()?.accountId}:${TimelineFragment.STATE_ARGS_KEY_CONTENTS}:${category.name}" fun showSoftKeyBoardOnFocusEditText(et: EditText, hideOnUnFocus: Boolean = true) { et.setOnFocusChangeListener { view, hasFocus -> diff --git a/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt b/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt index f692079..7aae90d 100644 --- a/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt +++ b/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt @@ -32,7 +32,6 @@ import com.geckour.egret.util.OrmaProvider import com.geckour.egret.view.adapter.TimelineAdapter import com.geckour.egret.view.adapter.model.TimelineContent import com.geckour.egret.view.fragment.* -import com.geckour.egret.view.fragment.TimelineFragment.Companion.STATE_ARGS_KEY_RESUME import com.mikepenz.materialdrawer.AccountHeader import com.mikepenz.materialdrawer.AccountHeaderBuilder import com.mikepenz.materialdrawer.Drawer @@ -40,9 +39,11 @@ import com.mikepenz.materialdrawer.DrawerBuilder import com.mikepenz.materialdrawer.model.DividerDrawerItem import com.mikepenz.materialdrawer.model.PrimaryDrawerItem import com.mikepenz.materialdrawer.model.ProfileDrawerItem +import com.mikepenz.materialdrawer.model.interfaces.IProfile import io.reactivex.Observable import io.reactivex.Single import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers import timber.log.Timber @@ -110,7 +111,7 @@ class MainActivity : BaseActivity() { } ) ) - 2 -> Pair(R.string.array_item_mute_hash_tag, if (content.tags.isEmpty()) "" else s.format(content.tags.map { s -> "#$s" }.joinToString())) + 2 -> Pair(R.string.array_item_mute_hash_tag, if (content.tags.isEmpty()) "" else s.format(content.tags.map { tag -> "#$tag" }.joinToString())) 3 -> { var instance = content.nameWeak.replace(Regex("^@.+@(.+)$"), "@$1") @@ -379,8 +380,10 @@ class MainActivity : BaseActivity() { .compose(bindToLifecycle()) .subscribe({ result -> Log.d("showSearchResult", "result.hashTags: ${result.hashTags}") + + val fragment = SearchResultFragment.newInstance(query = query, result = result) supportFragmentManager.beginTransaction() - .replace(R.id.container, SearchResultFragment.newInstance(query = query, result = result), SearchResultFragment.TAG) + .replace(R.id.container, fragment, SearchResultFragment.TAG) .addToBackStack(SearchResultFragment.TAG) .commit() }, Throwable::printStackTrace) @@ -460,31 +463,14 @@ class MainActivity : BaseActivity() { } return@withOnAccountHeaderListener false } else if (!current) { - OrmaProvider.db.updateAccessToken().isCurrentEq(true).isCurrent(false).executeAsSingle() - .flatMap { OrmaProvider.db.updateAccessToken().accountIdEq(profile.identifier).isCurrent(true).executeAsSingle() } - .subscribeOn(Schedulers.newThread()) - .observeOn(AndroidSchedulers.mainThread()) - .compose(bindToLifecycle()) - .subscribe({ i -> - Timber.d("updated row count: $i") - - supportFragmentManager.beginTransaction() - .apply { - val editor = sharedPref.edit() - editor.putBoolean(STATE_ARGS_KEY_RESUME, false) - listOf(TimelineFragment.Category.Public, TimelineFragment.Category.Local, TimelineFragment.Category.User) - .forEach { - supportFragmentManager.findFragmentByTag(it.name)?.let { fragment -> - detach(fragment) - remove(fragment) - } - } - editor.apply() - } - .commit() - - showTimelineFragment(force = true) - }, Throwable::printStackTrace) + supportFragmentManager.fragments.lastOrNull { it.isVisible }?.let { + (it as? TimelineFragment)?.let { + supportFragmentManager.beginTransaction().detach(it).commit() + supportFragmentManager.executePendingTransactions() + } + resetAccount(profile.identifier) + } + return@withOnAccountHeaderListener false } @@ -492,6 +478,17 @@ class MainActivity : BaseActivity() { } .build() + fun resetAccount(identifier: Long): Disposable = OrmaProvider.db.updateAccessToken().isCurrentEq(true).isCurrent(false).executeAsSingle() + .flatMap { OrmaProvider.db.updateAccessToken().accountIdEq(identifier).isCurrent(true).executeAsSingle() } + .subscribeOn(Schedulers.newThread()) + .observeOn(AndroidSchedulers.mainThread()) + .compose(bindToLifecycle()) + .subscribe({ i -> + Timber.d("updated row count: $i") + + showTimelineFragment(force = true) + }, Throwable::printStackTrace) + fun setNavDrawer() { drawer = DrawerBuilder().withActivity(this) .withTranslucentStatusBar(false) @@ -507,7 +504,7 @@ class MainActivity : BaseActivity() { DividerDrawerItem(), PrimaryDrawerItem().withName(R.string.navigation_drawer_item_settings).withIdentifier(NAV_ITEM_SETTINGS).withIcon(R.drawable.ic_settings_black_24px).withIconTintingEnabled(true).withIconColorRes(R.color.icon_tint_dark) ) - .withOnDrawerItemClickListener { v, position, item -> + .withOnDrawerItemClickListener { _, _, item -> return@withOnDrawerItemClickListener when (item.identifier) { NAV_ITEM_LOGIN -> { startActivity(LoginActivity.getIntent(this)) @@ -535,7 +532,6 @@ class MainActivity : BaseActivity() { } NAV_ITEM_SETTINGS -> { - val intent = SettingActivity.getIntent(this) startActivity(intent) false @@ -549,8 +545,8 @@ class MainActivity : BaseActivity() { } fun showTimelineFragment(category: TimelineFragment.Category = currentCategory, force: Boolean = false, hashTag: String? = null) { - val reqFragment = supportFragmentManager.findFragmentByTag(category.name) val currentFragment = supportFragmentManager.findFragmentByTag(currentCategory.name) + val reqFragment = supportFragmentManager.findFragmentByTag(category.name) if (!force && currentFragment != null @@ -559,14 +555,14 @@ class MainActivity : BaseActivity() { supportFragmentManager.beginTransaction() .apply { - if (currentFragment != null && currentFragment.tag != category.name) detach(currentFragment) - if (reqFragment == null) { + currentFragment?.let { detach(it) } + reqFragment?.let { + attach(it) + } ?: let { val fragment = TimelineFragment.newInstance(category, hashTag) replace(R.id.container, fragment, category.name) - } else { - attach(reqFragment) } - if (supportFragmentManager.backStackEntryCount > 0 && currentFragment != null) addToBackStack(category.name) + if (supportFragmentManager.fragments?.size ?: 0 > 1) addToBackStack(category.name) } .commit() diff --git a/app/src/main/java/com/geckour/egret/view/fragment/TimelineFragment.kt b/app/src/main/java/com/geckour/egret/view/fragment/TimelineFragment.kt index c8e0a7d..ac14904 100644 --- a/app/src/main/java/com/geckour/egret/view/fragment/TimelineFragment.kt +++ b/app/src/main/java/com/geckour/egret/view/fragment/TimelineFragment.kt @@ -163,12 +163,16 @@ class TimelineFragment: BaseFragment() { .apply { if (getCategory() != Category.HashTag) { val json = gson.toJson(adapter.getContents()) - putString(getStoreContentsKey(getCategory()), json) - }else { + val storedContentsKey = getStoreContentsKey(getCategory()).apply { Log.d("onPause", "storeContentsKey: $this") } + putString(storedContentsKey, json) + } else { getHashTag()?.let { putString(ARGS_KEY_HASH_TAG, it) } } } .apply() + + sinceId = -1L + maxId = -1L } override fun onResume() { @@ -208,11 +212,13 @@ class TimelineFragment: BaseFragment() { } fun restoreTimeline() { - val storeContentsKey = getStoreContentsKey(getCategory()) + adapter.clearContents() + + val storeContentsKey = getStoreContentsKey(getCategory()).apply { Log.d("restoreTimeline", "storeContentsKey: $this") } if (sharedPref.contains(storeContentsKey)) { - adapter.clearContents() val type = object: TypeToken>() {} val contents: List = gson.fromJson(sharedPref.getString(storeContentsKey, ""), type.type) + Log.d("restoreTimeline", "contents.size: ${contents.size}") adapter.addAllContents(contents) } From c5d527c90e98490d7fe4921a1ba4dbb5a8563164 Mon Sep 17 00:00:00 2001 From: geckour Date: Mon, 14 Aug 2017 17:54:29 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E7=89=B9=E5=AE=9A=E3=81=AEFragment?= =?UTF-8?q?=E3=81=8B=E3=82=89=E3=83=90=E3=83=83=E3=82=AF=E3=82=AD=E3=83=BC?= =?UTF-8?q?=E3=81=A7=E9=81=B7=E7=A7=BB=E3=81=97=E3=81=9F=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=82=A2=E3=83=97=E3=83=AA=E3=81=8C=E8=90=BD=E3=81=A1=E3=82=8B?= =?UTF-8?q?=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/geckour/egret/view/activity/MainActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt b/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt index 7aae90d..8071988 100644 --- a/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt +++ b/app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt @@ -545,7 +545,7 @@ class MainActivity : BaseActivity() { } fun showTimelineFragment(category: TimelineFragment.Category = currentCategory, force: Boolean = false, hashTag: String? = null) { - val currentFragment = supportFragmentManager.findFragmentByTag(currentCategory.name) + val currentFragment = supportFragmentManager.fragments?.lastOrNull { it?.isVisible ?: false } val reqFragment = supportFragmentManager.findFragmentByTag(category.name) if (!force