Skip to content

Commit

Permalink
~~
Browse files Browse the repository at this point in the history
  • Loading branch information
Zensey committed Mar 16, 2023
1 parent 7e37ca6 commit 4cffbd0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,23 @@ class MysteriumAndroidCoreService : VpnService(), KoinComponent {
}

private fun startMobileProviderService(active: Boolean) {

mobileNode?.let {
isProviderActive = active
try {
if (active) {
println("startMobileProviderService >>>> 1")
it.startProvider()
} else {
println("startMobileProviderService >>>> 0")
it.stopProvider()
}

} catch (e: Exception) {
isProviderActive = !active
println(e)
}
println("startMobileProviderService >>>> >>>>")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ class ProviderActivity : BaseActivity() {
initToolbar(binding.manualConnectToolbar)

viewModel.providerUpdate.observe(this) {
binding.providerModeSwitch.tag = true
binding.providerModeSwitch.isChecked = it.active
binding.providerModeSwitch.tag = null
}

viewModel.providerServiceStatus.observe(this) {
Expand All @@ -70,7 +72,11 @@ class ProviderActivity : BaseActivity() {
finish()
}
binding.providerModeSwitch.setOnCheckedChangeListener { _, isChecked ->
viewModel.toggleProvider(isChecked)
val v = binding.providerModeSwitch.tag
println("setOnCheckedChangeListener >>>>>> $v")
if (binding.providerModeSwitch.tag == null) {
viewModel.toggleProvider(isChecked)
}
}
binding.buttonUI.setOnClickListener {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {
private lateinit var appNotificationManager: AppNotificationManager
private var deferredNode = DeferredNode()


val handler = CoroutineExceptionHandler { _, exception ->
Log.i(TAG, exception.localizedMessage ?: exception.toString())
}
Expand All @@ -56,7 +57,7 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {
startDeferredNode()

val initialState = ProviderState(
active = getIsProvider(),
active = getIsProviderActive(),
)
_providerUpdate.postValue(initialState)

Expand All @@ -68,27 +69,42 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {
"scraping" -> servicesState.active[2] = running
}
_providerServiceStatus.postValue(servicesState)

// make provider switch state "false" if all services are disabled
var allDisabled = true
for (x in servicesState.active) {
if (x) {
allDisabled = false
}
}
if (allDisabled) {
_providerUpdate.postValue(ProviderState(false))
}

}
}
}

fun toggleProvider(isChecked: Boolean) {
println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> toggleProvider $isChecked")

CoroutineScope(Dispatchers.IO).launch {
coreService?.let {
if (isChecked) {
if (it.isProviderActive()) {
return@let
}
// if (it.isProviderActive()) {
// return@let
// }

it.startProvider(true)
it.startForegroundWithNotification(
NotificationChannels.PROVIDER_NOTIFICATION,
appNotificationManager.createProviderNotification()
)

} else {
if (!it.isProviderActive()) {
return@let
}
// if (!it.isProviderActive()) {
// return@let
// }

it.startProvider(false)
it.stopForeground()
Expand All @@ -112,7 +128,7 @@ class ProviderViewModel(useCaseProvider: UseCaseProvider) : ViewModel() {
connectionUseCase.getIdentity()
}

private fun getIsProvider(): Boolean {
private fun getIsProviderActive(): Boolean {
coreService?.let {
return it.isProviderActive()
}
Expand Down

0 comments on commit 4cffbd0

Please sign in to comment.