create a conditional base on switch value - material-ui

If the switch is checked I want one R.drawable if not I want another, could you help me, please?
I'm using a data base, viewmodel, room. I just don't know how to properly write the conditional
#Composable
fun PetCard(userInfo: PetEntity, mPetViewModel: PetViewModel) {
Card(
shape = MaterialTheme.shapes.large,
elevation = 5.dp,
backgroundColor= MaterialTheme.colorScheme.primary,
modifier = Modifier
.width(195.dp)
.padding(5.dp)
.fillMaxSize()
) {
Image(painter = if (userInfo.vPetsSpecies != null) {
painterResource(id = R.drawable.pet_cat)
} else {
painterResource(id = R.drawable.pet_dog)
},
contentDescription = "Pet",
contentScale = ContentScale.FillBounds,
modifier = Modifier
.fillMaxSize()
)
Text(text = userInfo.vPetName,
modifier = Modifier
.padding(10.dp),
color = MaterialTheme.colorScheme.onPrimary,
style = MaterialTheme.typography.headlineMedium,
textAlign= TextAlign.Left
)
}
}

Related

Create vertical chain with respect to other element in jetpack compose ConstraintLayout?

I want to chain title and description text centered with respect to image with chainStyle.Packed how to achieve this in jetpack compose.
when i use createVerticalChain() its create chain with respect to parent container that's not what i want, is there a way to achieve this?
Like suggested inside the documentation of createVerticalChain(), you can "Use constrain with the resulting VerticalChainReference to modify the top and bottom constraints of this chain.":
ConstraintLayout(modifier = Modifier.fillMaxSize()) {
val (box, text1, text2) = createRefs()
val chainRef = createVerticalChain(text1, text2, chainStyle = ChainStyle.Packed)
constrain(chainRef) {
top.linkTo(box.top)
bottom.linkTo(box.bottom)
}
Box(modifier = Modifier
.size(100.dp)
.padding(16.dp)
.background(color = Color.Red, shape = RoundedCornerShape(50.dp))
.constrainAs(box) {
start.linkTo(parent.start)
top.linkTo(parent.top)
}
)
Text("Line 1 goes here",
modifier = Modifier
.constrainAs(text1) {
start.linkTo(box.end)
end.linkTo(parent.end)
top.linkTo(box.top)
bottom.linkTo(text2.top)
width = Dimension.fillToConstraints
}
)
Text("Line 2 goes here",
modifier = Modifier
.constrainAs(text2) {
start.linkTo(box.end)
end.linkTo(parent.end)
top.linkTo(text1.bottom)
bottom.linkTo(box.bottom)
width = Dimension.fillToConstraints
}
)
}
There are two solutions. The first solution requires that the height of the content on the right of the circle is fixed, while in the second solution, it is not fixed but is bound by constraints:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(intent)
setContent {
Column(modifier = Modifier.fillMaxSize()) {
// First solution
Row(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight()
) {
Box(
modifier = Modifier
.size(100.dp)
.background(color = Color.Red, shape = RoundedCornerShape(50.dp))
)
Column(
modifier = Modifier
.height(100.dp)
.padding(start = 20.dp), verticalArrangement = Arrangement.Center
) {
Text("Line 1 goes here")
Text("Line 2 goes here")
}
}
Spacer(modifier = Modifier
.requiredHeight(30.dp)
.fillMaxWidth())
// Second solution
ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
val (left, right) = createRefs()
Box(modifier = Modifier
.size(100.dp)
.background(color = Color.Red, shape = RoundedCornerShape(50.dp))
.constrainAs(left) {
start.linkTo(parent.start)
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
})
Column(
verticalArrangement = Arrangement.Center,
modifier = Modifier
.padding(start = 20.dp)
.constrainAs(right) {
start.linkTo(left.end)
top.linkTo(left.top)
end.linkTo(parent.end)
bottom.linkTo(left.bottom)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
}) {
Text("Line 1 goes here")
Text("Line 2 goes here")
}
}
}
}
}
}

How to get tags around selected text in a contenteditable?

I'm trying to do an handler for applying certain formatting style (bold/cursive) at the click of respective button.
In particular, I want to handle selection of text: I want to toggle tags in a selection (tags are combinable).
I tried this way, but I think there should be better ideas. I found the nearest container to selected text, than I found all of its ancestors inside textarea.
var selection = document.getSelection();
var selectedText = selection.toString();
var range = selection.getRangeAt(0).cloneRange();
var firstTag = $("#textarea-ce *:contains("+selectedText+")");
var currNode = firstTag;
var newNode = $(document.createTextNode(selectedText))[0];
var currTagName;
var currTag;
var lastTag = false;
var found = false;
while (!lastTag) {
currTagName = $(currNode).prop('tagName').toLowerCase();
currTag = $("<"+currTagName+"><"+currTagName+"/>")[0];
if (currTagName != selectedTagName){
newNode = $(currTag).html(newNode);
}
else {
found = true;
}
if ($(currNode).parent().attr('id') == 'textarea-ce') {
lastTag = true;
}
else {
currNode = $(currNode).parent();
}
}
currNode = $(textarea).find(currNode)[0];
var newNodeContent = $(newNode).html();
if (!found) {
newNodeContent = "<"+selectedTagName+">"+newNodeContent+"<"+selectedTagName+"/>";
newNode = $(selectedTag).html($(newNode));
}
if (currTagName.startsWith("h")){
$(currNode).html(newNodeContent);
range.insertNode(currNode);
}
else {
$(currNode).replaceWith(newNode);
}
I used header (h1, h2,...) displayed as inline for sizing text.
In order to get surrounding tags I tried also with documentFragment (range.extractContents) but after a selection, sometimes it doesn't include tags, but only the text node.
The hardest challenge is to handle the case in which the user selects part of a formatted text and removes one of those tags.
Any suggestion is appreciated.

