-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Break behavior out into its own screen
- Loading branch information
Showing
80 changed files
with
1,442 additions
and
709 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
32 changes: 32 additions & 0 deletions
32
app/src/main/java/com/pyamsoft/tetherfi/behavior/BehaviorComponent.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright 2024 pyamsoft | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.pyamsoft.tetherfi.behavior | ||
|
||
import androidx.annotation.CheckResult | ||
import dagger.Subcomponent | ||
|
||
@Subcomponent | ||
internal interface BehaviorComponent { | ||
|
||
fun inject(injector: BehaviorInjector) | ||
|
||
@Subcomponent.Factory | ||
interface Factory { | ||
|
||
@CheckResult fun create(): BehaviorComponent | ||
} | ||
} |
195 changes: 195 additions & 0 deletions
195
app/src/main/java/com/pyamsoft/tetherfi/behavior/BehaviorEntry.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 |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright 2024 pyamsoft | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at: | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.pyamsoft.tetherfi.behavior | ||
|
||
import android.provider.Settings | ||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.rememberUpdatedState | ||
import androidx.compose.ui.Modifier | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.compose.LifecycleEventEffect | ||
import androidx.lifecycle.compose.LocalLifecycleOwner | ||
import androidx.lifecycle.lifecycleScope | ||
import com.pyamsoft.pydroid.arch.SaveStateDisposableEffect | ||
import com.pyamsoft.pydroid.bus.EventBus | ||
import com.pyamsoft.pydroid.bus.EventConsumer | ||
import com.pyamsoft.pydroid.ui.inject.ComposableInjector | ||
import com.pyamsoft.pydroid.ui.inject.rememberComposableInjector | ||
import com.pyamsoft.pydroid.ui.util.rememberNotNull | ||
import com.pyamsoft.tetherfi.ObjectGraph | ||
import com.pyamsoft.tetherfi.status.PermissionRequests | ||
import com.pyamsoft.tetherfi.status.PermissionResponse | ||
import com.pyamsoft.tetherfi.ui.ServerViewState | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.launch | ||
|
||
internal class BehaviorInjector : ComposableInjector() { | ||
|
||
@JvmField @Inject internal var viewModel: BehaviorViewModeler? = null | ||
|
||
@JvmField @Inject internal var permissionRequestBus: EventBus<PermissionRequests>? = null | ||
|
||
@JvmField @Inject internal var permissionResponseBus: EventConsumer<PermissionResponse>? = null | ||
|
||
override fun onInject(activity: ComponentActivity) { | ||
ObjectGraph.ActivityScope.retrieve(activity).plusBehavior().create().inject(this) | ||
} | ||
|
||
override fun onDispose() { | ||
viewModel = null | ||
permissionRequestBus = null | ||
permissionResponseBus = null | ||
} | ||
} | ||
|
||
/** Sets up permission request interaction */ | ||
@Composable | ||
private fun RegisterPermissionRequests( | ||
permissionResponseBus: Flow<PermissionResponse>, | ||
onRefreshSystemInfo: CoroutineScope.() -> Unit, | ||
) { | ||
// Create requesters | ||
val handleRefreshSystemInfo by rememberUpdatedState(onRefreshSystemInfo) | ||
|
||
LaunchedEffect( | ||
permissionResponseBus, | ||
) { | ||
// See MainActivity | ||
permissionResponseBus.flowOn(context = Dispatchers.Default).also { f -> | ||
launch(context = Dispatchers.Default) { | ||
f.collect { resp -> | ||
when (resp) { | ||
is PermissionResponse.RefreshNotification -> { | ||
// Call to the VM to refresh info | ||
handleRefreshSystemInfo(this) | ||
} | ||
is PermissionResponse.ToggleProxy -> { | ||
// Blank | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** On mount hooks */ | ||
@Composable | ||
private fun MountHooks( | ||
viewModel: BehaviorViewModeler, | ||
permissionResponseBus: Flow<PermissionResponse>, | ||
onRefreshConnection: () -> Unit | ||
) { | ||
val scope = rememberCoroutineScope() | ||
|
||
val handleRefreshSystemInfo by rememberUpdatedState { s: CoroutineScope -> | ||
viewModel.handleRefreshSystemInfo(scope = s) | ||
} | ||
|
||
// As early as possible because of Lifecycle quirks | ||
RegisterPermissionRequests( | ||
permissionResponseBus = permissionResponseBus, | ||
onRefreshSystemInfo = { handleRefreshSystemInfo(this) }, | ||
) | ||
|
||
SaveStateDisposableEffect(viewModel) | ||
|
||
LaunchedEffect( | ||
viewModel, | ||
) { | ||
viewModel.bind(scope = this) | ||
} | ||
|
||
val handleRefreshConnectionInfo by rememberUpdatedState { onRefreshConnection() } | ||
val bindLifecycleResumed by rememberUpdatedState { | ||
viewModel.bindLifecycleResumed( | ||
scope = scope, | ||
onRefreshConnectionInfo = { handleRefreshConnectionInfo() }, | ||
) | ||
} | ||
LifecycleEventEffect( | ||
event = Lifecycle.Event.ON_RESUME, | ||
) { | ||
bindLifecycleResumed() | ||
} | ||
} | ||
|
||
@Composable | ||
fun BehaviorEntry( | ||
modifier: Modifier = Modifier, | ||
appName: String, | ||
serverViewState: ServerViewState, | ||
|
||
// Actions | ||
onRefreshConnection: () -> Unit, | ||
onLaunchIntent: (String) -> Unit, | ||
) { | ||
val component = rememberComposableInjector { BehaviorInjector() } | ||
val viewModel = rememberNotNull(component.viewModel) | ||
val permissionRequestBus = rememberNotNull(component.permissionRequestBus) | ||
val permissionResponseBus = rememberNotNull(component.permissionResponseBus) | ||
|
||
// Use the LifecycleOwner.CoroutineScope (Activity usually) | ||
// so that the scope does not die because of navigation events | ||
val owner = LocalLifecycleOwner.current | ||
val lifecycleScope = owner.lifecycleScope | ||
|
||
// Hooks that run on mount | ||
MountHooks( | ||
viewModel = viewModel, | ||
permissionResponseBus = permissionResponseBus, | ||
onRefreshConnection = onRefreshConnection, | ||
) | ||
|
||
BehaviorScreen( | ||
modifier = modifier, | ||
state = viewModel, | ||
serverViewState = serverViewState, | ||
appName = appName, | ||
onOpenBatterySettings = { | ||
onLaunchIntent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS) | ||
}, | ||
onRequestNotificationPermission = { | ||
lifecycleScope.launch(context = Dispatchers.Default) { | ||
// See MainActivity | ||
permissionRequestBus.emit(PermissionRequests.Notification) | ||
} | ||
}, | ||
onToggleIgnoreVpn = { viewModel.handleToggleTweak(BehaviorViewTweaks.IGNORE_VPN) }, | ||
onToggleIgnoreLocation = { viewModel.handleToggleTweak(BehaviorViewTweaks.IGNORE_LOCATION) }, | ||
onToggleShutdownWithNoClients = { | ||
viewModel.handleToggleTweak(BehaviorViewTweaks.SHUTDOWN_NO_CLIENTS) | ||
}, | ||
onToggleKeepScreenOn = { viewModel.handleToggleTweak(BehaviorViewTweaks.KEEP_SCREEN_ON) }, | ||
onShowPowerBalance = { viewModel.handleOpenDialog(BehaviorViewDialogs.POWER_BALANCE) }, | ||
onHidePowerBalance = { viewModel.handleCloseDialog(BehaviorViewDialogs.POWER_BALANCE) }, | ||
onUpdatePowerBalance = { viewModel.handleUpdatePowerBalance(it) }, | ||
onSelectBroadcastType = { viewModel.handleUpdateBroadcastType(it) }, | ||
onSelectPreferredNetwork = { viewModel.handleUpdatePreferredNetwork(it) }, | ||
onHideSocketTimeout = { viewModel.handleCloseDialog(BehaviorViewDialogs.SOCKET_TIMEOUT) }, | ||
onShowSocketTimeout = { viewModel.handleOpenDialog(BehaviorViewDialogs.SOCKET_TIMEOUT) }, | ||
onUpdateSocketTimeout = { viewModel.handleUpdateSocketTimeout(it) }, | ||
) | ||
} |
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
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
Oops, something went wrong.