Skip to content

Commit 5b918c1

Browse files
authored
Merge pull request #7 from MangoCubes/dev
Dev
2 parents db31213 + 9cbbc03 commit 5b918c1

38 files changed

+591
-478
lines changed

app/src/main/java/ch/skew/remotrix/AccountList.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import androidx.work.WorkManager
3636
import ch.skew.remotrix.classes.Account
3737
import ch.skew.remotrix.components.DelAccountDialog
3838
import ch.skew.remotrix.components.ScreenHelper
39-
import ch.skew.remotrix.data.accountDB.AccountEvent
39+
import ch.skew.remotrix.data.RemotrixDB
4040
import ch.skew.remotrix.works.SendMsgWorker
4141
import kotlinx.coroutines.CoroutineScope
4242
import kotlinx.coroutines.launch
@@ -53,22 +53,21 @@ import java.util.Locale
5353
@Composable
5454
@Preview
5555
fun AccountListPreview() {
56-
AccountList(listOf(), {}, {}, {})
56+
AccountList(listOf(), {}, {})
5757
}
5858

5959
@OptIn(ExperimentalMaterial3Api::class)
6060
@Composable
6161
fun AccountList(
6262
accounts: List<Account>,
63-
onAccountEvent: (AccountEvent) -> Unit,
6463
onClickGoBack: () -> Unit,
6564
onClickNewAccount: () -> Unit
6665
){
6766
val askDel = remember{ mutableStateOf<Account?>(null) }
6867
val scope = rememberCoroutineScope()
6968
val context = LocalContext.current
7069
DelAccountDialog(close = { askDel.value = null }, confirm = {
71-
deleteAccount(context, scope, askDel.value, onAccountEvent) { askDel.value = null }
70+
deleteAccount(context, scope, askDel.value) { askDel.value = null }
7271
}, accountId = askDel.value?.userId)
7372
Scaffold(
7473
topBar = {
@@ -107,7 +106,6 @@ fun deleteAccount(
107106
context: Context,
108107
scope: CoroutineScope,
109108
account: Account?,
110-
onAccountEvent: (AccountEvent) -> Unit,
111109
close: () -> Unit
112110
) {
113111
if (account === null) return
@@ -135,7 +133,7 @@ fun deleteAccount(
135133
} catch (e: Throwable) {
136134
Toast.makeText(context, context.getString(R.string.cannot_logout_data_not_found), Toast.LENGTH_LONG).show()
137135
}
138-
onAccountEvent(AccountEvent.DeleteAccount(account.id))
136+
RemotrixDB.getInstance(context).accountDao.delete(account.id)
139137
close()
140138
}
141139
}

app/src/main/java/ch/skew/remotrix/Logs.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fun Logs(
6868
.padding(8.dp),
6969
verticalAlignment = Alignment.CenterVertically
7070
) {
71-
Text("Hide successful sends")
71+
Text(stringResource(R.string.hide_successful_sends))
7272
Spacer(modifier = Modifier.padding(start = 8.dp))
7373
Switch(
7474
checked = hideSuccesses.value,
@@ -77,10 +77,17 @@ fun Logs(
7777
}
7878
LazyColumn {
7979
items(logs) { log ->
80-
val accountUsed = accounts.find {it.id == log.senderId}
81-
val msg = if (accountUsed === null) stringResource(R.string.unknown_account_id).format(log.senderId)
82-
else stringResource(R.string.known_account_id).format(accountUsed.userId)
83-
Text("[${if(log.status === MsgStatus.MESSAGE_SENT || log.status === MsgStatus.MESSAGE_DROPPED) stringResource(R.string.success) else stringResource(R.string.failure)}] " + log.timestamp + ": " + msg)
80+
val success =
81+
log.status === MsgStatus.MESSAGE_SENT || log.status === MsgStatus.MESSAGE_DROPPED
82+
if (!(success && hideSuccesses.value)) {
83+
val accountUsed = accounts.find { it.id == log.forwarderId }
84+
val msg =
85+
if (accountUsed === null) stringResource(R.string.unknown_account_id).format(
86+
log.forwarderId
87+
)
88+
else stringResource(R.string.known_account_id).format(accountUsed.userId)
89+
Text("[${if (success) stringResource(R.string.success) else stringResource(R.string.failure)}] " + log.timestamp + ": " + msg)
90+
}
8491
}
8592
}
8693
}

app/src/main/java/ch/skew/remotrix/MainActivity.kt

Lines changed: 31 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,44 @@ import androidx.lifecycle.ViewModel
2929
import androidx.lifecycle.ViewModelProvider
3030
import androidx.navigation.compose.NavHost
3131
import androidx.navigation.compose.composable
32+
import androidx.navigation.compose.navigation
3233
import androidx.navigation.compose.rememberNavController
33-
import androidx.room.Room
3434
import ch.skew.remotrix.classes.Account
3535
import ch.skew.remotrix.classes.Destination
36+
import ch.skew.remotrix.classes.Setup
3637
import ch.skew.remotrix.components.ListHeader
3738
import ch.skew.remotrix.data.RemotrixDB
3839
import ch.skew.remotrix.data.RemotrixSettings
39-
import ch.skew.remotrix.data.accountDB.AccountEvent
40-
import ch.skew.remotrix.data.accountDB.AccountEventAsync
4140
import ch.skew.remotrix.data.accountDB.AccountViewModel
41+
import ch.skew.remotrix.data.forwardRuleDB.ForwardRule
42+
import ch.skew.remotrix.data.forwardRuleDB.ForwardRuleViewModel
4243
import ch.skew.remotrix.data.logDB.LogData
4344
import ch.skew.remotrix.data.logDB.LogViewModel
44-
import ch.skew.remotrix.data.sendActionDB.SendAction
45-
import ch.skew.remotrix.data.sendActionDB.SendActionEvent
46-
import ch.skew.remotrix.data.sendActionDB.SendActionViewModel
45+
import ch.skew.remotrix.setup.AdditionalInfo
46+
import ch.skew.remotrix.setup.SetManagementSpace
47+
import ch.skew.remotrix.setup.SetManagerAccount
48+
import ch.skew.remotrix.setup.Welcome
4749
import ch.skew.remotrix.ui.theme.RemotrixTheme
48-
import kotlinx.coroutines.Deferred
4950

5051

5152
class MainActivity : ComponentActivity() {
5253

53-
private val db by lazy {
54-
Room.databaseBuilder(
55-
applicationContext,
56-
RemotrixDB::class.java,
57-
"accounts.db"
58-
).build()
59-
}
60-
6154
@Suppress("UNCHECKED_CAST")
6255
private val accountViewModel by viewModels<AccountViewModel>(
6356
factoryProducer = {
6457
object: ViewModelProvider.Factory {
6558
override fun <T : ViewModel> create(modelClass: Class<T>): T {
66-
return AccountViewModel(db.accountDao) as T
59+
return AccountViewModel(RemotrixDB.getInstance(applicationContext).accountDao) as T
6760
}
6861
}
6962
}
7063
)
7164
@Suppress("UNCHECKED_CAST")
72-
private val sendActionViewModel by viewModels<SendActionViewModel>(
65+
private val forwardRuleViewModel by viewModels<ForwardRuleViewModel>(
7366
factoryProducer = {
7467
object: ViewModelProvider.Factory {
7568
override fun <T : ViewModel> create(modelClass: Class<T>): T {
76-
return SendActionViewModel(db.sendActionDao) as T
69+
return ForwardRuleViewModel(RemotrixDB.getInstance(applicationContext).forwardRuleDao) as T
7770
}
7871
}
7972
}
@@ -83,7 +76,7 @@ class MainActivity : ComponentActivity() {
8376
factoryProducer = {
8477
object: ViewModelProvider.Factory {
8578
override fun <T : ViewModel> create(modelClass: Class<T>): T {
86-
return LogViewModel(db.logDao) as T
79+
return LogViewModel(RemotrixDB.getInstance(applicationContext).logDao) as T
8780
}
8881
}
8982
}
@@ -92,13 +85,10 @@ class MainActivity : ComponentActivity() {
9285
super.onCreate(savedInstanceState)
9386
setContent {
9487
val accounts by accountViewModel.accounts.collectAsState()
95-
val sendActions by sendActionViewModel.sendActions.collectAsState()
88+
val sendActions by forwardRuleViewModel.forwardRule.collectAsState()
9689
val logs by logViewModel.logs.collectAsState()
9790
RemotrixApp(
98-
accountViewModel::onEvent,
99-
accountViewModel::onEventAsync,
10091
Account.from(accounts),
101-
sendActionViewModel::onEvent,
10292
sendActions,
10393
logs
10494
)
@@ -107,11 +97,8 @@ class MainActivity : ComponentActivity() {
10797
}
10898
@Composable
10999
fun RemotrixApp(
110-
onAccountEvent: (AccountEvent) -> Unit,
111-
onAccountEventAsync: (AccountEventAsync) -> Deferred<Long>,
112100
accounts: List<Account>,
113-
onSendActionEvent: (SendActionEvent) -> Unit,
114-
sendActions: List<SendAction>,
101+
forwardRules: List<ForwardRule>,
115102
logs: List<LogData>
116103
) {
117104
val settings = RemotrixSettings(LocalContext.current)
@@ -137,24 +124,28 @@ fun RemotrixApp(
137124
composable(route = Destination.AccountList.route) {
138125
AccountList(
139126
accounts = accounts,
140-
onAccountEvent = onAccountEvent,
141127
onClickGoBack = { navController.popBackStack() },
142128
onClickNewAccount = { navController.navigate(Destination.NewAccount.route) },
143129
)
144130
}
145131
composable(route = Destination.NewAccount.route) {
146132
NewAccount(
147-
onClickGoBack = { navController.popBackStack() },
148-
onAccountEvent = onAccountEvent,
149-
onAccountEventAsync = onAccountEventAsync
133+
onClickGoBack = { navController.popBackStack() }
150134
)
151135
}
152-
composable(route = Destination.Setup.route) {
153-
SetupScreen(
154-
done = { navController.navigate(Destination.Home.route) },
155-
goBack = { navController.popBackStack() },
156-
openedBefore = openedBefore.value!!
157-
)
136+
navigation(route = Destination.Setup.route, startDestination = Setup.Welcome.route) {
137+
composable(Setup.Welcome.route){
138+
Welcome { navController.navigate(Setup.Manager.route) }
139+
}
140+
composable(Setup.Manager.route){
141+
SetManagerAccount { navController.navigate(Setup.ManagerSpace.route) }
142+
}
143+
composable(Setup.ManagerSpace.route){
144+
SetManagementSpace { navController.navigate(Setup.NextStep.route) }
145+
}
146+
composable(Setup.NextStep.route){
147+
AdditionalInfo { navController.navigate(Destination.Home.route) }
148+
}
158149
}
159150
composable(route = Destination.Settings.route) {
160151
Settings(
@@ -169,7 +160,7 @@ fun RemotrixApp(
169160
accounts = accounts,
170161
logs = logs,
171162
isEnabled = logging.value!!,
172-
goBack = { navController.popBackStack() },
163+
goBack = { navController.popBackStack() }
173164
)
174165
}
175166
}
@@ -182,7 +173,7 @@ fun HomeScreen(
182173
accounts: List<Account> = listOf(),
183174
navigate: (String) -> Unit = {},
184175
defaultSend: Int = -1,
185-
sendActions: List<SendAction> = listOf()
176+
forwardRules: List<ForwardRule> = listOf()
186177
) {
187178
Scaffold(
188179
topBar = {
@@ -215,7 +206,7 @@ fun HomeScreen(
215206
},
216207
modifier = Modifier.clickable { navigate(Destination.AccountList.route) }
217208
)
218-
val desc = stringResource(R.string.settings_desc) + if (sendActions.isEmpty() && defaultSend == -1) stringResource(R.string.settings_desc_warning) else ""
209+
val desc = stringResource(R.string.settings_desc) + if (forwardRules.isEmpty() && defaultSend == -1) stringResource(R.string.settings_desc_warning) else ""
219210
ListItem(
220211
headlineText = { Text(stringResource(R.string.settings)) },
221212
supportingText = { Text(desc) },

app/src/main/java/ch/skew/remotrix/NewAccount.kt

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,10 @@ import androidx.compose.ui.tooling.preview.Preview
2828
import androidx.compose.ui.unit.dp
2929
import ch.skew.remotrix.components.LabelledRadioButton
3030
import ch.skew.remotrix.components.PasswordField
31+
import ch.skew.remotrix.data.RemotrixDB
3132
import ch.skew.remotrix.data.RemotrixSettings
32-
import ch.skew.remotrix.data.accountDB.AccountEvent
33-
import ch.skew.remotrix.data.accountDB.AccountEventAsync
3433
import io.ktor.http.Url
3534
import kotlinx.coroutines.CoroutineScope
36-
import kotlinx.coroutines.Deferred
37-
import kotlinx.coroutines.async
3835
import kotlinx.coroutines.launch
3936
import net.folivo.trixnity.client.MatrixClient
4037
import net.folivo.trixnity.client.login
@@ -95,15 +92,12 @@ enum class VerificationStep {
9592
@Composable
9693
@Preview
9794
fun NewAccountPreview(){
98-
val scope = rememberCoroutineScope()
99-
NewAccount({}, {}, {scope.async {return@async 1}})
95+
NewAccount {}
10096
}
10197
@OptIn(ExperimentalMaterial3Api::class)
10298
@Composable
10399
fun NewAccount(
104-
onClickGoBack: () -> Unit,
105-
onAccountEvent: (AccountEvent) -> Unit,
106-
onAccountEventAsync: (AccountEventAsync) -> Deferred<Long>
100+
onClickGoBack: () -> Unit
107101
){
108102
val context = LocalContext.current
109103
val scope = rememberCoroutineScope()
@@ -162,7 +156,7 @@ fun NewAccount(
162156
singleLine = true,
163157
enabled = enabled.value
164158
)
165-
Column() {
159+
Column {
166160
LabelledRadioButton(label = stringResource(R.string.auto), selected = autoMsgSpace.value) {
167161
autoMsgSpace.value = true
168162
}
@@ -194,12 +188,10 @@ fun NewAccount(
194188
managerId.value,
195189
managementSpace.value,
196190
if (autoMsgSpace.value) null else messageSpaceId.value,
197-
onAccountEventAsync,
198191
{
199192
errorMsg.value = it
200193
enabled.value = true
201194
},
202-
onAccountEvent,
203195
onClickGoBack,
204196
{ step.value = it }
205197
)
@@ -228,9 +220,7 @@ fun onLoginClick(
228220
managerId: String,
229221
managementSpaceId: String?,
230222
messagingSpaceInput: String?,
231-
addAccount: (AccountEventAsync) -> Deferred<Long>,
232223
abort: (String) -> Unit,
233-
onAccountEvent: (AccountEvent) -> Unit,
234224
onClickGoBack: () -> Unit,
235225
update: (VerificationStep) -> Unit
236226
) {
@@ -241,8 +231,8 @@ fun onLoginClick(
241231
Regex("@([a-z0-9_.-]+):").find(username.lowercase())?.value ?: username.lowercase()
242232
scope.launch {
243233
update(VerificationStep.STARTED)
244-
245-
val id = addAccount(AccountEventAsync.AddAccount(localPart, baseUrl)).await()
234+
val accountDao = RemotrixDB.getInstance(context).accountDao
235+
val id = accountDao.insert(localPart, baseUrl)
246236
val clientDir = context.filesDir.resolve("clients/${id}")
247237
clientDir.mkdirs()
248238
val repo = createRealmRepositoriesModule {
@@ -287,6 +277,7 @@ fun onLoginClick(
287277
} else {
288278
update(VerificationStep.JOINING_MESSAGING_SPACE)
289279
msgSpace = RoomId(messagingSpaceInput)
280+
client.api.rooms.joinRoom(msgSpace)
290281
val testRoom = client.api.rooms.createRoom(
291282
visibility = DirectoryVisibility.PRIVATE,
292283
name = "Test room",
@@ -376,7 +367,7 @@ fun onLoginClick(
376367
context.getString(R.string.logged_in).format(client.userId),
377368
Toast.LENGTH_SHORT
378369
).show()
379-
onAccountEvent(AccountEvent.ActivateAccount(id, client.userId.domain, roomId.full, msgSpace.full))
370+
accountDao.activateAccount(id, client.userId.domain, roomId.full, msgSpace.full)
380371
onClickGoBack()
381372
}
382373
}

app/src/main/java/ch/skew/remotrix/Settings.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package ch.skew.remotrix
22

3+
import android.widget.Toast
34
import androidx.compose.foundation.clickable
45
import androidx.compose.foundation.layout.Column
56
import androidx.compose.foundation.layout.padding
67
import androidx.compose.material.icons.Icons
78
import androidx.compose.material.icons.filled.AccountCircle
89
import androidx.compose.material.icons.filled.ArrowBack
10+
import androidx.compose.material.icons.filled.DeleteForever
911
import androidx.compose.material.icons.filled.Storage
1012
import androidx.compose.material3.ExperimentalMaterial3Api
1113
import androidx.compose.material3.Icon
@@ -25,6 +27,7 @@ import androidx.compose.ui.res.stringResource
2527
import androidx.compose.ui.tooling.preview.Preview
2628
import ch.skew.remotrix.classes.Account
2729
import ch.skew.remotrix.components.SelectAccountDialog
30+
import ch.skew.remotrix.data.RemotrixDB
2831
import ch.skew.remotrix.data.RemotrixSettings
2932
import kotlinx.coroutines.launch
3033

@@ -83,6 +86,20 @@ fun Settings(
8386
Switch(checked = logging, onCheckedChange = null)
8487
}
8588
)
89+
ListItem(
90+
headlineText = { Text(stringResource(R.string.delete_log)) },
91+
supportingText = { Text(stringResource(R.string.delete_log_desc)) },
92+
leadingContent = {
93+
Icon(
94+
Icons.Filled.DeleteForever,
95+
contentDescription = stringResource(R.string.delete_log)
96+
)
97+
},
98+
modifier = Modifier.clickable {
99+
Toast.makeText(context, context.getString(R.string.log_deleted), Toast.LENGTH_SHORT).show()
100+
scope.launch { RemotrixDB.getInstance(context).logDao.deleteAll() }
101+
}
102+
)
86103
}
87104
}
88105
SelectAccountDialog(

0 commit comments

Comments
 (0)