Skip to content

Commit 6cc502c

Browse files
committed
Adjust connection error deal.
1 parent a2746ad commit 6cc502c

File tree

3 files changed

+111
-90
lines changed

3 files changed

+111
-90
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ android {
3636
}
3737
release {
3838
multiDexEnabled true
39-
minifyEnabled true
39+
minifyEnabled false
4040
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
4141
signingConfig signingConfigs.debug
4242
}

app/proguard-rules.pro

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,10 @@
1818

1919
# If you keep the line number information, uncomment this to
2020
# hide the original source file name.
21-
#-renamesourcefileattribute SourceFile
21+
#-renamesourcefileattribute SourceFile
22+
23+
-keep, allowobfuscation, allowoptimization class org.kodein.type.TypeReference
24+
-keep, allowobfuscation, allowoptimization class org.kodein.type.JVMAbstractTypeToken$Companion$WrappingTest
25+
26+
-keep, allowobfuscation, allowoptimization class * extends org.kodein.type.TypeReference
27+
-keep, allowobfuscation, allowoptimization class * extends org.kodein.type.JVMAbstractTypeToken$Companion$WrappingTest

app/src/main/java/com/tans/tfiletransporter/ui/activity/filetransport/activity/FileTransportActivity.kt

Lines changed: 103 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.Intent
55
import android.os.SystemClock
66
import android.util.Log
77
import android.view.View
8+
import android.widget.Toast
89
import androidx.fragment.app.Fragment
910
import com.google.android.material.appbar.AppBarLayout
1011
import com.google.android.material.tabs.TabLayout
@@ -64,6 +65,7 @@ enum class DirTabType {
6465

6566
sealed class ConnectionStatus {
6667
object Connecting : ConnectionStatus()
68+
object Error : ConnectionStatus()
6769
data class Connected(
6870
val localAddress: InetAddress,
6971
val remoteAddress: InetAddress,
@@ -114,8 +116,8 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
114116
null
115117
}
116118

117-
if (handshakeModel != null) {
118-
updateState { oldState ->
119+
updateState { oldState ->
120+
if (handshakeModel != null) {
119121
oldState.copy(
120122
connectionStatus = ConnectionStatus.Connected(
121123
localAddress = localAddress,
@@ -124,8 +126,12 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
124126
fileExploreConnection = fileConnection
125127
)
126128
)
127-
}.await()
129+
} else {
130+
oldState.copy(connectionStatus = ConnectionStatus.Error)
131+
}
132+
}.await()
128133

134+
if (handshakeModel != null) {
129135

130136
fileConnection.observeRemoteFileExploreContent()
131137
.doOnNext {
@@ -258,109 +264,110 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
258264
binding.toolBar.subtitle = remoteAddress.hostAddress
259265

260266
val loadingDialog = showLoadingDialog(cancelable = false)
261-
withContext(Dispatchers.IO) {
267+
val status = withContext(Dispatchers.IO) {
262268
bindState()
263269
.map { it.connectionStatus }
264-
.filter { it is ConnectionStatus.Connected }
265-
.cast<ConnectionStatus.Connected>()
270+
.filter { it is ConnectionStatus.Connected || it is ConnectionStatus.Error }
266271
.firstOrError()
267272
.await()
268273
}
269274
loadingDialog.cancel()
270275

271-
render({ it.shareMyDir }) { binding.toolBar.menu.findItem(R.id.share_my_folder).isChecked = it }.bindLife()
276+
if (status is ConnectionStatus.Connected) {
277+
render({ it.shareMyDir }) { binding.toolBar.menu.findItem(R.id.share_my_folder).isChecked = it }.bindLife()
272278

273-
binding.viewPager.adapter = object : FragmentStateAdapter(this@FileTransportActivity) {
274-
override fun getItemCount(): Int = fragments.size
275-
override fun createFragment(position: Int): Fragment = fragments[DirTabType.values()[position]]!!
276-
}
277-
278-
TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
279-
tab.text = when (DirTabType.values()[position]) {
280-
DirTabType.MyApps -> getString(R.string.file_transport_activity_tab_my_apps)
281-
DirTabType.MyImages -> getString(R.string.file_transport_activity_tab_my_images)
282-
DirTabType.MyDir -> getString(R.string.file_transport_activity_tab_my_dir)
283-
DirTabType.RemoteDir -> getString(R.string.file_transport_activity_tab_remote_dir)
284-
DirTabType.Message -> getString(R.string.file_transport_activity_tab_message)
279+
binding.viewPager.adapter = object : FragmentStateAdapter(this@FileTransportActivity) {
280+
override fun getItemCount(): Int = fragments.size
281+
override fun createFragment(position: Int): Fragment = fragments[DirTabType.values()[position]]!!
285282
}
286-
}.attach()
287-
288-
binding.toolBar.setOnMenuItemClickListener {
289-
if (it.itemId == R.id.share_my_folder) {
290-
updateState { oldState ->
291-
oldState.copy(shareMyDir = !oldState.shareMyDir)
292-
}.bindLife()
293-
true
294-
} else {
295-
false
283+
284+
TabLayoutMediator(binding.tabLayout, binding.viewPager) { tab, position ->
285+
tab.text = when (DirTabType.values()[position]) {
286+
DirTabType.MyApps -> getString(R.string.file_transport_activity_tab_my_apps)
287+
DirTabType.MyImages -> getString(R.string.file_transport_activity_tab_my_images)
288+
DirTabType.MyDir -> getString(R.string.file_transport_activity_tab_my_dir)
289+
DirTabType.RemoteDir -> getString(R.string.file_transport_activity_tab_remote_dir)
290+
DirTabType.Message -> getString(R.string.file_transport_activity_tab_message)
291+
}
292+
}.attach()
293+
294+
binding.toolBar.setOnMenuItemClickListener {
295+
if (it.itemId == R.id.share_my_folder) {
296+
updateState { oldState ->
297+
oldState.copy(shareMyDir = !oldState.shareMyDir)
298+
}.bindLife()
299+
true
300+
} else {
301+
false
302+
}
296303
}
297-
}
298304

299-
binding.tabLayout.addOnTabSelectedListener(object :
305+
binding.tabLayout.addOnTabSelectedListener(object :
300306
TabLayout.OnTabSelectedListener {
301-
override fun onTabSelected(tab: TabLayout.Tab?) {
302-
when (tab?.position) {
303-
DirTabType.MyApps.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.MyApps) }.bindLife()
304-
DirTabType.MyImages.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.MyImages) }.bindLife()
305-
DirTabType.MyDir.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.MyDir) }.bindLife()
306-
DirTabType.RemoteDir.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.RemoteDir) }.bindLife()
307-
DirTabType.Message.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.Message) }.bindLife()
307+
override fun onTabSelected(tab: TabLayout.Tab?) {
308+
when (tab?.position) {
309+
DirTabType.MyApps.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.MyApps) }.bindLife()
310+
DirTabType.MyImages.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.MyImages) }.bindLife()
311+
DirTabType.MyDir.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.MyDir) }.bindLife()
312+
DirTabType.RemoteDir.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.RemoteDir) }.bindLife()
313+
DirTabType.Message.ordinal -> updateStateCompletable { it.copy(selectedTabType = DirTabType.Message) }.bindLife()
314+
}
308315
}
309-
}
310316

