Skip to content

Commit

Permalink
Merge pull request #141 from geckour/feature/fix-hash-tag-tl
Browse files Browse the repository at this point in the history
特定のFragmentからバックキーで遷移した時にアプリが落ちる問題を修正
  • Loading branch information
geckour authored Aug 14, 2017
2 parents 16b3a49 + c5d527c commit 13fa587
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/com/geckour/egret/util/Common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,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? {
Expand Down Expand Up @@ -299,7 +304,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 ->
Expand Down
68 changes: 32 additions & 36 deletions app/src/main/java/com/geckour/egret/view/activity/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ 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
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

Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -460,38 +463,32 @@ 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
}

return@withOnAccountHeaderListener true
}
.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)
Expand All @@ -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))
Expand Down Expand Up @@ -535,7 +532,6 @@ class MainActivity : BaseActivity() {
}

NAV_ITEM_SETTINGS -> {

val intent = SettingActivity.getIntent(this)
startActivity(intent)
false
Expand All @@ -549,8 +545,8 @@ class MainActivity : BaseActivity() {
}

fun showTimelineFragment(category: TimelineFragment.Category = currentCategory, force: Boolean = false, hashTag: String? = null) {
val currentFragment = supportFragmentManager.fragments?.lastOrNull { it?.isVisible ?: false }
val reqFragment = supportFragmentManager.findFragmentByTag(category.name)
val currentFragment = supportFragmentManager.findFragmentByTag(currentCategory.name)

if (!force
&& currentFragment != null
Expand All @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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<List<TimelineContent>>() {}
val contents: List<TimelineContent> = gson.fromJson(sharedPref.getString(storeContentsKey, ""), type.type)
Log.d("restoreTimeline", "contents.size: ${contents.size}")
adapter.addAllContents(contents)
}

Expand Down

0 comments on commit 13fa587

Please sign in to comment.