VS Code extension how to edit in context?

Her is the class I use automatically capitalize true, false, ...
export class StUpdater {
private _lines: number;
private _strings: Array<string>;
constructor() {
this._lines = 0;
this._strings = ['true', 'false', 'exit', 'continue', 'return'];
}
Update(Cntx: boolean = false) {
let editor = window.activeTextEditor;
if (!editor || (editor.document.languageId != 'st')) {
window.showErrorMessage('No editor!')
return;
}
let doc = editor.document;
if (Cntx == false) {
if (this._lines >= doc.lineCount) {
this._lines = doc.lineCount;
return;
}
this._lines = doc.lineCount;
let AutoFormat = workspace.getConfiguration('st').get('autoFormat');
if (!AutoFormat) {
return;
}
}
let edit = new WorkspaceEdit();
for (let line = 0; line < doc.lineCount; line++) {
const element = doc.lineAt(line);
for (let i = 0; i < this._strings.length; i++) {
let str = this._strings[i];
let last_char = 0;
while (element.text.indexOf(str, last_char) >= 0) {
let char = element.text.indexOf(str, last_char);
last_char = char + str.length;
edit.replace(
doc.uri,
new Range(
new Position(line, char),
new Position(line, last_char)
),
str.toUpperCase()
);
}
}
}
return workspace.applyEdit(edit);
}
public dispose() {
}
}
This code works fine, but I do not want to replace it inside the string or comment. How do I do that? I cannot find preg version of replace and even if I do, in one line I do not know if it is comment or not if it is multiple line comment.
If I understand you correctly you want capitalize only certain elements (identifiers probably), but not words in comments or strings, correct? That requires to identify lexical elements in the text, which is a mapping of a range of letters to a lexical type. This is usually done by a lexer. It's not difficult to write one by hand which walks over the characters on top of your current processing and find those ranges that can be manipulated.

Property Triggers is not working in the code-behind

I am using Xamarin.Forms in which i have used property triggers behind the code all coding is been done .Please help me out what mistake i did in the code.
Label header = new Label
{
Text = "Trigger",
Font = Font.BoldSystemFontOfSize(50),
HorizontalOptions = LayoutOptions.Center
};
Entry enter = new Entry
{
Placeholder = "Enter Name"
};
Trigger trigger = new Trigger(typeof(Entry))
{
Property = Entry.IsFocusedProperty,
Value = "True"
};
trigger.Setters.Add(new Setter
{
Property = VisualElement.BackgroundColorProperty,
Value = Color.Red
});
enter.Triggers.Add(trigger);
this.Content = new StackLayout
{
Children =
{
header,
enter
}
};
Only Trigger part is not working

Create a tag for generating #link in Play2.0

while actively learning Play2.0 I am stuck with creating a tag. In the sample application, called computer-database, the following helper is created in the list template:
#****************************************
* Helper generating navigation links *
****************************************#
#link(newPage:Int, newSortBy:String) = #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
controllers.orders.routes.Work.list(newPage, sortBy, order, currentFilter)
}
Since I want to use this helper in a view templates I thought that the best solution would be to create a tag for it. So I did the following (in my tags package):
#(newPage : Int, newSortBy:String) {
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
controllers.orders.routes.Computer.list(newPage, sortBy, order, currentFilter)
}
But, obviously this is not working and I do not know where or why it is not working.
Thanks for the input.
UPDATE WITH ANSWER:
So in Scala template we have to define, just as in Java, the arguments that are passed to this view (Note: that the variables that you will use in the javascript must be passed too!). The template will be compiled as a method as stated in the documentation.
The working tag looks like:
#(newPage : Int, newSortBy : String, currentSortBy: String, currentOrder: String, currentFilter : String ) #{
var sortBy = currentSortBy
var order = currentOrder
if(newSortBy != null) {
sortBy = newSortBy
if(currentSortBy == newSortBy) {
if(currentOrder == "asc") {
order = "desc"
} else {
order = "asc"
}
} else {
order = "asc"
}
}
// Generate the link
controllers.orders.routes.Work.list(newPage, sortBy, order, currentFilter)
}
The trick is that the first version uses a template syntax allowing to write Scala code instead of HTML: #{ val scalaVal = 42}.
In your tag, the template engine interpretes your code as HTML.
If you want to copy-paste this code, don’t forget the leading # before the opening brace.