Skip to content

Commit

Permalink
Add about page (#260)
Browse files Browse the repository at this point in the history
* Adding light and dark theme options. Fixes #254

* Adding an about page. Fixes #250
  • Loading branch information
dessalines authored Dec 20, 2022
1 parent 3b4e635 commit 5c9682a
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 9 deletions.
11 changes: 11 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.jerboa.ui.components.comment.edit.CommentEditActivity
import com.jerboa.ui.components.comment.edit.CommentEditViewModel
import com.jerboa.ui.components.comment.reply.CommentReplyActivity
import com.jerboa.ui.components.comment.reply.CommentReplyViewModel
import com.jerboa.ui.components.common.ShowChangelog
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.common.getCurrentAccountSync
import com.jerboa.ui.components.community.CommunityActivity
Expand All @@ -56,6 +57,7 @@ import com.jerboa.ui.components.report.CreateReportViewModel
import com.jerboa.ui.components.report.comment.CreateCommentReportActivity
import com.jerboa.ui.components.report.post.CreatePostReportActivity
import com.jerboa.ui.components.settings.SettingsActivity
import com.jerboa.ui.components.settings.about.AboutActivity
import com.jerboa.ui.components.settings.account.AccountSettingsActivity
import com.jerboa.ui.components.settings.account.AccountSettingsViewModel
import com.jerboa.ui.components.settings.lookandfeel.LookAndFeelActivity
Expand Down Expand Up @@ -106,6 +108,8 @@ class MainActivity : ComponentActivity() {
val navController = rememberNavController()
val ctx = LocalContext.current

ShowChangelog(appSettingsViewModel = appSettingsViewModel)

NavHost(
navController = navController,
startDestination = "splashScreen"
Expand Down Expand Up @@ -528,6 +532,13 @@ class MainActivity : ComponentActivity() {
accountSettingsViewModel = accountSettingsViewModel
)
}
composable(
route = "about"
) {
AboutActivity(
navController = navController
)
}
}
}
}
Expand Down
43 changes: 36 additions & 7 deletions app/src/main/java/com/jerboa/db/AppDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ data class AppSettings(
name = "dark_theme",
defaultValue = "0"
)
val darkTheme: Int
val darkTheme: Int,
@ColumnInfo(
name = "viewed_changelog",
defaultValue = "0"
)
val viewedChangelog: Int
)

@Dao
Expand Down Expand Up @@ -88,6 +93,9 @@ interface AppSettingsDao {

@Update
suspend fun updateAppSettings(appSettings: AppSettings)

@Query("UPDATE AppSettings set viewed_changelog = 1")
suspend fun markChangelogViewed()
}

// Declares the DAO as a private property in the constructor. Pass in the DAO
Expand Down Expand Up @@ -138,6 +146,11 @@ class AppSettingsRepository(private val appSettingsDao: AppSettingsDao) {
suspend fun update(appSettings: AppSettings) {
appSettingsDao.updateAppSettings(appSettings)
}

@WorkerThread
suspend fun markChangelogViewed() {
appSettingsDao.markChangelogViewed()
}
}

val MIGRATION_1_2 = object : Migration(1, 2) {
Expand Down Expand Up @@ -169,8 +182,19 @@ val MIGRATION_2_3 = object : Migration(2, 3) {
}
}

val MIGRATION_3_4 = object : Migration(3, 4) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
"""
alter table AppSettings add column viewed_changelog INTEGER NOT NULL
default 0
"""
)
}
}

@Database(
version = 3,
version = 4,
entities = [Account::class, AppSettings::class],
exportSchema = true
)
Expand All @@ -194,7 +218,7 @@ abstract class AppDB : RoomDatabase() {
"jerboa"
)
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2, MIGRATION_2_3)
.addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
// Necessary because it can't insert data on creation
.addCallback(object : Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
Expand All @@ -205,10 +229,11 @@ abstract class AppDB : RoomDatabase() {
CONFLICT_IGNORE, // Ensures it won't overwrite the existing data
ContentValues(2).apply {
put("id", 1)
put("font_size", DEFAULT_FONT_SIZE)
put("theme", 0)
put("light_theme", 0)
put("dark_theme", 0)
// put("font_size", DEFAULT_FONT_SIZE)
// put("theme", 0)
// put("light_theme", 0)
// put("dark_theme", 0)
// put("")
}
)
}
Expand Down Expand Up @@ -263,6 +288,10 @@ class AppSettingsViewModel(private val repository: AppSettingsRepository) : View
fun update(appSettings: AppSettings) = viewModelScope.launch {
repository.update(appSettings)
}

