|  |  | @ -3,6 +3,7 @@ package io.legado.app.ui.rss.source.edit | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.app.Application |  |  |  | import android.app.Application | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.content.ClipboardManager |  |  |  | import android.content.ClipboardManager | 
			
		
	
		
		
			
				
					
					|  |  |  | import android.content.Context |  |  |  | import android.content.Context | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | import android.content.Intent | 
			
		
	
		
		
			
				
					
					|  |  |  | import androidx.lifecycle.MutableLiveData |  |  |  | import androidx.lifecycle.MutableLiveData | 
			
		
	
		
		
			
				
					
					|  |  |  | import io.legado.app.App |  |  |  | import io.legado.app.App | 
			
		
	
		
		
			
				
					
					|  |  |  | import io.legado.app.base.BaseViewModel |  |  |  | import io.legado.app.base.BaseViewModel | 
			
		
	
	
		
		
			
				
					|  |  | @ -13,26 +14,35 @@ import io.legado.app.utils.fromJsonObject | 
			
		
	
		
		
			
				
					
					|  |  |  | class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { |  |  |  | class RssSourceEditViewModel(application: Application) : BaseViewModel(application) { | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     val sourceLiveData: MutableLiveData<RssSource> = MutableLiveData() |  |  |  |     val sourceLiveData: MutableLiveData<RssSource> = MutableLiveData() | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     var oldSourceUrl: String? = null | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     fun setSource(key: String) { |  |  |  |     fun initData(intent: Intent) { | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |         execute { |  |  |  |         execute { | 
			
		
	
		
		
			
				
					
					|  |  |  |             App.db.rssSourceDao().getByKey(key)?.let { |  |  |  |             val key = intent.getStringExtra("data") | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             var source: RssSource? = null | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             if (key != null) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 source = App.db.rssSourceDao().getByKey(key) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             source?.let { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 oldSourceUrl = it.sourceUrl | 
			
		
	
		
		
			
				
					
					|  |  |  |                 sourceLiveData.postValue(it) |  |  |  |                 sourceLiveData.postValue(it) | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             } ?: let { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 sourceLiveData.postValue(RssSource().apply { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                     customOrder = App.db.rssSourceDao().maxOrder + 1 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |                 }) | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |         } |  |  |  |         } | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |     } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |     fun save(rssSource: RssSource, success: (() -> Unit)) { |  |  |  |     fun save(rssSource: RssSource, success: (() -> Unit)) { | 
			
		
	
		
		
			
				
					
					|  |  |  |         execute { |  |  |  |         execute { | 
			
		
	
		
		
			
				
					
					|  |  |  |             sourceLiveData.value?.let { |  |  |  |             oldSourceUrl?.let { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |                 if (it.sourceUrl != rssSource.sourceUrl) { |  |  |  |                 if (oldSourceUrl != rssSource.sourceUrl) { | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |                     App.db.rssSourceDao().delete(it) |  |  |  |                     App.db.rssSourceDao().delete(it) | 
			
		
	
		
		
			
				
					
					|  |  |  |                 } |  |  |  |                 } | 
			
		
	
		
		
			
				
					
					|  |  |  |             } ?: let { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |                 rssSource.customOrder = App.db.rssSourceDao().maxOrder + 1 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |             } |  |  |  |             } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |             oldSourceUrl = rssSource.sourceUrl | 
			
		
	
		
		
			
				
					
					|  |  |  |             App.db.rssSourceDao().insert(rssSource) |  |  |  |             App.db.rssSourceDao().insert(rssSource) | 
			
		
	
		
		
			
				
					
					|  |  |  |             sourceLiveData.postValue(rssSource) |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |         }.onSuccess { |  |  |  |         }.onSuccess { | 
			
		
	
		
		
			
				
					
					|  |  |  |             success() |  |  |  |             success() | 
			
		
	
		
		
			
				
					
					|  |  |  |         }.onError { |  |  |  |         }.onError { | 
			
		
	
	
		
		
			
				
					|  |  | 
 |