rename ProfileMetadata.kt -> Profile.kt

This commit is contained in:
Kr328
2020-04-14 10:47:14 +08:00
parent 1536c9f056
commit 57b8ce3d7f
12 changed files with 74 additions and 77 deletions

View File

@@ -15,7 +15,7 @@ import android.widget.BaseAdapter
import android.widget.TextView import android.widget.TextView
import com.github.kr328.clash.common.util.intent import com.github.kr328.clash.common.util.intent
import com.github.kr328.clash.remote.withProfile 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.android.synthetic.main.activity_create_profile.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch

View File

@@ -6,7 +6,7 @@ import android.view.View
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import com.github.kr328.clash.fragment.ProfileEditFragment import com.github.kr328.clash.fragment.ProfileEditFragment
import com.github.kr328.clash.remote.withProfile 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 com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_profile_edit.* import kotlinx.android.synthetic.main.activity_profile_edit.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -101,7 +101,7 @@ class ProfileEditActivity : BaseActivity() {
super.onDestroy() super.onDestroy()
} }
private fun commit(metadata: ProfileMetadata) { private fun commit(metadata: Profile) {
launch { launch {
try { try {
withProfile { withProfile {

View File

@@ -5,15 +5,12 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import com.github.kr328.clash.adapter.ProfileAdapter 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.intent
import com.github.kr328.clash.common.util.startForegroundServiceCompat import com.github.kr328.clash.common.util.startForegroundServiceCompat
import com.github.kr328.clash.remote.withProfile 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.ProfileProvider
import com.github.kr328.clash.service.ProfileReceiver 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.github.kr328.clash.weight.ProfilesMenu
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_profiles.* import kotlinx.android.synthetic.main.activity_profiles.*
@@ -104,7 +101,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
reloadMutex.unlock() reloadMutex.unlock()
} }
override fun onProfileClicked(entity: ProfileMetadata) { override fun onProfileClicked(entity: Profile) {
launch { launch {
withProfile { withProfile {
setActive(entity.id) 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() ProfilesMenu(this, entity, this).show()
} }
@@ -155,19 +152,19 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C
startForegroundServiceCompat(ProfileReceiver.buildUpdateIntentForId(id)) startForegroundServiceCompat(ProfileReceiver.buildUpdateIntentForId(id))
} }
override fun onOpenEditor(entity: ProfileMetadata) { override fun onOpenEditor(entity: Profile) {
openEditor(entity.id) openEditor(entity.id)
} }
override fun onUpdate(entity: ProfileMetadata) { override fun onUpdate(entity: Profile) {
startUpdate(entity.id) startUpdate(entity.id)
} }
override fun onOpenProperties(entity: ProfileMetadata) { override fun onOpenProperties(entity: Profile) {
openProperties(entity.id) openProperties(entity.id)
} }
override fun onDuplicate(entity: ProfileMetadata) { override fun onDuplicate(entity: Profile) {
launch { launch {
withProfile { withProfile {
openProperties(acquireCloned(entity.id)) 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 { launch {
withProfile { withProfile {
clear(entity.id) 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 { launch {
withProfile { withProfile {
delete(entity.id) delete(entity.id)

View File

@@ -9,7 +9,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.kr328.clash.R 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 com.github.kr328.clash.utils.IntervalUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
@@ -17,12 +17,12 @@ import kotlinx.coroutines.withContext
class ProfileAdapter(private val context: Context, private val callback: Callback) : class ProfileAdapter(private val context: Context, private val callback: Callback) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() { RecyclerView.Adapter<RecyclerView.ViewHolder>() {
interface Callback { interface Callback {
fun onProfileClicked(entity: ProfileMetadata) fun onProfileClicked(entity: Profile)
fun onMenuClicked(entity: ProfileMetadata) fun onMenuClicked(entity: Profile)
fun onNewProfile() fun onNewProfile()
} }
private var entities: List<ProfileMetadata> = emptyList() private var entities: List<Profile> = emptyList()
class EntityHolder(view: View) : RecyclerView.ViewHolder(view) { class EntityHolder(view: View) : RecyclerView.ViewHolder(view) {
val root: View = view.findViewById(R.id.root) 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) 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) { val old = withContext(Dispatchers.Main) {
entities 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) { return when (type) {
ProfileMetadata.Type.FILE -> Profile.Type.FILE ->
context.getText(R.string.file) context.getText(R.string.file)
ProfileMetadata.Type.URL -> Profile.Type.URL ->
context.getText(R.string.url) context.getText(R.string.url)
ProfileMetadata.Type.EXTERNAL -> Profile.Type.EXTERNAL ->
context.getText(R.string.external) context.getText(R.string.external)
else -> else ->
context.getText(R.string.unknown) context.getText(R.string.unknown)

View File

@@ -16,7 +16,7 @@ import com.github.kr328.clash.Constants
import com.github.kr328.clash.R import com.github.kr328.clash.R
import com.github.kr328.clash.design.common.TextInput import com.github.kr328.clash.design.common.TextInput
import com.github.kr328.clash.design.view.CommonUiLayout 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 import com.google.android.material.snackbar.Snackbar
class ProfileEditFragment( class ProfileEditFragment(

View File

@@ -4,13 +4,13 @@ import android.os.RemoteException
import com.github.kr328.clash.service.IProfileService import com.github.kr328.clash.service.IProfileService
import com.github.kr328.clash.service.transact.IStreamCallback import com.github.kr328.clash.service.transact.IStreamCallback
import com.github.kr328.clash.service.transact.ParcelableContainer 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.CompletableDeferred
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class ProfileClient(private val service: IProfileService) { 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) service.acquireUnused(type.name)
} }
@@ -18,7 +18,7 @@ class ProfileClient(private val service: IProfileService) {
service.acquireCloned(id) 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) service.updateMetadata(id, metadata)
} }
@@ -50,15 +50,15 @@ class ProfileClient(private val service: IProfileService) {
service.clear(id) service.clear(id)
} }
suspend fun queryAll(): Array<ProfileMetadata> = withContext(Dispatchers.IO) { suspend fun queryAll(): Array<Profile> = withContext(Dispatchers.IO) {
service.queryAll() service.queryAll()
} }
suspend fun queryActive(): ProfileMetadata? = withContext(Dispatchers.IO) { suspend fun queryActive(): Profile? = withContext(Dispatchers.IO) {
service.queryActive() service.queryActive()
} }
suspend fun queryById(id: Long): ProfileMetadata? = withContext(Dispatchers.IO) { suspend fun queryById(id: Long): Profile? = withContext(Dispatchers.IO) {
service.queryById(id) service.queryById(id)
} }

View File

@@ -6,21 +6,21 @@ import android.view.ViewGroup
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import com.github.kr328.clash.R import com.github.kr328.clash.R
import com.github.kr328.clash.design.view.CommonUiLayout 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 import com.google.android.material.bottomsheet.BottomSheetDialog
class ProfilesMenu( class ProfilesMenu(
context: Context, context: Context,
private val entity: ProfileMetadata, private val entity: Profile,
private val callback: Callback private val callback: Callback
) : BottomSheetDialog(context) { ) : BottomSheetDialog(context) {
interface Callback { interface Callback {
fun onOpenEditor(entity: ProfileMetadata) fun onOpenEditor(entity: Profile)
fun onUpdate(entity: ProfileMetadata) fun onUpdate(entity: Profile)
fun onOpenProperties(entity: ProfileMetadata) fun onOpenProperties(entity: Profile)
fun onDuplicate(entity: ProfileMetadata) fun onDuplicate(entity: Profile)
fun onResetProvider(entity: ProfileMetadata) fun onResetProvider(entity: Profile)
fun onDelete(entity: ProfileMetadata) fun onDelete(entity: Profile)
} }
init { init {
@@ -38,7 +38,7 @@ class ProfilesMenu(
} }
menu.build { menu.build {
if (entity.type != ProfileMetadata.Type.FILE) { if (entity.type != Profile.Type.FILE) {
option( option(
title = context.getString(R.string.update), title = context.getString(R.string.update),
icon = context.getDrawable(R.drawable.ic_update) icon = context.getDrawable(R.drawable.ic_update)

View File

@@ -5,9 +5,9 @@ import android.net.Uri
import android.webkit.URLUtil import android.webkit.URLUtil
import com.github.kr328.clash.core.Clash import com.github.kr328.clash.core.Clash
import com.github.kr328.clash.service.data.ProfileDao import com.github.kr328.clash.service.data.ProfileDao
import com.github.kr328.clash.service.model.ProfileMetadata import com.github.kr328.clash.service.model.Profile
import com.github.kr328.clash.service.model.ProfileMetadata.Type import com.github.kr328.clash.service.model.Profile.Type
import com.github.kr328.clash.service.model.toProfileEntity import com.github.kr328.clash.service.model.asEntity
import com.github.kr328.clash.service.util.resolveBaseDir import com.github.kr328.clash.service.util.resolveBaseDir
import com.github.kr328.clash.service.util.resolveProfileFile import com.github.kr328.clash.service.util.resolveProfileFile
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -17,7 +17,7 @@ import java.io.FileNotFoundException
import java.util.* import java.util.*
object ProfileProcessor { object ProfileProcessor {
suspend fun createOrUpdate(context: Context, metadata: ProfileMetadata) = suspend fun createOrUpdate(context: Context, metadata: Profile) =
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
metadata.enforceFieldValid() metadata.enforceFieldValid()
@@ -36,9 +36,9 @@ object ProfileProcessor {
context, context,
context.resolveProfileFile(metadata.id) context.resolveProfileFile(metadata.id)
) )
).toProfileEntity() ).asEntity()
else else
metadata.toProfileEntity() metadata.asEntity()
if (ProfileDao.queryById(metadata.id) == null) if (ProfileDao.queryById(metadata.id) == null)
ProfileDao.insert(entity) ProfileDao.insert(entity)
@@ -66,7 +66,7 @@ object ProfileProcessor {
}.await() }.await()
} }
private fun ProfileMetadata.enforceFieldValid() { private fun Profile.enforceFieldValid() {
when { when {
id < 0 -> id < 0 ->
throw IllegalArgumentException("Invalid id") throw IllegalArgumentException("Invalid id")

View File

@@ -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.componentName
import com.github.kr328.clash.common.util.startForegroundServiceCompat import com.github.kr328.clash.common.util.startForegroundServiceCompat
import com.github.kr328.clash.service.data.ProfileDao 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 import kotlinx.coroutines.sync.Mutex
class ProfileReceiver : BroadcastReceiver() { class ProfileReceiver : BroadcastReceiver() {
@@ -42,7 +42,7 @@ class ProfileReceiver : BroadcastReceiver() {
} }
suspend fun requestNextUpdate(context: Context, id: Long) { 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 val service = context.getSystemService<AlarmManager>() ?: return
if (metadata.interval <= 0) if (metadata.interval <= 0)

View File

@@ -5,8 +5,8 @@ import android.net.Uri
import android.os.IBinder import android.os.IBinder
import android.os.RemoteException import android.os.RemoteException
import com.github.kr328.clash.service.data.ProfileDao import com.github.kr328.clash.service.data.ProfileDao
import com.github.kr328.clash.service.model.ProfileMetadata import com.github.kr328.clash.service.model.Profile
import com.github.kr328.clash.service.model.toProfileMetadata import com.github.kr328.clash.service.model.asProfile
import com.github.kr328.clash.service.transact.IStreamCallback import com.github.kr328.clash.service.transact.IStreamCallback
import com.github.kr328.clash.service.util.broadcastProfileChanged import com.github.kr328.clash.service.util.broadcastProfileChanged
import com.github.kr328.clash.service.util.resolveBaseDir import com.github.kr328.clash.service.util.resolveBaseDir
@@ -20,7 +20,7 @@ import kotlinx.coroutines.sync.withLock
class ProfileService : BaseService() { class ProfileService : BaseService() {
private val service = this private val service = this
private val lock = Mutex() private val lock = Mutex()
private val pending = mutableMapOf<Long, ProfileMetadata>() private val pending = mutableMapOf<Long, Profile>()
private val tasks = mutableMapOf<Long, IStreamCallback?>() private val tasks = mutableMapOf<Long, IStreamCallback?>()
private val request = Channel<Unit>(Channel.CONFLATED) private val request = Channel<Unit>(Channel.CONFLATED)
@@ -57,10 +57,10 @@ class ProfileService : BaseService() {
lock.withLock { lock.withLock {
val id = generateNextId() val id = generateNextId()
pending[id] = ProfileMetadata( pending[id] = Profile(
id = id, id = id,
name = "", name = "",
type = ProfileMetadata.Type.valueOf(type), type = Profile.Type.valueOf(type),
uri = Uri.EMPTY, uri = Uri.EMPTY,
source = null, source = null,
active = false, active = false,
@@ -89,9 +89,9 @@ class ProfileService : BaseService() {
} }
} }
override fun queryActive(): ProfileMetadata? { override fun queryActive(): Profile? {
return runBlocking { 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 { 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 { return runBlocking {
lock.withLock { lock.withLock {
queryMetadataById(id) queryMetadataById(id)
@@ -137,7 +137,7 @@ class ProfileService : BaseService() {
} }
} }
override fun updateMetadata(id: Long, metadata: ProfileMetadata?) { override fun updateMetadata(id: Long, metadata: Profile?) {
launch { launch {
lock.withLock { lock.withLock {
pending[id] = metadata ?: return@launch pending[id] = metadata ?: return@launch
@@ -188,8 +188,8 @@ class ProfileService : BaseService() {
} }
} }
private suspend fun queryMetadataById(id: Long): ProfileMetadata? { private suspend fun queryMetadataById(id: Long): Profile? {
return pending[id] ?: ProfileDao.queryById(id)?.toProfileMetadata(service) return pending[id] ?: ProfileDao.queryById(id)?.asProfile(service)
} }
private suspend fun generateNextId(): Long { private suspend fun generateNextId(): Long {

View File

@@ -5,16 +5,16 @@ import android.net.Uri
import com.github.kr328.clash.service.data.ProfileEntity import com.github.kr328.clash.service.data.ProfileEntity
import com.github.kr328.clash.service.util.resolveProfileFile 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) { val type = when (this.type) {
ProfileEntity.TYPE_FILE -> ProfileMetadata.Type.FILE ProfileEntity.TYPE_FILE -> Profile.Type.FILE
ProfileEntity.TYPE_URL -> ProfileMetadata.Type.URL ProfileEntity.TYPE_URL -> Profile.Type.URL
ProfileEntity.TYPE_EXTERNAL -> ProfileMetadata.Type.EXTERNAL ProfileEntity.TYPE_EXTERNAL -> Profile.Type.EXTERNAL
else -> ProfileMetadata.Type.EXTERNAL else -> Profile.Type.EXTERNAL
} }
val lastModified = context.resolveProfileFile(id).lastModified() val lastModified = context.resolveProfileFile(id).lastModified()
return ProfileMetadata( return Profile(
id = id, id = id,
name = name, name = name,
type = type, 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) { val type = when (this.type) {
ProfileMetadata.Type.FILE -> ProfileEntity.TYPE_FILE Profile.Type.FILE -> ProfileEntity.TYPE_FILE
ProfileMetadata.Type.URL -> ProfileEntity.TYPE_URL Profile.Type.URL -> ProfileEntity.TYPE_URL
ProfileMetadata.Type.EXTERNAL -> ProfileEntity.TYPE_EXTERNAL Profile.Type.EXTERNAL -> ProfileEntity.TYPE_EXTERNAL
ProfileMetadata.Type.UNKNOWN -> ProfileEntity.TYPE_UNKNOWN Profile.Type.UNKNOWN -> ProfileEntity.TYPE_UNKNOWN
} }
return ProfileEntity( return ProfileEntity(

View File

@@ -10,7 +10,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers import kotlinx.serialization.UseSerializers
@Serializable @Serializable
data class ProfileMetadata( data class Profile(
val id: Long, val id: Long,
val name: String, val name: String,
val type: Type, val type: Type,
@@ -32,12 +32,12 @@ data class ProfileMetadata(
return 0 return 0
} }
companion object CREATOR : Parcelable.Creator<ProfileMetadata> { companion object CREATOR : Parcelable.Creator<Profile> {
override fun createFromParcel(parcel: Parcel): ProfileMetadata { override fun createFromParcel(parcel: Parcel): Profile {
return Parcels.load(serializer(), parcel) return Parcels.load(serializer(), parcel)
} }
override fun newArray(size: Int): Array<ProfileMetadata?> { override fun newArray(size: Int): Array<Profile?> {
return arrayOfNulls(size) return arrayOfNulls(size)
} }
} }