311-
override fun onTabUnselected(tab: TabLayout.Tab?) {
312-
}
317+
override fun onTabUnselected(tab: TabLayout.Tab?) {
318+
}
313319

314-
override fun onTabReselected(tab: TabLayout.Tab?) {
315-
}
316-
})
320+
override fun onTabReselected(tab: TabLayout.Tab?) {
321+
}
322+
})
317323

318-
binding.floatingActionBt.clicks()
324+
binding.floatingActionBt.clicks()
319325
.doOnNext { fileTransportScopeData.floatBtnEvent.onNext(Unit) }
320326
.bindLife()
321-
render({ it.connectionStatus }) {
322-
when (it) {
323-
ConnectionStatus.Connecting -> {
324-
binding.toolBar.title = ""
325-
binding.toolBar.subtitle = ""
326-
}
327-
is ConnectionStatus.Connected -> {
328-
binding.toolBar.title = it.handshakeModel.deviceName
329-
binding.toolBar.subtitle = it.remoteAddress.hostAddress
327+
render({ it.connectionStatus }) {
328+
when (it) {
329+
ConnectionStatus.Connecting -> {
330+
binding.toolBar.title = ""
331+
binding.toolBar.subtitle = ""
332+
}
333+
is ConnectionStatus.Connected -> {
334+
binding.toolBar.title = it.handshakeModel.deviceName
335+
binding.toolBar.subtitle = it.remoteAddress.hostAddress
336+
}
330337
}
331-
}
332-
}.bindLife()
338+
}.bindLife()
333339

334-
render({ it.selectedTabType }) {
340+
render({ it.selectedTabType }) {
335341

336-
when (it) {
337-
DirTabType.MyApps, DirTabType.MyImages, DirTabType.MyDir, DirTabType.RemoteDir -> {
338-
val lpCollapsing = (binding.collapsingLayout.layoutParams as? AppBarLayout.LayoutParams)
339-
lpCollapsing?.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED
340-
binding.collapsingLayout.layoutParams = lpCollapsing
341-
}
342-
DirTabType.Message -> {
343-
val lpCollapsing = (binding.collapsingLayout.layoutParams as? AppBarLayout.LayoutParams)
344-
lpCollapsing?.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL
345-
binding.collapsingLayout.layoutParams = lpCollapsing
342+
when (it) {
343+
DirTabType.MyApps, DirTabType.MyImages, DirTabType.MyDir, DirTabType.RemoteDir -> {
344+
val lpCollapsing = (binding.collapsingLayout.layoutParams as? AppBarLayout.LayoutParams)
345+
lpCollapsing?.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL or AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED
346+
binding.collapsingLayout.layoutParams = lpCollapsing
347+
}
348+
DirTabType.Message -> {
349+
val lpCollapsing = (binding.collapsingLayout.layoutParams as? AppBarLayout.LayoutParams)
350+
lpCollapsing?.scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL
351+
binding.collapsingLayout.layoutParams = lpCollapsing
352+
}
346353
}
347-
}
348354

349-
when (it) {
350-
DirTabType.MyApps, DirTabType.MyImages, DirTabType.MyDir -> {
351-
binding.floatingActionBt.setImageResource(R.drawable.share_variant_outline)
352-
binding.floatingActionBt.visibility = View.VISIBLE
353-
}
354-
DirTabType.RemoteDir -> {
355-
binding.floatingActionBt.setImageResource(R.drawable.download_outline)
356-
binding.floatingActionBt.visibility = View.VISIBLE
357-
}
358-
DirTabType.Message -> {
359-
binding.floatingActionBt.visibility = View.GONE
355+
when (it) {
356+
DirTabType.MyApps, DirTabType.MyImages, DirTabType.MyDir -> {
357+
binding.floatingActionBt.setImageResource(R.drawable.share_variant_outline)
358+
binding.floatingActionBt.visibility = View.VISIBLE
359+
}
360+
DirTabType.RemoteDir -> {
361+
binding.floatingActionBt.setImageResource(R.drawable.download_outline)
362+
binding.floatingActionBt.visibility = View.VISIBLE
363+
}
364+
DirTabType.Message -> {
365+
binding.floatingActionBt.visibility = View.GONE
366+
}
360367
}
361-
}
362-
binding.appbarLayout.setExpanded(true, true)
363-
}.bindLife()
368+
binding.appbarLayout.setExpanded(true, true)
369+
}.bindLife()
370+
}
364371
}
365372
}
366373

