Support
Quality
Security
License
Reuse
kandi has reviewed Dexter and discovered the below as its top functions. This is intended to give you an instant insight into Dexter implemented functionality, and help decide if they suit your requirements.
Android library that simplifies the process of requesting permissions at runtime.
default
![Demo screenshot][1]
Usage
-----
### Dependency
Include the library in your ``build.gradle``
```groovy
dependencies{
implementation 'com.karumi:dexter:6.2.2'
}
```
To start using the library you just need to call `Dexter` with a valid `Context`:
```java
public MyActivity extends Activity {
@Override public void onCreate() {
super.onCreate();
Dexter.withContext(activity)
.withPermission(permission)
.withListener(listener)
.check();
}
}
```
### Single permission
For each permission, register a ``PermissionListener`` implementation to receive the state of the request:
```java
Dexter.withContext(this)
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
@Override public void onPermissionGranted(PermissionGrantedResponse response) {/* ... */}
@Override public void onPermissionDenied(PermissionDeniedResponse response) {/* ... */}
@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {/* ... */}
}).check();
```
To make your life easier we offer some ``PermissionListener`` implementations to perform recurrent actions:
* ``BasePermissionListener`` to make it easier to implement only the methods you want. Keep in mind that you should not call `super` methods when overriding them.
* ``DialogOnDeniedPermissionListener`` to show a configurable dialog whenever the user rejects a permission request:
```java
PermissionListener dialogPermissionListener =
DialogOnDeniedPermissionListener.Builder
.withContext(context)
.withTitle("Camera permission")
.withMessage("Camera permission is needed to take pictures of your cat")
.withButtonText(android.R.string.ok)
.withIcon(R.mipmap.my_icon)
.build();
```
* ``SnackbarOnDeniedPermissionListener`` to show a snackbar message whenever the user rejects a permission request:
```java
PermissionListener snackbarPermissionListener =
SnackbarOnDeniedPermissionListener.Builder
.with(view, "Camera access is needed to take pictures of your dog")
.withOpenSettingsButton("Settings")
.withCallback(new Snackbar.Callback() {
@Override
public void onShown(Snackbar snackbar) {
// Event handler for when the given Snackbar is visible
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
// Event handler for when the given Snackbar has been dismissed
}
}).build();
```
* ``CompositePermissionListener`` to compound multiple listeners into one:
```java
PermissionListener snackbarPermissionListener = /*...*/;
PermissionListener dialogPermissionListener = /*...*/;
PermissionListener compositePermissionListener = new CompositePermissionListener(snackbarPermissionListener, dialogPermissionListener, /*...*/);
```
### Multiple permissions
If you want to request multiple permissions you just need to call `withPermissions` and register an implementation of ``MultiplePermissionsListener``:
```java
Dexter.withContext(this)
.withPermissions(
Manifest.permission.CAMERA,
Manifest.permission.READ_CONTACTS,
Manifest.permission.RECORD_AUDIO
).withListener(new MultiplePermissionsListener() {
@Override public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
@Override public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */}
}).check();
```
The ``MultiplePermissionsReport`` contains all the details of the permission request like the list of denied/granted permissions or utility methods like ``areAllPermissionsGranted`` and ``isAnyPermissionPermanentlyDenied``.
As with the single permission listener, there are also some useful implementations for recurring patterns:
* ``BaseMultiplePermissionsListener`` to make it easier to implement only the methods you want. Keep in mind that you should not call `super` methods when overriding them.
* ``DialogOnAnyDeniedMultiplePermissionsListener`` to show a configurable dialog whenever the user rejects at least one permission:
```java
MultiplePermissionsListener dialogMultiplePermissionsListener =
DialogOnAnyDeniedMultiplePermissionsListener.Builder
.withContext(context)
.withTitle("Camera & audio permission")
.withMessage("Both camera and audio permission are needed to take pictures of your cat")
.withButtonText(android.R.string.ok)
.withIcon(R.mipmap.my_icon)
.build();
```
* ``SnackbarOnAnyDeniedMultiplePermissionsListener`` to show a snackbar message whenever the user rejects any of the requested permissions:
```java
MultiplePermissionsListener snackbarMultiplePermissionsListener =
SnackbarOnAnyDeniedMultiplePermissionsListener.Builder
.with(view, "Camera and audio access is needed to take pictures of your dog")
.withOpenSettingsButton("Settings")
.withCallback(new Snackbar.Callback() {
@Override
public void onShown(Snackbar snackbar) {
// Event handler for when the given Snackbar is visible
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
// Event handler for when the given Snackbar has been dismissed
}
})
.build();
```
* ``CompositePermissionListener`` to compound multiple listeners into one:
```java
MultiplePermissionsListener snackbarMultiplePermissionsListener = /*...*/;
MultiplePermissionsListener dialogMultiplePermissionsListener = /*...*/;
MultiplePermissionsListener compositePermissionsListener = new CompositeMultiplePermissionsListener(snackbarMultiplePermissionsListener, dialogMultiplePermissionsListener, /*...*/);
```
### Handling listener threads
If you want to receive permission listener callbacks on the same thread that fired the permission request, you just need to call ``onSameThread`` before checking for permissions:
```java
Dexter.withContext(context)
.withPermission(permission)
.withListener(listener)
.onSameThread()
.check();
```
### Showing a rationale
Android will notify you when you are requesting a permission that needs an additional explanation for its usage, either because it is considered dangerous, or because the user has already declined that permission once.
Dexter will call the method ``onPermissionRationaleShouldBeShown`` implemented in your listener with a ``PermissionToken``. **It's important to keep in mind that the request process will pause until the token is used**, therefore, you won't be able to call Dexter again or request any other permissions if the token has not been used.
The most simple implementation of your ``onPermissionRationaleShouldBeShown`` method could be:
```java
@Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
token.continuePermissionRequest();
}
```
### Error handling
If you think there is an error in your Dexter integration, just register a `PermissionRequestErrorListener` when calling Dexter:
```java
Dexter.withContext(context)
.withPermission(permission)
.withListener(listener)
.withErrorListener(new PermissionRequestErrorListener() {
@Override public void onError(DexterError error) {
Log.e("Dexter", "There was an error: " + error.toString());
}
}).check();
```
The library will notify you when something bad happens. In general, it is a good practice to, at least, log every error Dexter may throw but is up to you, the developer, to do that.
**IMPORTANT**: Remember to follow the [Google design guidelines][2] to make your application as user-friendly as possible.
### Permission dialog not being shown
If you are using the ``MultiplePermissionsListener`` and you don't see the permission dialog the second time the permission is checked review your configuration. Keep in mind you need to let Dexter know the rationale you can show was closed or not by calling ``token?.continuePermissionRequest()``. If you don't do this, the next time the permission is requested, the OS dialog asking for this permission won't be shown. You can find more information about this in [here](https://github.com/Karumi/Dexter/issues/105). This is an example of how a multiple permission request should be implemented:
```kotlin
button.setOnClickListener {
Dexter.withContext(this@MainActivity)
.withPermissions(
Manifest.permission.ACCESS_COARSE_LOCATION
,Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(object: MultiplePermissionsListener {
override fun onPermissionsChecked(report: MultiplePermissionsReport?) {
report?.let {
if(report.areAllPermissionsGranted()){
toast("OK")
}
}
}
override fun onPermissionRationaleShouldBeShown(
permissions: MutableList<PermissionRequest>?,
token: PermissionToken?
) {
// Remember to invoke this method when the custom rationale is closed
// or just by default if you don't want to use any custom rationale.
token?.continuePermissionRequest()
}
})
.withErrorListener {
toast(it.name)
}
.check()
}
```
Caveats
-------
* For permissions that did not exist before API Level 16, you should check the OS version and use *ContextCompat.checkSelfPermission*. See [You Cannot Hold Non-Existent Permissions](https://commonsware.com/blog/2015/11/09/you-cannot-hold-nonexistent-permissions.html).
* Don't call Dexter from an Activity with the flag `noHistory` enabled. When a permission is requested, Dexter creates its own Activity internally and pushes it into the stack causing the original Activity to be dismissed.
* Permissions `SYSTEM_ALERT_WINDOW` and `WRITE_SETTINGS` are considered special by Android. Dexter doesn't handle those and you'll need to request them in the old fashioned way.
# Contributors
Thank you all for your work!
| [<img src="https://avatars1.githubusercontent.com/u/3494156?v=4" width="100px;"/><br /><sub><b>Carlos Morera de la Chica</b></sub>](https://github.com/CarlosMChica) | [<img src="https://avatars0.githubusercontent.com/u/237122?v=4" width="100px;"/><br /><sub><b>Alex Florescu</b></sub>](https://github.com/anothem) | [<img src="https://avatars3.githubusercontent.com/u/416941?v=4" width="100px;"/><br /><sub><b>Pedro Veloso</b></sub>](https://github.com/pedronveloso) | [<img src="https://avatars3.githubusercontent.com/u/1636897?v=4" width="100px;"/><br /><sub><b>Dion Segijn</b></sub>](https://github.com/DanielMartinus) | [<img src="https://avatars0.githubusercontent.com/u/837104?v=4" width="100px;"/><br /><sub><b>Christian Panadero</b></sub>](https://github.com/PaNaVTEC) |
| :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars3.githubusercontent.com/u/6309101?v=4" width="100px;"/><br /><sub><b>Vignesh</b></sub>](https://github.com/VigneshPeriasami) | [<img src="https://avatars0.githubusercontent.com/u/5009609?v=4" width="100px;"/><br /><sub><b>Andy French</b></sub>](https://github.com/AndyFrench) | [<img src="https://avatars2.githubusercontent.com/u/887462?v=4" width="100px;"/><br /><sub><b>Bernat Borrás Paronella</b></sub>](https://github.com/alorma) | [<img src="https://avatars0.githubusercontent.com/u/21121410?v=4" width="100px;"/><br /><sub><b>Bastien Paul</b></sub>](https://github.com/bastienpaulfr) | [<img src="https://avatars2.githubusercontent.com/u/4190298?v=4" width="100px;"/><br /><sub><b>Bas Broek</b></sub>](https://github.com/BasThomas) |
| [<img src="https://avatars2.githubusercontent.com/u/6371716?v=4" width="100px;"/><br /><sub><b>Bartek Lipinski</b></sub>](https://github.com/blipinsk) | [<img src="https://avatars0.githubusercontent.com/u/4202457?v=4" width="100px;"/><br /><sub><b>emmano</b></sub>](https://github.com/emmano) | [<img src="https://avatars1.githubusercontent.com/u/7110368?v=4" width="100px;"/><br /><sub><b>Konrad Morawski</b></sub>](https://github.com/Konrad-Morawski) | [<img src="https://avatars3.githubusercontent.com/u/18151375?v=4" width="100px;"/><br /><sub><b>Hrant Alaverdyan</b></sub>](https://github.com/rid-hrant) | [<img src="https://avatars2.githubusercontent.com/u/1730320?v=4" width="100px;"/><br /><sub><b>Carla</b></sub>](https://github.com/iriberri)
| [<img src="https://avatars1.githubusercontent.com/u/3470701?v=4" width="100px;"/><br /><sub><b>Pavel Stepanov</b></sub>](https://github.com/stefan-nsk) | [<img src="https://avatars2.githubusercontent.com/u/16154410?v=4" width="100px;"/><br /><sub><b>Emmett Wilson</b></sub>](https://github.com/EmmettWilson) | [<img src="https://avatars0.githubusercontent.com/u/16763485?v=4" width="100px;"/><br /><sub><b>Max</b></sub>](https://github.com/TheMaxCoder) | [<img src="https://avatars3.githubusercontent.com/u/18224621?v=4" width="100px;"/><br /><sub><b>Al B.</b></sub>](https://github.com/albodelu) | [<img src="https://avatars0.githubusercontent.com/u/578021?v=4" width="100px;"/><br /><sub><b>Vladislav Bauer</b></sub>](https://github.com/vbauer)
| [<img src="https://avatars2.githubusercontent.com/u/4047514?v=4" width="100px;"/><br /><sub><b>Jc Miñarro</b></sub>](https://github.com/JcMinarro) | [<img src="https://avatars0.githubusercontent.com/u/944957?v=4" width="100px;"/><br /><sub><b>handrenliang</b></sub>](https://github.com/handrenliang) | [<img src="https://avatars3.githubusercontent.com/u/19774257?v=4" width="100px;"/><br /><sub><b>jcruzsousa</b></sub>](https://github.com/jcruzsousa) | [<img src="https://avatars0.githubusercontent.com/u/18180559?v=4" width="100px;"/><br /><sub><b>lzdon</b></sub>](https://github.com/lzdon)
Do you want to contribute?
--------------------------
Feel free to add any useful feature to the library, we will be glad to improve it with your help.
Keep in mind that your PRs **must** be validated by Travis-CI. Please, run a local build with ``./gradlew checkstyle build test`` before submiting your code.
Libraries used in this project
------------------------------
* [Butterknife][3]
* [JUnit][4]
* [Mockito][5]
License
-------
Copyright 2015 Karumi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
[1]: ./art/sample.gif
[2]: http://www.google.es/design/spec/patterns/permissions.html
[3]: https://github.com/JakeWharton/butterknife
[4]: https://github.com/junit-team/junit
[5]: https://github.com/mockito/mockito
[karumilogo]: https://cloud.githubusercontent.com/assets/858090/11626547/e5a1dc66-9ce3-11e5-908d-537e07e82090.png
How to get character info from a file?
CharacterData Character = new CharacterData()
{
health = 100,
power = 100,
speed = 100,
dexterity = 100,
hungry = 100,
weapon = "Fist",
inventory = new List<string>()
{
"Bread"
}
};
string stringjson = JsonConvert.SerializeObject(Character);
string path = @"C:\DEV\StackOverflow\";
string characterPath = path + "johnny.json";
File.WriteAllText(characterPath, stringjson);
public int GetCharInfo(string charName, string stat)
{
string json = File.ReadAllText(path + $"{charName}.json");
JObject obj = JObject.Parse(json);
return (int)obj[stat];
}
GetCharInfo("johnny", "power")
100
if(obj.ContainsKey(stat))
return (int)obj[stat];
-----------------------
CharacterData Character = new CharacterData()
{
health = 100,
power = 100,
speed = 100,
dexterity = 100,
hungry = 100,
weapon = "Fist",
inventory = new List<string>()
{
"Bread"
}
};
string stringjson = JsonConvert.SerializeObject(Character);
string path = @"C:\DEV\StackOverflow\";
string characterPath = path + "johnny.json";
File.WriteAllText(characterPath, stringjson);
public int GetCharInfo(string charName, string stat)
{
string json = File.ReadAllText(path + $"{charName}.json");
JObject obj = JObject.Parse(json);
return (int)obj[stat];
}
GetCharInfo("johnny", "power")
100
if(obj.ContainsKey(stat))
return (int)obj[stat];
-----------------------
CharacterData Character = new CharacterData()
{
health = 100,
power = 100,
speed = 100,
dexterity = 100,
hungry = 100,
weapon = "Fist",
inventory = new List<string>()
{
"Bread"
}
};
string stringjson = JsonConvert.SerializeObject(Character);
string path = @"C:\DEV\StackOverflow\";
string characterPath = path + "johnny.json";
File.WriteAllText(characterPath, stringjson);
public int GetCharInfo(string charName, string stat)
{
string json = File.ReadAllText(path + $"{charName}.json");
JObject obj = JObject.Parse(json);
return (int)obj[stat];
}
GetCharInfo("johnny", "power")
100
if(obj.ContainsKey(stat))
return (int)obj[stat];
-----------------------
CharacterData Character = new CharacterData()
{
health = 100,
power = 100,
speed = 100,
dexterity = 100,
hungry = 100,
weapon = "Fist",
inventory = new List<string>()
{
"Bread"
}
};
string stringjson = JsonConvert.SerializeObject(Character);
string path = @"C:\DEV\StackOverflow\";
string characterPath = path + "johnny.json";
File.WriteAllText(characterPath, stringjson);
public int GetCharInfo(string charName, string stat)
{
string json = File.ReadAllText(path + $"{charName}.json");
JObject obj = JObject.Parse(json);
return (int)obj[stat];
}
GetCharInfo("johnny", "power")
100
if(obj.ContainsKey(stat))
return (int)obj[stat];
Python - How do I take items from a list and assign them to values in a dictionary?
standardArray = [15, 14, 13, 12, 10, 8]
stats = {"Strength":[], "Dexterity":[], "Constitution":[], "Intelligence":[], "Wisdom":[], "Charisma":[]}
print("Please select a value from this list for each one of your character stats!")
for s in stats:
choice = None
while True:
choice = input(f"Please select how many points you would like your '{s}' stat to start with: \nvalid entries are {standardArray} :")
try:
choice = int(choice)
except ValueError:
print(f'"{choice}" is not a valid input')
continue
if choice in standardArray:
stats[s].append(choice)
standardArray.remove(choice)
break
print(stats)
Dynamically add matTooltip in text for specific keywords
export type keyword = {
word: string;
tooltip: string;
};
keywords: { [key: string]: keyword } = {
disadvantage: {
word: 'disadvantage',
tooltip: 'advantage description',
},
incapacitated: {
word: 'incapacitated',
tooltip: 'incapacitated description',
},
};
toDynamicText(text: string): (string | keyword)[] {
const res: (string | keyword)[] = [];
const tokens = text.split(' ');
let i = 0;
for (const token of tokens) {
let keyword = this.keywords[token.toLowerCase()]; //undefined if word is not a keyword
if (keyword) {
i = res.push(keyword);
} else {
if (!res[i]) res[i] = token;
else res[i] += ' ' + token;
}
}
return res;
}
[
"When you take the Dodge action, you focus entirely on avoiding attacks. Until the start of your next turn, any Attack roll made against you has",
{
"word": "disadvantage",
"tooltip": "advantage description"
},
"if you can see the attacker, and you make Dexterity Saving Throws with advantage. You lose this benefit if you are",
{
"word": "incapacitated",
"tooltip": "incapacitated description"
},
"(as explained in Conditions ) or if your speed drops to 0. "
]
text =
'When you take the Dodge action, you focus entirely on avoiding attacks. ' +
'Until the start of your next turn, any Attack roll made against you has disadvantage if you can see the attacker, ' +
'and you make Dexterity Saving Throws with advantage. ' +
'You lose this benefit if you are Incapacitated (as explained in Conditions ) or if your speed drops to 0. ';
dynamicText: (string | keyword)[] = [];
constructor(private dynamicTextService: DynamicTextService) {}
ngOnInit(): void {
this.dynamicText = this.dynamicTextService.toDynamicText(this.text);
}
isString(token: any) {
return typeof token === 'string';
}
//Typescript compiler wants a typecast, so we have two helper functions
getTooltip(token: string | keyword) {
return (token as keyword).tooltip;
}
getWord(token: string | keyword) {
return (token as keyword).word;
}
<p>
<ng-container *ngFor="let token of dynamicText">
<ng-container *ngIf="isString(token)">{{ token }}</ng-container>
<ng-container *ngIf="!isString(token)"
><b [matTooltip]="getTooltip(token)" style="cursor: pointer">
{{ getWord(token) }}
</b></ng-container
>
</ng-container>
</p>
-----------------------
export type keyword = {
word: string;
tooltip: string;
};
keywords: { [key: string]: keyword } = {
disadvantage: {
word: 'disadvantage',
tooltip: 'advantage description',
},
incapacitated: {
word: 'incapacitated',
tooltip: 'incapacitated description',
},
};
toDynamicText(text: string): (string | keyword)[] {
const res: (string | keyword)[] = [];
const tokens = text.split(' ');
let i = 0;
for (const token of tokens) {
let keyword = this.keywords[token.toLowerCase()]; //undefined if word is not a keyword
if (keyword) {
i = res.push(keyword);
} else {
if (!res[i]) res[i] = token;
else res[i] += ' ' + token;
}
}
return res;
}
[
"When you take the Dodge action, you focus entirely on avoiding attacks. Until the start of your next turn, any Attack roll made against you has",
{
"word": "disadvantage",
"tooltip": "advantage description"
},
"if you can see the attacker, and you make Dexterity Saving Throws with advantage. You lose this benefit if you are",
{
"word": "incapacitated",
"tooltip": "incapacitated description"
},
"(as explained in Conditions ) or if your speed drops to 0. "
]
text =
'When you take the Dodge action, you focus entirely on avoiding attacks. ' +
'Until the start of your next turn, any Attack roll made against you has disadvantage if you can see the attacker, ' +
'and you make Dexterity Saving Throws with advantage. ' +
'You lose this benefit if you are Incapacitated (as explained in Conditions ) or if your speed drops to 0. ';
dynamicText: (string | keyword)[] = [];
constructor(private dynamicTextService: DynamicTextService) {}
ngOnInit(): void {
this.dynamicText = this.dynamicTextService.toDynamicText(this.text);
}
isString(token: any) {
return typeof token === 'string';
}
//Typescript compiler wants a typecast, so we have two helper functions
getTooltip(token: string | keyword) {
return (token as keyword).tooltip;
}
getWord(token: string | keyword) {
return (token as keyword).word;
}
<p>
<ng-container *ngFor="let token of dynamicText">
<ng-container *ngIf="isString(token)">{{ token }}</ng-container>
<ng-container *ngIf="!isString(token)"
><b [matTooltip]="getTooltip(token)" style="cursor: pointer">
{{ getWord(token) }}
</b></ng-container
>
</ng-container>
</p>
-----------------------
export type keyword = {
word: string;
tooltip: string;
};
keywords: { [key: string]: keyword } = {
disadvantage: {
word: 'disadvantage',
tooltip: 'advantage description',
},
incapacitated: {
word: 'incapacitated',
tooltip: 'incapacitated description',
},
};
toDynamicText(text: string): (string | keyword)[] {
const res: (string | keyword)[] = [];
const tokens = text.split(' ');
let i = 0;
for (const token of tokens) {
let keyword = this.keywords[token.toLowerCase()]; //undefined if word is not a keyword
if (keyword) {
i = res.push(keyword);
} else {
if (!res[i]) res[i] = token;
else res[i] += ' ' + token;
}
}
return res;
}
[
"When you take the Dodge action, you focus entirely on avoiding attacks. Until the start of your next turn, any Attack roll made against you has",
{
"word": "disadvantage",
"tooltip": "advantage description"
},
"if you can see the attacker, and you make Dexterity Saving Throws with advantage. You lose this benefit if you are",
{
"word": "incapacitated",
"tooltip": "incapacitated description"
},
"(as explained in Conditions ) or if your speed drops to 0. "
]
text =
'When you take the Dodge action, you focus entirely on avoiding attacks. ' +
'Until the start of your next turn, any Attack roll made against you has disadvantage if you can see the attacker, ' +
'and you make Dexterity Saving Throws with advantage. ' +
'You lose this benefit if you are Incapacitated (as explained in Conditions ) or if your speed drops to 0. ';
dynamicText: (string | keyword)[] = [];
constructor(private dynamicTextService: DynamicTextService) {}
ngOnInit(): void {
this.dynamicText = this.dynamicTextService.toDynamicText(this.text);
}
isString(token: any) {
return typeof token === 'string';
}
//Typescript compiler wants a typecast, so we have two helper functions
getTooltip(token: string | keyword) {
return (token as keyword).tooltip;
}
getWord(token: string | keyword) {
return (token as keyword).word;
}
<p>
<ng-container *ngFor="let token of dynamicText">
<ng-container *ngIf="isString(token)">{{ token }}</ng-container>
<ng-container *ngIf="!isString(token)"
><b [matTooltip]="getTooltip(token)" style="cursor: pointer">
{{ getWord(token) }}
</b></ng-container
>
</ng-container>
</p>
-----------------------
export type keyword = {
word: string;
tooltip: string;
};
keywords: { [key: string]: keyword } = {
disadvantage: {
word: 'disadvantage',
tooltip: 'advantage description',
},
incapacitated: {
word: 'incapacitated',
tooltip: 'incapacitated description',
},
};
toDynamicText(text: string): (string | keyword)[] {
const res: (string | keyword)[] = [];
const tokens = text.split(' ');
let i = 0;
for (const token of tokens) {
let keyword = this.keywords[token.toLowerCase()]; //undefined if word is not a keyword
if (keyword) {
i = res.push(keyword);
} else {
if (!res[i]) res[i] = token;
else res[i] += ' ' + token;
}
}
return res;
}
[
"When you take the Dodge action, you focus entirely on avoiding attacks. Until the start of your next turn, any Attack roll made against you has",
{
"word": "disadvantage",
"tooltip": "advantage description"
},
"if you can see the attacker, and you make Dexterity Saving Throws with advantage. You lose this benefit if you are",
{
"word": "incapacitated",
"tooltip": "incapacitated description"
},
"(as explained in Conditions ) or if your speed drops to 0. "
]
text =
'When you take the Dodge action, you focus entirely on avoiding attacks. ' +
'Until the start of your next turn, any Attack roll made against you has disadvantage if you can see the attacker, ' +
'and you make Dexterity Saving Throws with advantage. ' +
'You lose this benefit if you are Incapacitated (as explained in Conditions ) or if your speed drops to 0. ';
dynamicText: (string | keyword)[] = [];
constructor(private dynamicTextService: DynamicTextService) {}
ngOnInit(): void {
this.dynamicText = this.dynamicTextService.toDynamicText(this.text);
}
isString(token: any) {
return typeof token === 'string';
}
//Typescript compiler wants a typecast, so we have two helper functions
getTooltip(token: string | keyword) {
return (token as keyword).tooltip;
}
getWord(token: string | keyword) {
return (token as keyword).word;
}
<p>
<ng-container *ngFor="let token of dynamicText">
<ng-container *ngIf="isString(token)">{{ token }}</ng-container>
<ng-container *ngIf="!isString(token)"
><b [matTooltip]="getTooltip(token)" style="cursor: pointer">
{{ getWord(token) }}
</b></ng-container
>
</ng-container>
</p>
-----------------------
export type keyword = {
word: string;
tooltip: string;
};
keywords: { [key: string]: keyword } = {
disadvantage: {
word: 'disadvantage',
tooltip: 'advantage description',
},
incapacitated: {
word: 'incapacitated',
tooltip: 'incapacitated description',
},
};
toDynamicText(text: string): (string | keyword)[] {
const res: (string | keyword)[] = [];
const tokens = text.split(' ');
let i = 0;
for (const token of tokens) {
let keyword = this.keywords[token.toLowerCase()]; //undefined if word is not a keyword
if (keyword) {
i = res.push(keyword);
} else {
if (!res[i]) res[i] = token;
else res[i] += ' ' + token;
}
}
return res;
}
[
"When you take the Dodge action, you focus entirely on avoiding attacks. Until the start of your next turn, any Attack roll made against you has",
{
"word": "disadvantage",
"tooltip": "advantage description"
},
"if you can see the attacker, and you make Dexterity Saving Throws with advantage. You lose this benefit if you are",
{
"word": "incapacitated",
"tooltip": "incapacitated description"
},
"(as explained in Conditions ) or if your speed drops to 0. "
]
text =
'When you take the Dodge action, you focus entirely on avoiding attacks. ' +
'Until the start of your next turn, any Attack roll made against you has disadvantage if you can see the attacker, ' +
'and you make Dexterity Saving Throws with advantage. ' +
'You lose this benefit if you are Incapacitated (as explained in Conditions ) or if your speed drops to 0. ';
dynamicText: (string | keyword)[] = [];
constructor(private dynamicTextService: DynamicTextService) {}
ngOnInit(): void {
this.dynamicText = this.dynamicTextService.toDynamicText(this.text);
}
isString(token: any) {
return typeof token === 'string';
}
//Typescript compiler wants a typecast, so we have two helper functions
getTooltip(token: string | keyword) {
return (token as keyword).tooltip;
}
getWord(token: string | keyword) {
return (token as keyword).word;
}
<p>
<ng-container *ngFor="let token of dynamicText">
<ng-container *ngIf="isString(token)">{{ token }}</ng-container>
<ng-container *ngIf="!isString(token)"
><b [matTooltip]="getTooltip(token)" style="cursor: pointer">
{{ getWord(token) }}
</b></ng-container
>
</ng-container>
</p>
-----------------------
export type keyword = {
word: string;
tooltip: string;
};
keywords: { [key: string]: keyword } = {
disadvantage: {
word: 'disadvantage',
tooltip: 'advantage description',
},
incapacitated: {
word: 'incapacitated',
tooltip: 'incapacitated description',
},
};
toDynamicText(text: string): (string | keyword)[] {
const res: (string | keyword)[] = [];
const tokens = text.split(' ');
let i = 0;
for (const token of tokens) {
let keyword = this.keywords[token.toLowerCase()]; //undefined if word is not a keyword
if (keyword) {
i = res.push(keyword);
} else {
if (!res[i]) res[i] = token;
else res[i] += ' ' + token;
}
}
return res;
}
[
"When you take the Dodge action, you focus entirely on avoiding attacks. Until the start of your next turn, any Attack roll made against you has",
{
"word": "disadvantage",
"tooltip": "advantage description"
},
"if you can see the attacker, and you make Dexterity Saving Throws with advantage. You lose this benefit if you are",
{
"word": "incapacitated",
"tooltip": "incapacitated description"
},
"(as explained in Conditions ) or if your speed drops to 0. "
]
text =
'When you take the Dodge action, you focus entirely on avoiding attacks. ' +
'Until the start of your next turn, any Attack roll made against you has disadvantage if you can see the attacker, ' +
'and you make Dexterity Saving Throws with advantage. ' +
'You lose this benefit if you are Incapacitated (as explained in Conditions ) or if your speed drops to 0. ';
dynamicText: (string | keyword)[] = [];
constructor(private dynamicTextService: DynamicTextService) {}
ngOnInit(): void {
this.dynamicText = this.dynamicTextService.toDynamicText(this.text);
}
isString(token: any) {
return typeof token === 'string';
}
//Typescript compiler wants a typecast, so we have two helper functions
getTooltip(token: string | keyword) {
return (token as keyword).tooltip;
}
getWord(token: string | keyword) {
return (token as keyword).word;
}
<p>
<ng-container *ngFor="let token of dynamicText">
<ng-container *ngIf="isString(token)">{{ token }}</ng-container>
<ng-container *ngIf="!isString(token)"
><b [matTooltip]="getTooltip(token)" style="cursor: pointer">
{{ getWord(token) }}
</b></ng-container
>
</ng-container>
</p>
Meshcat not showing the changes to a Free Body's Pose
simulator = Simulator(diagram)
diagram_context = simulator.get_mutable_context()
station_context = station.GetMyMutableContextFromRoot(diagram_context)
station.SetRandomPoses(station_context)
Android Permissions Helper Functions
class PermissionsHelper(private val activity: Activity) {
fun requestPermissionsFromDevice(
arrayPermissions: Array<String>, permissionsResultInterface: PermissionsResultInterface
) {
setPermissionResultInterface(permissionsResultInterface)
getMyPermissionRequestActivity().launch(arrayPermissions)
}
fun checkPermissionsFromDevice(permission: String): Boolean {
return ContextCompat.checkSelfPermission(activity, permission) ==
PackageManager.PERMISSION_GRANTED
}
}
interface PermissionsResultInterface {
fun onPermissionFinishResult(permissions: MutableMap<String, Boolean>)
}
private fun clearFiles(firstCall: Boolean = false) {
if (verifyStoragePermissions(firstCall)) {
val dir = File(getExternalFilesDir(null).toString())
removeFileOrDirectory(dir)
Toast.makeText(
applicationContext,
resources.getString(R.string.done),
Toast.LENGTH_SHORT
).show()
}
}
private fun verifyStoragePermissions(firstCall: Boolean = false): Boolean {
val arrayListPermissions = arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
)
for (permission in arrayListPermissions) {
if (!PermissionsHelper(this).checkPermissionsFromDevice(permission)) {
if (firstCall) PermissionsHelper(this)
.requestPermissionsFromDevice(arrayListPermissions, this)
else PermissionsDialogs(this).showPermissionsErrorDialog()
return false
}
}
return true
}
override fun onPermissionFinishResult(permissions: MutableMap<String, Boolean>) {
clearFiles()
}
-----------------------
class PermissionsHelper(private val activity: Activity) {
fun requestPermissionsFromDevice(
arrayPermissions: Array<String>, permissionsResultInterface: PermissionsResultInterface
) {
setPermissionResultInterface(permissionsResultInterface)
getMyPermissionRequestActivity().launch(arrayPermissions)
}
fun checkPermissionsFromDevice(permission: String): Boolean {
return ContextCompat.checkSelfPermission(activity, permission) ==
PackageManager.PERMISSION_GRANTED
}
}
interface PermissionsResultInterface {
fun onPermissionFinishResult(permissions: MutableMap<String, Boolean>)
}
private fun clearFiles(firstCall: Boolean = false) {
if (verifyStoragePermissions(firstCall)) {
val dir = File(getExternalFilesDir(null).toString())
removeFileOrDirectory(dir)
Toast.makeText(
applicationContext,
resources.getString(R.string.done),
Toast.LENGTH_SHORT
).show()
}
}
private fun verifyStoragePermissions(firstCall: Boolean = false): Boolean {
val arrayListPermissions = arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
)
for (permission in arrayListPermissions) {
if (!PermissionsHelper(this).checkPermissionsFromDevice(permission)) {
if (firstCall) PermissionsHelper(this)
.requestPermissionsFromDevice(arrayListPermissions, this)
else PermissionsDialogs(this).showPermissionsErrorDialog()
return false
}
}
return true
}
override fun onPermissionFinishResult(permissions: MutableMap<String, Boolean>) {
clearFiles()
}
-----------------------
class PermissionsHelper(private val activity: Activity) {
fun requestPermissionsFromDevice(
arrayPermissions: Array<String>, permissionsResultInterface: PermissionsResultInterface
) {
setPermissionResultInterface(permissionsResultInterface)
getMyPermissionRequestActivity().launch(arrayPermissions)
}
fun checkPermissionsFromDevice(permission: String): Boolean {
return ContextCompat.checkSelfPermission(activity, permission) ==
PackageManager.PERMISSION_GRANTED
}
}
interface PermissionsResultInterface {
fun onPermissionFinishResult(permissions: MutableMap<String, Boolean>)
}
private fun clearFiles(firstCall: Boolean = false) {
if (verifyStoragePermissions(firstCall)) {
val dir = File(getExternalFilesDir(null).toString())
removeFileOrDirectory(dir)
Toast.makeText(
applicationContext,
resources.getString(R.string.done),
Toast.LENGTH_SHORT
).show()
}
}
private fun verifyStoragePermissions(firstCall: Boolean = false): Boolean {
val arrayListPermissions = arrayOf(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
)
for (permission in arrayListPermissions) {
if (!PermissionsHelper(this).checkPermissionsFromDevice(permission)) {
if (firstCall) PermissionsHelper(this)
.requestPermissionsFromDevice(arrayListPermissions, this)
else PermissionsDialogs(this).showPermissionsErrorDialog()
return false
}
}
return true
}
override fun onPermissionFinishResult(permissions: MutableMap<String, Boolean>) {
clearFiles()
}
Find The Best Match In List of string
from difflib import SequenceMatcher
target = "dexternewblood"
lst = ['Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter ', 'Dexter: New Blood ', 'Dexter ', 'Dexter ']
a = [SequenceMatcher(None, i, target).ratio() for i in lst]
index = a.index(max(a)) # 6
match = lst[index] # 'Dexter: New Blood '
Why flutter local notifications give exceptions
static Future _notificationDetails() async {
return const NotificationDetails(
android: AndroidNotificationDetails(
'channel id',
'channel name',
channelDescription: 'channel description',
importance: Importance.max,
icon: "ic_notification", //<-- Add this parameter
),
//iOS: IOSNotificationDetails(),
);
}
How to convert a string to enum in Godot?
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
-----------------------
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
-----------------------
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
-----------------------
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
-----------------------
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
-----------------------
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
-----------------------
enum MyEnum {
STRENGTH, DEXTERITY, CONSTITUTION, INTELLIGENCE, WISDOM, CHARISMA
}
var boost = "DEXTERITY"
print("Value of ", boost, ": ", MyEnum.get(boost))
Value of DEXTERITY: 1
var value = MyEnum.DEXTERITY
print("Name of ", value, ": ", MyEnum.keys()[value])
Name of 1: DEXTERITY
for boost in MyEnum:
print(boost)
STRENGTH
DEXTERITY
CONSTITUTION
INTELLIGENCE
WISDOM
CHARISMA
Why does exoplayer 2.13.3 get downloaded, when I have included a dependency for exoplayer 2.8.4?
implementation('com.google.android.exoplayer:exoplayer') {
version {
strictly '2.8.4'
}
}
Oracle Self-Join
no id id1<>id2 id1<id2
A B C A B C A B C
A * * * A * * A
B * * * B * * B *
C * * * C * * C * *
-----------------------
SELECT e1.hire_date,
e1.first_name || ' ' || e1.last_name AS employee1,
e2.first_name || ' ' || e2.last_name AS employee2
FROM employees e1
INNER JOIN employees e2
ON e1.employee_id != e2.employee_id AND e1.hire_date = e2.hire_date
ORDER BY
e1.hire_date DESC,
employee1,
employee2;
QUESTION
How to get character info from a file?
Asked 2022-Apr-17 at 17:15I created a file which named by a given name by user, and add some stats in it for example : healt, power, speed, inventory like so. Now, i want to create a function that get stats from given path. It need find the file from given name and get the stat.
I tried re-read the file as note/json so on and it didnt work very well. I want to get variables like : if(inventory.Contains("Bread"))
Note : I tried to save files as .json but it saved as unknown note i dont know how and why.
...
...
CharacterData Character = new CharacterData()
{
health = 100,
power = 100,
speed = 100,
dexterity = 100,
hungry = 100,
weapon = "Fist",
inventory = new List<string>()
{
"Bread"
}
};
string stringjson = JsonConvert.SerializeObject(Character);
File.WriteAllText(characterPath, stringjson);
}
public int GetCharInfo(string charName,string stat)
{
//return (stat value)
}
ANSWER
Answered 2022-Apr-17 at 16:19I guess begin with File.ReadAllText(path) Example of usage and documentation
https://docs.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=net-6.0
Then you can covert the result to JSON and extract the information that you want
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit