Skip to content

Commit

Permalink
Added background processing in Android, implemented option to log out
Browse files Browse the repository at this point in the history
  • Loading branch information
aeoliux committed Nov 30, 2024
1 parent 7e52d04 commit 0d8134e
Show file tree
Hide file tree
Showing 27 changed files with 234 additions and 77 deletions.
27 changes: 25 additions & 2 deletions build_ios.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
#!/bin/sh

CONFIG=Debug
TARGET=iphoneos
CERTIFICATE=""

while getopts "c:r" opt; do
case $opt in
c)
CERTIFICATE="${OPTARG}"
;;
r)
CONFIG=Release
;;
t)
TARGET="${OPTARG}"
;;
esac
done

oldPwd=$(pwd)

cd $oldPwd/iosApp
xcodebuild -project iosApp.xcodeproj -scheme iosApp -sdk iphoneos -configuration Release CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO SYMROOT=$PWD/Build
cd Build/Release-iphoneos
xcodebuild -project iosApp.xcodeproj -scheme iosApp -sdk "${TARGET}" -configuration "${CONFIG}" CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO SYMROOT=$PWD/Build
cd Build/${CONFIG}-${TARGET}
mkdir -p Payload
cp -pr Violet.app Payload

if [ -n "${CERTIFICATE}" ]; then
codesign --force --sign "${CERTIFICATE}" Payload/Violet.app/Violet
fi

zip -r $oldPwd/Violet.ipa Payload
cd $oldPwd
rm -rf $oldPwd/iosApp/Build
3 changes: 3 additions & 0 deletions composeApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<application
android:allowBackup="true"
Expand All @@ -21,5 +22,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".app.appState.BackgroundJob" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ actual class Keychain(private val context: Context) {
println(final)
return final
}

actual fun deletePass() {
sharedPreferences.edit().remove("synergiaCredentials").apply()
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.github.aeoliux.violet

import android.graphics.Color
import android.content.Intent
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import com.github.aeoliux.violet.app.appState.BackgroundJob
import com.github.aeoliux.violet.app.appState.runBackgroundTask
import com.github.aeoliux.violet.app.appState.taskToBePerformed
import com.github.aeoliux.violet.app.storage.Database
import com.github.aeoliux.violet.app.storage.DatabaseDriverFactory

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

runBackgroundTask = { task ->
taskToBePerformed = task
startService(Intent(this, BackgroundJob::class.java))
}

Database.open(DatabaseDriverFactory(context = this).createDriver())
enableEdgeToEdge()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.aeoliux.violet.app.appState

import android.app.Service
import android.content.Intent
import android.os.IBinder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

var taskToBePerformed: suspend () -> Unit = {}

class BackgroundJob: Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
CoroutineScope(Dispatchers.Default).launch {
taskToBePerformed()
}

return START_STICKY
}

override fun onBind(intent: Intent?): IBinder? = null
}

actual var runBackgroundTask: (task: suspend () -> Unit) -> Unit = {}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.github.aeoliux.violet
expect class Keychain {
fun savePass(password: String)
fun getPass(): String?
fun deletePass()
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.github.aeoliux.violet.app.agenda

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import com.github.aeoliux.violet.api.types.AgendaItem
import com.github.aeoliux.violet.app.components.Dialog
Expand All @@ -16,23 +12,13 @@ import com.github.aeoliux.violet.app.components.Dialog
fun AgendaDialog(agendaItem: AgendaItem, onDismiss: () -> Unit) {
Dialog({ onDismiss() }) {
Column(Modifier.wrapContentSize()) {
Text("Category: ")
if (agendaItem.subject != null) Text("Subject: ")
if (agendaItem.classroom != null) Text("Classroom: ")
Text("Added by: ")
Text("Added at: ")
Text("Starts at: ")

Text("Content: ")
}
Column(Modifier.wrapContentHeight()) {
Text(agendaItem.category)
if (agendaItem.subject != null) Text(agendaItem.subject)
if (agendaItem.classroom != null) Text(agendaItem.classroom)
Text(agendaItem.createdBy)
Text(agendaItem.addedAt.toString())
Text(agendaItem.timeFrom)
Text(agendaItem.content)
Text("Category: ${agendaItem.category}")
agendaItem.subject?.let { Text("Subject: $it") }
agendaItem.classroom?.let { Text("Classroom: $it") }
Text("Added by: ${agendaItem.createdBy}")
Text("Added at: ${agendaItem.addedAt}")
Text("Starts at: ${agendaItem.timeFrom}")
Text("Content: ${agendaItem.content}")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ fun AgendaView(vm: AgendaViewModel = viewModel { AgendaViewModel() }) {
}

if (isLoaded) {
Header("Agenda")

var dateIndex = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()).date.toEpochDays()
val startDate = dateIndex

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.github.aeoliux.violet.app.appState

expect var runBackgroundTask: (task: suspend () -> Unit) -> Unit
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ suspend fun AppState.logIn(login: String? = null, password: String? = null) {

client.value.proceedLogin(login, password)
val data = "$login $password"
println(data)
keychain.savePass(data)
isLoggedIn.value = true
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ fun AttendanceView(vm: AttendanceViewModel = viewModel { AttendanceViewModel() }
}

if (isLoaded) {
Header("Attendance")

TabRow(
selectedTabIndex = selectedView,
contentColor = Color.Black,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.github.aeoliux.violet.app.grades

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -12,21 +15,15 @@ import com.github.aeoliux.violet.app.components.Dialog
@Composable
fun GradeDialog(grade: Grade, onDismiss: () -> Unit) {
Dialog({ onDismiss() }) {
Column(Modifier.wrapContentWidth().wrapContentHeight()) {
Text("Grade: ")
Text("Added by: ")
Text("Category: ")
Text("Weight: ")
Text("Date: ")
if (grade.comment != null) Text("Comment: ")
}
Column(Modifier.wrapContentHeight()) {
Text(grade.grade)
Text(grade.addedBy)
Text(grade.category)
Text(grade.weight.toString())
Text(grade.addDate.toString())
if (grade.comment != null) Text(grade.comment)
Column(Modifier.wrapContentSize()) {
Text("Grade: ${grade.grade}")
Text("Added by: ${grade.addedBy}")
Text("Category: ${grade.category}")
Text("Weight: ${grade.weight}")
Text("Date: ${grade.addDate}")
grade.comment?.let {
Text("Comment: $it")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ fun GradesView(vm: GradesViewModel = viewModel { GradesViewModel() }) {
}

if (isLoaded) {
Header("Grades")

grades.forEach { (subject, grades) ->
if (grades.isEmpty())
return@forEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class LoginViewModel(
appState.fetchData(login.value, password.value)

_showLoadingIndicator.update { false }

_login.update { "" }
_password.update { "" }
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.aeoliux.violet.app.main

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand All @@ -13,37 +12,33 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.BottomAppBar
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.github.aeoliux.violet.app.appState.LocalAppState
import com.github.aeoliux.violet.app.components.Header
import com.github.aeoliux.violet.app.login.LoginView
import com.github.aeoliux.violet.app.settings.SettingsView
import kotlinx.coroutines.launch

@Composable
Expand All @@ -52,6 +47,7 @@ fun MainView() {
val vm = viewModel { MainViewModel(appState) }

val selectedView by vm.selectedView.collectAsState()
val settings by vm.settings.collectAsState()

val coroutineScope = rememberCoroutineScope()
val scrollState = rememberScrollState()
Expand Down Expand Up @@ -114,12 +110,26 @@ fun MainView() {
Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.End
) {
IconButton({ vm.refresh() }) {
Icon(
imageVector = Icons.Filled.Refresh,
contentDescription = "Sync data",
tint = MaterialTheme.colorScheme.onBackground
)
Row {
IconButton({ vm.showOrHideSettings() }) {
Icon(
imageVector = Icons.Filled.Settings,
contentDescription = "Show/hide settings"
)
}

appState.statusMessage.value?.let {
CircularProgressIndicator(
Modifier
.padding(end = 10.dp)
)
} ?: IconButton({ vm.refresh() }) {
Icon(
imageVector = Icons.Filled.Refresh,
contentDescription = "Sync data",
tint = MaterialTheme.colorScheme.onBackground
)
}
}
}
}
Expand All @@ -129,7 +139,12 @@ fun MainView() {
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally
) {
vm.tabs[selectedView].destination()
if (settings) {
SettingsView()
} else {
Header(vm.tabs[selectedView].text)
vm.tabs[selectedView].destination()
}

Spacer(Modifier.height(20.dp))
}
Expand Down
Loading

0 comments on commit 0d8134e

Please sign in to comment.