@@ -369,9 +376,12 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
369376
val tabType = withContext(Dispatchers.IO) { bindState().firstOrError().map { it.selectedTabType }.await() }
370377
if (fragments[tabType]?.onBackPressed() != true) {
371378
ioExecutor.execute {
372-
fileTransportScopeData.fileExploreConnection.let {
373-
if (it.isConnectionActive()) {
374-
it.close(true)
379+
val status = bindState().firstOrError().map { it.connectionStatus }.blockingGet()
380+
if (status is ConnectionStatus.Connected) {
381+
fileTransportScopeData.fileExploreConnection.let {
382+
if (it.isConnectionActive()) {
383+
it.close(true)
384+
}
375385
}
376386
}
377387
}
@@ -382,9 +392,14 @@ class FileTransportActivity : BaseActivity<FileTransportActivityBinding, FileTra
382392

383393
override fun onDestroy() {
384394
super.onDestroy()
385-
fileTransportScopeData.fileExploreConnection.let {
386-
if (it.isConnectionActive()) {
387-
it.close(false)
395+
ioExecutor.execute {
396+
val status = bindState().firstOrError().map { it.connectionStatus }.blockingGet()
397+
if (status is ConnectionStatus.Connected) {
398+
fileTransportScopeData.fileExploreConnection.let {
399+
if (it.isConnectionActive()) {
400+
it.close(false)
401+
}
402+
}
388403
}
389404
}
390405
}

0 commit comments

Comments
 (0)