mirror of
https://github.com/MetaCubeX/ClashMetaForAndroid.git
synced 2026-05-09 18:11:26 +08:00
rename ProfileMetadata.kt -> Profile.kt
This commit is contained in:
@@ -15,7 +15,7 @@ import android.widget.BaseAdapter
|
||||
import android.widget.TextView
|
||||
import com.github.kr328.clash.common.util.intent
|
||||
import com.github.kr328.clash.remote.withProfile
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata.Type
|
||||
import com.github.kr328.clash.service.model.Profile.Type
|
||||
import kotlinx.android.synthetic.main.activity_create_profile.*
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@@ -6,7 +6,7 @@ import android.view.View
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.github.kr328.clash.fragment.ProfileEditFragment
|
||||
import com.github.kr328.clash.remote.withProfile
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.activity_profile_edit.*
|
||||
import kotlinx.coroutines.launch
|
||||
@@ -101,7 +101,7 @@ class ProfileEditActivity : BaseActivity() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
private fun commit(metadata: ProfileMetadata) {
|
||||
private fun commit(metadata: Profile) {
|
||||
launch {
|
||||
try {
|
||||
withProfile {
|
||||
|
||||
@@ -5,15 +5,12 @@ import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.github.kr328.clash.adapter.ProfileAdapter
|
||||
import com.github.kr328.clash.common.ids.Intents
|
||||
import com.github.kr328.clash.common.util.componentName
|
||||
import com.github.kr328.clash.common.util.intent
|
||||
import com.github.kr328.clash.common.util.startForegroundServiceCompat
|
||||
import com.github.kr328.clash.remote.withProfile
|
||||
import com.github.kr328.clash.service.ProfileBackgroundService
|
||||
import com.github.kr328.clash.service.ProfileProvider
|
||||
import com.github.kr328.clash.service.ProfileReceiver
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import com.github.kr328.clash.weight.ProfilesMenu
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import kotlinx.android.synthetic.main.activity_profiles.*
|
||||
@@ -104,7 +101,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
|
||||
reloadMutex.unlock()
|
||||
}
|
||||
|
||||
override fun onProfileClicked(entity: ProfileMetadata) {
|
||||
override fun onProfileClicked(entity: Profile) {
|
||||
launch {
|
||||
withProfile {
|
||||
setActive(entity.id)
|
||||
@@ -112,7 +109,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMenuClicked(entity: ProfileMetadata) {
|
||||
override fun onMenuClicked(entity: Profile) {
|
||||
ProfilesMenu(this, entity, this).show()
|
||||
}
|
||||
|
||||
@@ -155,19 +152,19 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
|
||||
startForegroundServiceCompat(ProfileReceiver.buildUpdateIntentForId(id))
|
||||
}
|
||||
|
||||
override fun onOpenEditor(entity: ProfileMetadata) {
|
||||
override fun onOpenEditor(entity: Profile) {
|
||||
openEditor(entity.id)
|
||||
}
|
||||
|
||||
override fun onUpdate(entity: ProfileMetadata) {
|
||||
override fun onUpdate(entity: Profile) {
|
||||
startUpdate(entity.id)
|
||||
}
|
||||
|
||||
override fun onOpenProperties(entity: ProfileMetadata) {
|
||||
override fun onOpenProperties(entity: Profile) {
|
||||
openProperties(entity.id)
|
||||
}
|
||||
|
||||
override fun onDuplicate(entity: ProfileMetadata) {
|
||||
override fun onDuplicate(entity: Profile) {
|
||||
launch {
|
||||
withProfile {
|
||||
openProperties(acquireCloned(entity.id))
|
||||
@@ -175,7 +172,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
|
||||
}
|
||||
}
|
||||
|
||||
override fun onResetProvider(entity: ProfileMetadata) {
|
||||
override fun onResetProvider(entity: Profile) {
|
||||
launch {
|
||||
withProfile {
|
||||
clear(entity.id)
|
||||
@@ -183,7 +180,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDelete(entity: ProfileMetadata) {
|
||||
override fun onDelete(entity: Profile) {
|
||||
launch {
|
||||
withProfile {
|
||||
delete(entity.id)
|
||||
|
||||
@@ -9,7 +9,7 @@ import android.widget.TextView
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.github.kr328.clash.R
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import com.github.kr328.clash.utils.IntervalUtils
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
@@ -17,12 +17,12 @@ import kotlinx.coroutines.withContext
|
||||
class ProfileAdapter(private val context: Context, private val callback: Callback) :
|
||||
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
||||
interface Callback {
|
||||
fun onProfileClicked(entity: ProfileMetadata)
|
||||
fun onMenuClicked(entity: ProfileMetadata)
|
||||
fun onProfileClicked(entity: Profile)
|
||||
fun onMenuClicked(entity: Profile)
|
||||
fun onNewProfile()
|
||||
}
|
||||
|
||||
private var entities: List<ProfileMetadata> = emptyList()
|
||||
private var entities: List<Profile> = emptyList()
|
||||
|
||||
class EntityHolder(view: View) : RecyclerView.ViewHolder(view) {
|
||||
val root: View = view.findViewById(R.id.root)
|
||||
@@ -37,7 +37,7 @@ class ProfileAdapter(private val context: Context, private val callback: Callbac
|
||||
val root: View = view.findViewById(R.id.root)
|
||||
}
|
||||
|
||||
suspend fun setEntitiesAsync(new: List<ProfileMetadata>) {
|
||||
suspend fun setEntitiesAsync(new: List<Profile>) {
|
||||
val old = withContext(Dispatchers.Main) {
|
||||
entities
|
||||
}
|
||||
@@ -118,13 +118,13 @@ class ProfileAdapter(private val context: Context, private val callback: Callbac
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTypeName(type: ProfileMetadata.Type): CharSequence {
|
||||
private fun getTypeName(type: Profile.Type): CharSequence {
|
||||
return when (type) {
|
||||
ProfileMetadata.Type.FILE ->
|
||||
Profile.Type.FILE ->
|
||||
context.getText(R.string.file)
|
||||
ProfileMetadata.Type.URL ->
|
||||
Profile.Type.URL ->
|
||||
context.getText(R.string.url)
|
||||
ProfileMetadata.Type.EXTERNAL ->
|
||||
Profile.Type.EXTERNAL ->
|
||||
context.getText(R.string.external)
|
||||
else ->
|
||||
context.getText(R.string.unknown)
|
||||
|
||||
@@ -16,7 +16,7 @@ import com.github.kr328.clash.Constants
|
||||
import com.github.kr328.clash.R
|
||||
import com.github.kr328.clash.design.common.TextInput
|
||||
import com.github.kr328.clash.design.view.CommonUiLayout
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata.Type
|
||||
import com.github.kr328.clash.service.model.Profile.Type
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
|
||||
class ProfileEditFragment(
|
||||
|
||||
@@ -4,13 +4,13 @@ import android.os.RemoteException
|
||||
import com.github.kr328.clash.service.IProfileService
|
||||
import com.github.kr328.clash.service.transact.IStreamCallback
|
||||
import com.github.kr328.clash.service.transact.ParcelableContainer
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ProfileClient(private val service: IProfileService) {
|
||||
suspend fun acquireUnused(type: ProfileMetadata.Type) = withContext(Dispatchers.IO) {
|
||||
suspend fun acquireUnused(type: Profile.Type) = withContext(Dispatchers.IO) {
|
||||
service.acquireUnused(type.name)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ class ProfileClient(private val service: IProfileService) {
|
||||
service.acquireCloned(id)
|
||||
}
|
||||
|
||||
suspend fun updateMetadata(id: Long, metadata: ProfileMetadata) = withContext(Dispatchers.IO) {
|
||||
suspend fun updateMetadata(id: Long, metadata: Profile) = withContext(Dispatchers.IO) {
|
||||
service.updateMetadata(id, metadata)
|
||||
}
|
||||
|
||||
@@ -50,15 +50,15 @@ class ProfileClient(private val service: IProfileService) {
|
||||
service.clear(id)
|
||||
}
|
||||
|
||||
suspend fun queryAll(): Array<ProfileMetadata> = withContext(Dispatchers.IO) {
|
||||
suspend fun queryAll(): Array<Profile> = withContext(Dispatchers.IO) {
|
||||
service.queryAll()
|
||||
}
|
||||
|
||||
suspend fun queryActive(): ProfileMetadata? = withContext(Dispatchers.IO) {
|
||||
suspend fun queryActive(): Profile? = withContext(Dispatchers.IO) {
|
||||
service.queryActive()
|
||||
}
|
||||
|
||||
suspend fun queryById(id: Long): ProfileMetadata? = withContext(Dispatchers.IO) {
|
||||
suspend fun queryById(id: Long): Profile? = withContext(Dispatchers.IO) {
|
||||
service.queryById(id)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,21 +6,21 @@ import android.view.ViewGroup
|
||||
import androidx.annotation.ColorInt
|
||||
import com.github.kr328.clash.R
|
||||
import com.github.kr328.clash.design.view.CommonUiLayout
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import com.google.android.material.bottomsheet.BottomSheetDialog
|
||||
|
||||
class ProfilesMenu(
|
||||
context: Context,
|
||||
private val entity: ProfileMetadata,
|
||||
private val entity: Profile,
|
||||
private val callback: Callback
|
||||
) : BottomSheetDialog(context) {
|
||||
interface Callback {
|
||||
fun onOpenEditor(entity: ProfileMetadata)
|
||||
fun onUpdate(entity: ProfileMetadata)
|
||||
fun onOpenProperties(entity: ProfileMetadata)
|
||||
fun onDuplicate(entity: ProfileMetadata)
|
||||
fun onResetProvider(entity: ProfileMetadata)
|
||||
fun onDelete(entity: ProfileMetadata)
|
||||
fun onOpenEditor(entity: Profile)
|
||||
fun onUpdate(entity: Profile)
|
||||
fun onOpenProperties(entity: Profile)
|
||||
fun onDuplicate(entity: Profile)
|
||||
fun onResetProvider(entity: Profile)
|
||||
fun onDelete(entity: Profile)
|
||||
}
|
||||
|
||||
init {
|
||||
@@ -38,7 +38,7 @@ class ProfilesMenu(
|
||||
}
|
||||
|
||||
menu.build {
|
||||
if (entity.type != ProfileMetadata.Type.FILE) {
|
||||
if (entity.type != Profile.Type.FILE) {
|
||||
option(
|
||||
title = context.getString(R.string.update),
|
||||
icon = context.getDrawable(R.drawable.ic_update)
|
||||
|
||||
@@ -5,9 +5,9 @@ import android.net.Uri
|
||||
import android.webkit.URLUtil
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.service.data.ProfileDao
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata.Type
|
||||
import com.github.kr328.clash.service.model.toProfileEntity
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import com.github.kr328.clash.service.model.Profile.Type
|
||||
import com.github.kr328.clash.service.model.asEntity
|
||||
import com.github.kr328.clash.service.util.resolveBaseDir
|
||||
import com.github.kr328.clash.service.util.resolveProfileFile
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -17,7 +17,7 @@ import java.io.FileNotFoundException
|
||||
import java.util.*
|
||||
|
||||
object ProfileProcessor {
|
||||
suspend fun createOrUpdate(context: Context, metadata: ProfileMetadata) =
|
||||
suspend fun createOrUpdate(context: Context, metadata: Profile) =
|
||||
withContext(Dispatchers.IO) {
|
||||
metadata.enforceFieldValid()
|
||||
|
||||
@@ -36,9 +36,9 @@ object ProfileProcessor {
|
||||
context,
|
||||
context.resolveProfileFile(metadata.id)
|
||||
)
|
||||
).toProfileEntity()
|
||||
).asEntity()
|
||||
else
|
||||
metadata.toProfileEntity()
|
||||
metadata.asEntity()
|
||||
|
||||
if (ProfileDao.queryById(metadata.id) == null)
|
||||
ProfileDao.insert(entity)
|
||||
@@ -66,7 +66,7 @@ object ProfileProcessor {
|
||||
}.await()
|
||||
}
|
||||
|
||||
private fun ProfileMetadata.enforceFieldValid() {
|
||||
private fun Profile.enforceFieldValid() {
|
||||
when {
|
||||
id < 0 ->
|
||||
throw IllegalArgumentException("Invalid id")
|
||||
|
||||
@@ -12,7 +12,7 @@ import com.github.kr328.clash.common.ids.PendingIds
|
||||
import com.github.kr328.clash.common.util.componentName
|
||||
import com.github.kr328.clash.common.util.startForegroundServiceCompat
|
||||
import com.github.kr328.clash.service.data.ProfileDao
|
||||
import com.github.kr328.clash.service.model.toProfileMetadata
|
||||
import com.github.kr328.clash.service.model.asProfile
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
|
||||
class ProfileReceiver : BroadcastReceiver() {
|
||||
@@ -42,7 +42,7 @@ class ProfileReceiver : BroadcastReceiver() {
|
||||
}
|
||||
|
||||
suspend fun requestNextUpdate(context: Context, id: Long) {
|
||||
val metadata = ProfileDao.queryById(id)?.toProfileMetadata(context) ?: return
|
||||
val metadata = ProfileDao.queryById(id)?.asProfile(context) ?: return
|
||||
val service = context.getSystemService<AlarmManager>() ?: return
|
||||
|
||||
if (metadata.interval <= 0)
|
||||
|
||||
@@ -5,8 +5,8 @@ import android.net.Uri
|
||||
import android.os.IBinder
|
||||
import android.os.RemoteException
|
||||
import com.github.kr328.clash.service.data.ProfileDao
|
||||
import com.github.kr328.clash.service.model.ProfileMetadata
|
||||
import com.github.kr328.clash.service.model.toProfileMetadata
|
||||
import com.github.kr328.clash.service.model.Profile
|
||||
import com.github.kr328.clash.service.model.asProfile
|
||||
import com.github.kr328.clash.service.transact.IStreamCallback
|
||||
import com.github.kr328.clash.service.util.broadcastProfileChanged
|
||||
import com.github.kr328.clash.service.util.resolveBaseDir
|
||||
@@ -20,7 +20,7 @@ import kotlinx.coroutines.sync.withLock
|
||||
class ProfileService : BaseService() {
|
||||
private val service = this
|
||||
private val lock = Mutex()
|
||||
private val pending = mutableMapOf<Long, ProfileMetadata>()
|
||||
private val pending = mutableMapOf<Long, Profile>()
|
||||
private val tasks = mutableMapOf<Long, IStreamCallback?>()
|
||||
private val request = Channel<Unit>(Channel.CONFLATED)
|
||||
|
||||
@@ -57,10 +57,10 @@ class ProfileService : BaseService() {
|
||||
lock.withLock {
|
||||
val id = generateNextId()
|
||||
|
||||
pending[id] = ProfileMetadata(
|
||||
pending[id] = Profile(
|
||||
id = id,
|
||||
name = "",
|
||||
type = ProfileMetadata.Type.valueOf(type),
|
||||
type = Profile.Type.valueOf(type),
|
||||
uri = Uri.EMPTY,
|
||||
source = null,
|
||||
active = false,
|
||||
@@ -89,9 +89,9 @@ class ProfileService : BaseService() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun queryActive(): ProfileMetadata? {
|
||||
override fun queryActive(): Profile? {
|
||||
return runBlocking {
|
||||
ProfileDao.queryActive()?.toProfileMetadata(service)
|
||||
ProfileDao.queryActive()?.asProfile(service)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,13 +123,13 @@ class ProfileService : BaseService() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun queryAll(): Array<ProfileMetadata> {
|
||||
override fun queryAll(): Array<Profile> {
|
||||
return runBlocking {
|
||||
ProfileDao.queryAll().map { it.toProfileMetadata(service) }.toTypedArray()
|
||||
ProfileDao.queryAll().map { it.asProfile(service) }.toTypedArray()
|
||||
}
|
||||
}
|
||||
|
||||
override fun queryById(id: Long): ProfileMetadata? {
|
||||
override fun queryById(id: Long): Profile? {
|
||||
return runBlocking {
|
||||
lock.withLock {
|
||||
queryMetadataById(id)
|
||||
@@ -137,7 +137,7 @@ class ProfileService : BaseService() {
|
||||
}
|
||||
}
|
||||
|
||||
override fun updateMetadata(id: Long, metadata: ProfileMetadata?) {
|
||||
override fun updateMetadata(id: Long, metadata: Profile?) {
|
||||
launch {
|
||||
lock.withLock {
|
||||
pending[id] = metadata ?: return@launch
|
||||
@@ -188,8 +188,8 @@ class ProfileService : BaseService() {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun queryMetadataById(id: Long): ProfileMetadata? {
|
||||
return pending[id] ?: ProfileDao.queryById(id)?.toProfileMetadata(service)
|
||||
private suspend fun queryMetadataById(id: Long): Profile? {
|
||||
return pending[id] ?: ProfileDao.queryById(id)?.asProfile(service)
|
||||
}
|
||||
|
||||
private suspend fun generateNextId(): Long {
|
||||
|
||||
@@ -5,16 +5,16 @@ import android.net.Uri
|
||||
import com.github.kr328.clash.service.data.ProfileEntity
|
||||
import com.github.kr328.clash.service.util.resolveProfileFile
|
||||
|
||||
fun ProfileEntity.toProfileMetadata(context: Context): ProfileMetadata {
|
||||
fun ProfileEntity.asProfile(context: Context): Profile {
|
||||
val type = when (this.type) {
|
||||
ProfileEntity.TYPE_FILE -> ProfileMetadata.Type.FILE
|
||||
ProfileEntity.TYPE_URL -> ProfileMetadata.Type.URL
|
||||
ProfileEntity.TYPE_EXTERNAL -> ProfileMetadata.Type.EXTERNAL
|
||||
else -> ProfileMetadata.Type.EXTERNAL
|
||||
ProfileEntity.TYPE_FILE -> Profile.Type.FILE
|
||||
ProfileEntity.TYPE_URL -> Profile.Type.URL
|
||||
ProfileEntity.TYPE_EXTERNAL -> Profile.Type.EXTERNAL
|
||||
else -> Profile.Type.EXTERNAL
|
||||
}
|
||||
val lastModified = context.resolveProfileFile(id).lastModified()
|
||||
|
||||
return ProfileMetadata(
|
||||
return Profile(
|
||||
id = id,
|
||||
name = name,
|
||||
type = type,
|
||||
@@ -26,12 +26,12 @@ fun ProfileEntity.toProfileMetadata(context: Context): ProfileMetadata {
|
||||
)
|
||||
}
|
||||
|
||||
fun ProfileMetadata.toProfileEntity(): ProfileEntity {
|
||||
fun Profile.asEntity(): ProfileEntity {
|
||||
val type = when (this.type) {
|
||||
ProfileMetadata.Type.FILE -> ProfileEntity.TYPE_FILE
|
||||
ProfileMetadata.Type.URL -> ProfileEntity.TYPE_URL
|
||||
ProfileMetadata.Type.EXTERNAL -> ProfileEntity.TYPE_EXTERNAL
|
||||
ProfileMetadata.Type.UNKNOWN -> ProfileEntity.TYPE_UNKNOWN
|
||||
Profile.Type.FILE -> ProfileEntity.TYPE_FILE
|
||||
Profile.Type.URL -> ProfileEntity.TYPE_URL
|
||||
Profile.Type.EXTERNAL -> ProfileEntity.TYPE_EXTERNAL
|
||||
Profile.Type.UNKNOWN -> ProfileEntity.TYPE_UNKNOWN
|
||||
}
|
||||
|
||||
return ProfileEntity(
|
||||
|
||||
@@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.UseSerializers
|
||||
|
||||
@Serializable
|
||||
data class ProfileMetadata(
|
||||
data class Profile(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val type: Type,
|
||||
@@ -32,12 +32,12 @@ data class ProfileMetadata(
|
||||
return 0
|
||||
}
|
||||
|
||||
companion object CREATOR : Parcelable.Creator<ProfileMetadata> {
|
||||
override fun createFromParcel(parcel: Parcel): ProfileMetadata {
|
||||
companion object CREATOR : Parcelable.Creator<Profile> {
|
||||
override fun createFromParcel(parcel: Parcel): Profile {
|
||||
return Parcels.load(serializer(), parcel)
|
||||
}
|
||||
|
||||
override fun newArray(size: Int): Array<ProfileMetadata?> {
|
||||
override fun newArray(size: Int): Array<Profile?> {
|
||||
return arrayOfNulls(size)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user