fun markChangelogViewed() = viewModelScope.launch {
repository.markChangelogViewed()
}
}

class AppSettingsViewModelFactory(private val repository: AppSettingsRepository) :
Expand Down
90 changes: 89 additions & 1 deletion app/src/main/java/com/jerboa/ui/components/common/Dialogs.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
package com.jerboa.ui.components.common

import android.util.Log
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material.icons.filled.BarChart
import androidx.compose.material.icons.filled.Bookmarks
import androidx.compose.material.icons.filled.BrightnessLow
import androidx.compose.material.icons.filled.FormatListNumbered
import androidx.compose.material.icons.filled.List
import androidx.compose.material.icons.filled.LocalFireDepartment
import androidx.compose.material.icons.filled.LocationCity
import androidx.compose.material.icons.filled.MarkunreadMailbox
import androidx.compose.material.icons.filled.Moving
import androidx.compose.material.icons.filled.NewReleases
import androidx.compose.material.icons.filled.Public
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.jerboa.UnreadOrAll
import com.jerboa.datatypes.ListingType
import com.jerboa.datatypes.SortType
import com.jerboa.db.AppSettingsViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request

val topSortTypes = listOf(
SortType.TopDay,
Expand Down Expand Up @@ -197,3 +229,59 @@ fun ListingTypeOptionsDialogPreview() {
onDismissRequest = {}
)
}

@Composable
fun ShowChangelog(appSettingsViewModel: AppSettingsViewModel) {
val changelogViewed = appSettingsViewModel.appSettings.observeAsState().value?.viewedChangelog

// Make sure its initialized
changelogViewed?.also { cViewed ->
val viewed = cViewed == 1

val whatsChangedDialogOpen = remember { mutableStateOf(!viewed) }

if (whatsChangedDialogOpen.value) {
val scrollState = rememberScrollState()
val scope = rememberCoroutineScope()
val markdown = remember { mutableStateOf("") }

AlertDialog(
text = {
Column(modifier = Modifier.fillMaxSize().verticalScroll(scrollState)) {
MyMarkdownText(markdown = markdown.value)
}
},
buttons = {
Row(
modifier = Modifier.padding(8.dp),
horizontalArrangement = Arrangement.Center
) {
Button(
onClick = {
whatsChangedDialogOpen.value = false
appSettingsViewModel.markChangelogViewed()
},
modifier = Modifier.fillMaxWidth()
) {
Text("Done")
}
}
},
onDismissRequest = {
whatsChangedDialogOpen.value = false
appSettingsViewModel.markChangelogViewed()
}
)

scope.launch(Dispatchers.IO) {
Log.d("jerboa", "Fetching RELEASES.md ...")
// Fetch the markdown text
val client = OkHttpClient()
val releasesUrl = "https://raw.githubusercontent.com/dessalines/jerboa/main/RELEASES.md".toHttpUrl()
val req = Request.Builder().url(releasesUrl).build()
val res = client.newCall(req).execute()
markdown.value = res.body?.string() ?: ""
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.ManageAccounts
import androidx.compose.material.icons.filled.Palette
import androidx.compose.material.rememberScaffoldState
Expand Down Expand Up @@ -60,6 +61,16 @@ fun SettingsActivity(
onClick = { navController.navigate("accountSettings") }
)
}
SettingsMenuLink(
title = { Text("About") },
icon = {
Icon(
imageVector = Icons.Default.Info,
contentDescription = "TODO"
)
},
onClick = { navController.navigate("about") }
)
}
}
)
Expand Down
Loading

0 comments on commit 5c9682a

Please sign in to comment.