Skip to content

Commit

Permalink
Add api settings (#183)
Browse files Browse the repository at this point in the history
* start working on settings page

* functionality is done

Now we just need to make this pretty

* add relevant modifiers

* fix lint

* some refactoring

* add todos
  • Loading branch information
LunaticHacker authored Jul 14, 2022
1 parent 89fca00 commit ee9bbaf
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import com.jerboa.ui.components.private_message.PrivateMessageReplyActivity
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.SettingsViewModel
import com.jerboa.ui.theme.JerboaTheme

class JerboaApplication : Application() {
Expand All @@ -69,6 +71,7 @@ class MainActivity : ComponentActivity() {
private val commentEditViewModel by viewModels<CommentEditViewModel>()
private val postEditViewModel by viewModels<PostEditViewModel>()
private val createReportViewModel by viewModels<CreateReportViewModel>()
private val settingsViewModel by viewModels<SettingsViewModel>()

private val accountViewModel: AccountViewModel by viewModels {
AccountViewModelFactory((application as JerboaApplication).repository)
Expand Down Expand Up @@ -318,6 +321,16 @@ class MainActivity : ComponentActivity() {
navController = navController,
)
}
composable(
route = "settings",
) {
SettingsActivity(
navController = navController,
accountViewModel = accountViewModel,
siteViewModel = siteViewModel,
settingsViewModel = settingsViewModel
)
}
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/com/jerboa/api/Http.kt
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ interface API {
@POST("community/block")
suspend fun blockCommunity(@Body form: BlockCommunity): Response<BlockCommunityResponse>

/**
* Save your user settings.
*/
@PUT("user/save_user_settings")
suspend fun saveUserSettings(@Body form: SaveUserSettings): Response<LoginResponse>
/**
* Upload an image.
*/
Expand Down Expand Up @@ -725,6 +730,22 @@ suspend fun blockCommunityWrapper(
return blockCommunityRes
}

suspend fun saveUserSettingsWrapper(
form: SaveUserSettings,
ctx: Context,
): LoginResponse? {

var saveUserSettingsResponse: LoginResponse? = null
val api = API.getInstance()

try {
saveUserSettingsResponse = retrofitErrorHandler(api.saveUserSettings(form))
} catch (e: Exception) {
toastException(ctx = ctx, error = e)
}
return saveUserSettingsResponse
}

suspend fun uploadPictrsImage(account: Account, imageIs: InputStream, ctx: Context): String? {
var imageUrl: String? = null
val api = API.getInstance()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/jerboa/datatypes/api/Person.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ data class SaveUserSettings(
val show_bot_accounts: Boolean?,
val show_read_posts: Boolean?,
val show_new_post_notifs: Boolean?,
val auth: String?,
val auth: String,
)

data class ChangePassword(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
Expand All @@ -33,6 +34,7 @@ import com.jerboa.appendMarkdownImage
import com.jerboa.db.Account
import com.jerboa.imageInputStreamFromUri
import com.jerboa.ui.theme.MEDIUM_PADDING
import com.jerboa.ui.theme.SMALL_PADDING
import com.jerboa.ui.theme.XXL_PADDING
import com.jerboa.ui.theme.muted
import dev.jeziellago.compose.markdowntext.MarkdownText
Expand Down Expand Up @@ -513,6 +515,78 @@ fun MarkdownHelperBar(
}
}

@Composable
fun MyCheckBox(
checked: Boolean,
enabled: Boolean = true,
label: String,
onCheckedChange: (Boolean) -> Unit
) {
Row(
modifier = Modifier.padding(SMALL_PADDING),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = label)
Checkbox(
checked = checked,
onCheckedChange = onCheckedChange,
enabled = enabled,
)
}
}

// https://stackoverflow.com/a/67111599
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MyDropDown(
suggestions: List<String>,
onValueChange: (Int) -> Unit,
initialValue: Int,
label: String
) {
var expanded by remember { mutableStateOf(false) }
var selectedText by remember { mutableStateOf(suggestions[initialValue]) }

ExposedDropdownMenuBox(
expanded = expanded,
onExpandedChange = {
expanded = !expanded
}
) {
TextField(
readOnly = true,
value = selectedText,
modifier = Modifier.fillMaxWidth(),
onValueChange = { },
label = { Text(label) },
trailingIcon = {
ExposedDropdownMenuDefaults.TrailingIcon(
expanded = expanded
)
},
colors = ExposedDropdownMenuDefaults.textFieldColors()
)
ExposedDropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
}
) {
suggestions.forEach { selectionOption ->
DropdownMenuItem(
onClick = {
selectedText = selectionOption
expanded = false
onValueChange(suggestions.indexOf(selectedText))
}
) {
Text(text = selectionOption)
}
}
}
}
}

@Preview
@Composable
fun ReplyMarkdownBarPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ fun PickImage(
onPickedImage: (image: Uri) -> Unit,
image: Uri? = null,
showImage: Boolean = true,
horizontalAlignment: Alignment.Horizontal = Alignment.Start
) {
val ctx = LocalContext.current
var imageUri by remember {
Expand Down Expand Up @@ -168,8 +169,8 @@ fun PickImage(
onPickedImage(uri!!)
}
Column(
modifier = modifier.fillMaxWidth(),
horizontalAlignment = Alignment.End
modifier = modifier,
horizontalAlignment = horizontalAlignment
) {
OutlinedButton(onClick = {
launcher.launch("image/*")
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/jerboa/ui/components/home/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fun Drawer(
onClickProfile: () -> Unit,
onClickInbox: () -> Unit,
onClickSaved: () -> Unit,
onClickSettings: () -> Unit,
unreadCounts: GetUnreadCountResponse?,
) {
var showAccountAddMode by rememberSaveable { mutableStateOf(false) }
Expand All @@ -71,6 +72,7 @@ fun Drawer(
onClickProfile = onClickProfile,
onClickInbox = onClickInbox,
onClickSaved = onClickSaved,
onClickSettings = onClickSettings
)
}

Expand All @@ -86,6 +88,7 @@ fun DrawerContent(
onClickProfile: () -> Unit,
onClickInbox: () -> Unit,
onClickSaved: () -> Unit,
onClickSettings: () -> Unit,
follows: List<CommunityFollowerView>?,
unreadCounts: GetUnreadCountResponse?,
) {
Expand All @@ -111,6 +114,7 @@ fun DrawerContent(
onClickSaved = onClickSaved,
follows = follows,
unreadCounts = unreadCounts,
onClickSettings = onClickSettings
)
}
}
Expand All @@ -121,6 +125,7 @@ fun DrawerItemsMain(
onClickSaved: () -> Unit,
onClickProfile: () -> Unit,
onClickInbox: () -> Unit,
onClickSettings: () -> Unit,
onClickListingType: (ListingType) -> Unit,
onCommunityClick: (community: CommunitySafe) -> Unit,
unreadCounts: GetUnreadCountResponse? = null,
Expand Down Expand Up @@ -181,6 +186,13 @@ fun DrawerItemsMain(
iconBadgeCount = totalUnreads,
)
}
item {
IconAndTextDrawerItem(
text = "Settings",
icon = Icons.Default.Settings,
onClick = onClickSettings,
)
}
item {
Divider()
}
Expand Down Expand Up @@ -213,6 +225,7 @@ fun DrawerItemsMainPreview() {
onClickInbox = {},
onCommunityClick = {},
onClickSaved = {},
onClickSettings = {}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.jerboa.ui.components.post.edit.postEditClickWrapper
import com.jerboa.ui.components.post.postClickWrapper
import com.jerboa.ui.components.report.CreateReportViewModel
import com.jerboa.ui.components.report.postReportClickWrapper
import com.jerboa.ui.components.settings.settingsClickWrapper
import kotlinx.coroutines.CoroutineScope

@Composable
Expand Down Expand Up @@ -404,6 +405,13 @@ fun MainDrawer(
)
closeDrawer(scope, scaffoldState)
},
onClickSettings = {
settingsClickWrapper(
navController = navController,
account = account
)
closeDrawer(scope, scaffoldState)
},
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.ArrowDropDown
import androidx.compose.material.icons.filled.Close
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
Expand Down Expand Up @@ -152,7 +153,9 @@ fun CreatePostBody(
item {
PickImage(
onPickedImage = onPickedImage,
image = image
image = image,
modifier = Modifier.fillMaxWidth(),
horizontalAlignment = Alignment.End
)
}
// TODO change this to reply text field at some point
Expand Down
Loading

0 comments on commit ee9bbaf

Please sign in to comment.