Skip to content

Commit

Permalink
Added saving and loading server entries [#5]
Browse files Browse the repository at this point in the history
 * Added a shared view model.
 * Changed Server to be a shared model.
 * Added dependency injection using Koin.
 * Added the ServersDb table.
 * Added screens to edit servers.
  • Loading branch information
mcpierce committed Jun 23, 2024
1 parent a37d7a9 commit 9acce4d
Show file tree
Hide file tree
Showing 40 changed files with 1,355 additions and 160 deletions.
2 changes: 2 additions & 0 deletions androidVariant/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ dependencies {
implementation(composeBom)
implementation(libs.bundles.androidx)
implementation(libs.bundles.compose)
implementation(libs.koin.android)
implementation(libs.koin.androidx.compose)

testImplementation(libs.bundles.unit.tests)
androidTestImplementation(composeBom)
Expand Down
2 changes: 2 additions & 0 deletions androidVariant/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".VariantApp"
android:allowBackup="false"
android:label="Variant"
android:supportsRtl="true"
android:usesCleartextTraffic="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import org.comixedproject.variant.android.ui.HomeScreen
import org.comixedproject.variant.android.ui.server.HomeScreen

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Variant - A digital comic book reading application for the iPad and Android tablets.
* Copyright (C) 2024, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixedproject.variant.android

import android.app.Application
import android.content.Context
import org.comixedproject.variant.shared.initKoin
import org.comixedproject.variant.shared.model.VariantViewModel
import org.koin.androidx.viewmodel.dsl.viewModel
import org.koin.dsl.module


class VariantApp : Application() {
override fun onCreate() {
super.onCreate()

initKoin(
appModule = module {
single<Context> { this@VariantApp }

},
viewModelsModule = module {
viewModel {
VariantViewModel(get())
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
package org.comixedproject.variant.android.ui

import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountBox
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.filled.Settings
import androidx.compose.ui.graphics.vector.ImageVector
import org.comixedproject.variant.android.R

enum class Screens(val label: Int, val icon: ImageVector) {
ServerManagement(R.string.serverButtonLabel, Icons.Filled.List),
ComicManagement(R.string.comicsButtonLabel, Icons.Filled.PlayArrow),
ComicManagement(R.string.comicsButtonLabel, Icons.Filled.List),
ServerManagement(R.string.serverButtonLabel, Icons.Filled.AccountBox),
Settings(R.string.settingsButtonLabel, Icons.Filled.Settings);

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Variant - A digital comic book reading application for the iPad and Android tablets.
* Copyright (C) 2024, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixedproject.variant.android.ui

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.SwipeToDismissBoxState
import androidx.compose.material3.SwipeToDismissBoxValue
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.comixedproject.variant.android.R
import org.comixedproject.variant.android.VariantTheme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ServerListEntryDismissBackground(dismissState: SwipeToDismissBoxState) {
val color = when (dismissState.dismissDirection) {
SwipeToDismissBoxValue.StartToEnd -> Color(0xFFFF1744)
SwipeToDismissBoxValue.EndToStart -> Color(0xFF1DE986)
SwipeToDismissBoxValue.Settled -> Color.Transparent
}

Row(
modifier = Modifier
.fillMaxSize()
.background(color)
.padding(12.dp, 8.dp),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
Icon(Icons.Default.Delete, contentDescription = stringResource(id = R.string.deleteServer))
Spacer(modifier = Modifier)
Icon(Icons.Default.Edit, contentDescription = stringResource(id = R.string.editServer))
}
}

@Preview
@Composable
fun ServerListEntryDismissBackgroundPreview() {
VariantTheme {
ServerListEntryDismissBackground(rememberSwipeToDismissBoxState())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixedproject.variant.android.ui
package org.comixedproject.variant.android.ui.server

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
Expand All @@ -35,11 +35,13 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import org.comixedproject.variant.android.VariantTheme
import org.comixedproject.variant.android.ui.server.ServerManagementScreen
import org.comixedproject.variant.android.ui.Screens
import org.comixedproject.variant.shared.model.VariantViewModel
import org.koin.androidx.compose.getViewModel

@Composable
fun HomeScreen() {
val selectedItem = remember { mutableStateOf(Screens.ServerManagement) }
fun HomeScreen(viewModel: VariantViewModel = getViewModel()) {
val selectedItem = remember { mutableStateOf(Screens.ComicManagement) }
val navHost = rememberNavController()
val navBackStackEntry by navHost.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
Expand All @@ -65,7 +67,16 @@ fun HomeScreen() {
Box(modifier = Modifier.fillMaxSize()) {
Surface(modifier = Modifier.align(Alignment.Center)) {
when (selectedItem.value) {
Screens.ServerManagement -> ServerManagementScreen()
Screens.ServerManagement -> ServerManagementScreen(
viewModel.servers,
onSaveServer = { server ->
viewModel.saveServer(server)
},
onBrowserServer = {},
onDeleteServer = { server ->
viewModel.deleteServer(server)
})

Screens.ComicManagement -> Text("Comic Book Management")
Screens.Settings -> Text("Settings")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Variant - A digital comic book reading application for the iPad and Android tablets.
* Copyright (C) 2024, The ComiXed Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses>
*/

package org.comixedproject.variant.android.ui.server

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import org.comixedproject.variant.android.R
import org.comixedproject.variant.android.VariantTheme
import org.comixedproject.variant.shared.model.server.Server

@Composable
fun ServerDetail(
server: Server,
onEdit: () -> Unit,
onDelete: () -> Unit,
onBrowse: () -> Unit
) {
Scaffold(bottomBar = {
BottomAppBar(
containerColor = MaterialTheme.colorScheme.primaryContainer,
contentColor = MaterialTheme.colorScheme.primary
) {
Button(onClick = onEdit) {
Icon(
imageVector = Icons.Filled.Edit,
contentDescription = stringResource(id = R.string.editButtonLabel)
)
}
Button(onClick = onBrowse) {
Icon(
imageVector = Icons.Filled.Search,
contentDescription = stringResource(id = R.string.browseButtonLabel)
)
}
Button(onClick = onDelete) {
Icon(
imageVector = Icons.Filled.Delete,
contentDescription = stringResource(id = R.string.deleteButtonLabel)
)
}
}
}) { padding ->
Box(modifier = Modifier.padding(padding)) {
Column(modifier = Modifier.padding(32.dp)) {
Text(text = server.name, style = MaterialTheme.typography.titleLarge)
Text(text = server.url, style = MaterialTheme.typography.bodyMedium)
Text(text = server.username, style = MaterialTheme.typography.bodyMedium)
Text(
text = server.password.replace(".".toRegex(), "*"),
style = MaterialTheme.typography.bodyMedium
)
}
}
}
}

@Preview
@Composable
fun ServerDetailPreview() {
VariantTheme {
ServerDetail(
server = Server(
"1",
"My Server",
"http://www.comixedproject.org:7171/opds",
"[email protected]",
"my!password"
), onEdit = {}, onDelete = {}, onBrowse = {})
}
}
Loading

0 comments on commit 9acce4d

Please sign in to comment.