-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
完成 基于Kotlin+协程+MVVM的重构(还剩一个剪切板的UI展示)
- Loading branch information
1 parent
0317188
commit f433d7f
Showing
13 changed files
with
569 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.sibyl.httpfiledominator | ||
|
||
import android.app.Application | ||
|
||
/** | ||
* @author Sasuke on 2020/6/23. | ||
*/ | ||
class MyApp : Application(){ | ||
companion object{ | ||
@JvmStatic | ||
var instance: MyApp? = null | ||
} | ||
|
||
override fun onCreate() { | ||
super.onCreate() | ||
instance = this | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 22 additions & 1 deletion
23
app/src/main/java/com/sibyl/httpfiledominator/activities/BaseActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,30 @@ | ||
package com.sibyl.httpfiledominator.activities | ||
|
||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.appcompat.widget.Toolbar | ||
import com.sibyl.httpfiledominator.R | ||
|
||
/** | ||
* @author Sasuke on 2020/6/23. | ||
*/ | ||
class BaseActivity: AppCompatActivity() { | ||
open class BaseActivity: AppCompatActivity() { | ||
companion object{ | ||
val HANDLER_CONNECTION_START = 42 | ||
val HANDLER_CONNECTION_END = 4242 | ||
} | ||
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
} | ||
|
||
fun initUIAfter(toolbar: Toolbar){ | ||
window.navigationBarColor = resources.getColor(R.color.main_activity_background_color, null) | ||
//设置ActionBar | ||
setSupportActionBar(toolbar.apply { | ||
setTitle(getString(R.string.app_name)) | ||
setTitleTextColor(getResources().getColor(R.color.light_blue, null)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 74 additions & 1 deletion
75
app/src/main/java/com/sibyl/httpfiledominator/mainactivity/model/MainModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,84 @@ | ||
package com.sibyl.httpfiledominator.mainactivity.model | ||
|
||
import android.content.Intent | ||
import android.os.Handler | ||
import android.os.Looper | ||
import androidx.databinding.ObservableField | ||
import androidx.lifecycle.MutableLiveData | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.sibyl.httpfiledominator.MyHttpServer | ||
import com.sibyl.httpfiledominator.UriInterpretation | ||
import com.sibyl.httpfiledominator.mainactivity.repo.MainRepo | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* @author HUANGSHI-PC on 2020-03-06 0006. | ||
*/ | ||
class MainModel(val repo: MainRepo): ViewModel() { | ||
class MainModel(val repo: MainRepo) : ViewModel() { | ||
val isLoading = MutableLiveData<Boolean>().apply { value = false } | ||
|
||
val snackbarMsg = MutableLiveData<String>().apply { value = "" } | ||
|
||
val httpServer = MutableLiveData<MyHttpServer?>() | ||
/**默认显示的IP地址*/ | ||
val preferredServerUrl = ObservableField<String>() | ||
/**服务器IP地址集*/ | ||
val listOfServerUris = MutableLiveData<MutableList<String>>() | ||
|
||
/**每次加的新数据缓存在这里*/ | ||
val newUrisCache = MutableLiveData<List<UriInterpretation>>() | ||
|
||
/**是否剪切板模式*/ | ||
val isClipboardMode = MutableLiveData<Boolean>().apply { value = false } | ||
|
||
/**处理刚进页面时传入的Intent*/ | ||
fun dealWithNewIntent(intent: Intent?) = viewModelScope.launch { | ||
// if (isFinishing()) conti.resumeWithException(Exception("")) | ||
if (intent?.extras != null) isLoading.value = true | ||
try { | ||
intent?.extras?.let { | ||
val sharedUriList = repo.getIntentFileUris(intent) | ||
dealWithNewUris(sharedUriList) | ||
} | ||
} catch (e: Exception) { | ||
snackbarMsg.value = e.message | ||
}finally { | ||
isLoading.value = false | ||
} | ||
} | ||
|
||
/**处理ActivityResult传入的Intent*/ | ||
fun dealActivityResultIntent(intent: Intent?) = viewModelScope.launch { | ||
if (intent != null) isLoading.value = true | ||
try { | ||
val newUriList = repo.getActivityResultUris(intent) | ||
dealWithNewUris(newUriList) | ||
} catch (e: Exception) { | ||
snackbarMsg.value = e.message | ||
}finally { | ||
isLoading.value = false | ||
} | ||
} | ||
|
||
|
||
|
||
/**对新uri的处理*/ | ||
fun dealWithNewUris(newUriList: MutableList<UriInterpretation>?) = viewModelScope.launch { | ||
//先过滤一波已经添加过的 | ||
newUriList?.removeAll(MyHttpServer.getNormalUris()) | ||
if (newUriList.isNullOrEmpty()) return@launch | ||
//如果没有建立起http服务时========== | ||
if (httpServer.value == null){ | ||
httpServer.value = MyHttpServer(1120) | ||
} | ||
MyHttpServer.getNormalUris().addAll(newUriList) | ||
//显示到UI | ||
newUrisCache.value = newUriList | ||
MyHttpServer.changeUrisByMode(false) | ||
//切换模式 | ||
isClipboardMode.value = false | ||
} | ||
|
||
|
||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/sibyl/httpfiledominator/mainactivity/repo/MainRepo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,63 @@ | ||
package com.sibyl.httpfiledominator.mainactivity.repo | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Parcelable | ||
import com.sibyl.httpfiledominator.MyApp | ||
import com.sibyl.httpfiledominator.UriInterpretation | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* @author HUANGSHI-PC on 2020-03-06 0006. | ||
*/ | ||
class MainRepo { | ||
|
||
/** | ||
* 以下是从SendFileActivity里拷过来的。用来接收直接从外面分享进来的文件, | ||
* 原SendFileActivity已废弃 | ||
*/ | ||
suspend fun getIntentFileUris(dataIntent: Intent): MutableList<UriInterpretation>? = withContext(Dispatchers.IO) { | ||
val theUris = mutableListOf<UriInterpretation>() | ||
if (Intent.ACTION_SEND_MULTIPLE == dataIntent.action) { | ||
dataIntent.getParcelableArrayListExtra<Parcelable>(Intent.EXTRA_STREAM)?.forEach { | ||
it?.let { theUris.add(UriInterpretation(it as Uri, MyApp.instance?.contentResolver)) } | ||
} | ||
return@withContext theUris | ||
} | ||
val extras = dataIntent.extras | ||
if (extras == null) {//直接手动进的主页,不会存在传intent数据的情况 | ||
throw Exception() | ||
} | ||
var myUri: Uri? = extras[Intent.EXTRA_STREAM] as Uri | ||
if (myUri == null) { | ||
val tempString = extras[Intent.EXTRA_TEXT] as String? | ||
if (tempString == null) { | ||
throw Exception("Error obtaining the file path") | ||
} | ||
myUri = Uri.parse(tempString) | ||
if (myUri == null) { | ||
throw Exception("Error obtaining the file path") | ||
} | ||
} | ||
theUris.add(UriInterpretation(myUri, MyApp.instance?.getContentResolver())) | ||
theUris | ||
} | ||
|
||
/**处理onActivityResult()里的新数据*/ | ||
suspend fun getActivityResultUris(data: Intent?): MutableList<UriInterpretation> = withContext(Dispatchers.IO) { | ||
val theUris = mutableListOf<UriInterpretation>() | ||
val dataUri = data?.getData() | ||
if (dataUri != null) { | ||
theUris.add(UriInterpretation(dataUri, MyApp.instance?.getContentResolver())) | ||
} else { | ||
val clipData = data?.getClipData() | ||
for (i in 0 until (clipData?.itemCount ?: 0)) { | ||
val item = clipData?.getItemAt(i) | ||
val uri = item?.uri | ||
theUris.add(UriInterpretation(uri, MyApp.instance?.getContentResolver())) | ||
} | ||
} | ||
theUris | ||
} | ||
} |
Oops, something went wrong.