-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac4f143
commit 6569145
Showing
9 changed files
with
167 additions
and
97 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
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
58 changes: 58 additions & 0 deletions
58
app/src/main/java/io/vonley/mi/ui/screens/home/presentation/HomeViewModel.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,58 @@ | ||
package io.vonley.mi.ui.screens.home.presentation | ||
|
||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import io.vonley.mi.di.annotations.SharedPreferenceStorage | ||
import io.vonley.mi.di.network.MiServer | ||
import io.vonley.mi.di.network.impl.MiServerImpl | ||
import io.vonley.mi.models.Device | ||
import io.vonley.mi.models.MiCMDResponse | ||
import io.vonley.mi.models.MiResponse | ||
import io.vonley.mi.utils.SharedPreferenceManager | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.update | ||
import javax.inject.Inject | ||
|
||
sealed class HomeState { | ||
data object Empty : HomeState() | ||
data class Log(val device: Device, val logs: ArrayList<MiCMDResponse>) : HomeState() | ||
} | ||
|
||
@HiltViewModel | ||
class HomeViewModel @Inject constructor( | ||
@SharedPreferenceStorage val manager: SharedPreferenceManager, | ||
val jb: MiServer, | ||
) : ViewModel(), MiServerImpl.MiJbServerListener { | ||
|
||
private val _state = MutableStateFlow<HomeState>(HomeState.Empty) | ||
val state: StateFlow<HomeState> get() = _state | ||
|
||
init { | ||
jb.add(this) | ||
} | ||
|
||
override fun onDeviceConnected(device: Device) = Unit | ||
override fun onLog(string: String) = Unit | ||
override fun onJailbreakSucceeded(message: String) = Unit | ||
override fun onJailbreakFailed(message: String) = Unit | ||
override fun onPayloadSent(msg: String?) = Unit | ||
override fun onUnsupported(s: String) = Unit | ||
override fun onSendPayloadAttempt(attempt: Int) = Unit | ||
|
||
override fun onCommand(mi: MiResponse<MiResponse.Cmd>) { | ||
_state.update { currState -> | ||
when (currState) { | ||
HomeState.Empty -> HomeState.Log(mi.device!!, arrayListOf(mi)) | ||
is HomeState.Log -> if (currState.device != mi.device) { | ||
currState.copy(mi.device!!, arrayListOf(mi)) | ||
} else { | ||
val logs = currState.logs | ||
logs.add(mi) | ||
currState.copy(currState.device, logs) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...mi/ui/screens/home/article/ArticleCard.kt → .../home/presentation/article/ArticleCard.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
2 changes: 1 addition & 1 deletion
2
...i/screens/home/changelog/ChangelogCard.kt → ...e/presentation/changelog/ChangelogCard.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
2 changes: 1 addition & 1 deletion
2
.../vonley/mi/ui/screens/home/log/LogCard.kt → .../screens/home/presentation/log/LogCard.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
Oops, something went wrong.