How to get the Default Calendar in Android using the Expo SDK Calendar API? - react-native

SOLVED: Used await and a variable to store the promise.
I am trying to insert an event into the default calendar on an android device using react-native and expo sdk. Previously, one could create an event using the createEventAsync() method with the 1st argument as Calendar.DEFAULT but that is no longer supported. I can use the getCalendarsAsync() method to get all calenders but the object I am getting back is making no sense. Some code:
async addReservationToCalendar(date) {
await this.obtainCalendarPermission();
console.log(Calendar.getCalendarsAsync());
}
Someone had suggested that I could iterate through the responses to find a calendar with the isPrimary attribute set to true but I don't see how I achieve this. I have already gotten the permission and I am receiving the following promise as a response.
Promise {
"_40": 0,
"_55": null,
"_65": 0,
"_72": null,
}.
What do I need to do to set a calendar event on an android device using the expo Calendar API ?

Because that's a promise, you need to do like this
Calendar.getCalendarsAsync().then(calendars => console.log(calendars))

Thats a promise so either you can use then statement or async await .
So
Or you can do
async addReservationToCalendar(date) {
await this.obtainCalendarPermission();
let result = await Calendar.getCalendarsAsync();
console.log(result,'result')
console.log(Calendar.getCalendarsAsync());
}
or
Calendar.getCalendarsAsync().then(calendars => console.log(calendars,'calenders'))
Hope it helps.

Version 39 has an example at https://docs.expo.io/versions/v39.0.0/sdk/calendar/#usage:
async function getDefaultCalendarSource() {
const calendars = await Calendar.getCalendarsAsync();
const defaultCalendars = calendars.filter(each => each.source.name === 'Default');
return defaultCalendars[0].source;
}

Related

useInfiniteScroll utility of Vueuse is fetching same items again

Here is a reproducable stackblitz -
https://stackblitz.com/edit/nuxt-starter-jlzzah?file=components/users.vue
What's wrong? -
My code fetches 15 items, and with the bottom scroll event it should fetch another 15 different items but it just fetches same items again.
I've followed this bottom video for this implementation, it's okay in the video but not okay in my stackblitz code:
https://www.youtube.com/watch?v=WRnoQdIU-uE&t=3s&ab_channel=JohnKomarnicki
The only difference with this video is that he's using axios while i use useFetch of nuxt 3.
It's not really a cache issue. useFetch is "freezing" the API URL, the changes you make to the string directly will not be reliably reflected. If you want to add parameters to your API URL, use the query option of useFetch. This option is reactive, so you can use refs and the query will update with the refs. Alternatively, you can use the provided refresh() method
const limit = ref(10)
const skip = ref(20)
const { data: users, refresh: refreshUsers } = await useFetch(
'https://dummyjson.com/users',
{
query:{
limit,
skip
}
}
);
//use the data object directly to access the result
console.log(users.value)
//if you want to update users with different params later, simply change the ref and the query will update
limit.value = 23
//use refresh to manually refresh the query
refreshUsers()
This results in a first API call http://127.0.0.1:8000/api/tasks?limit=10&skip=20 and then a second with the updated values http://127.0.0.1:8000/api/tasks?limit=23&skip=20
You can leave the cache alone, as it is just a workaround, and will not work reliably.
[Updated] The useFetch() documentation is now updated as described below.
The query option is not well documented yet, as discussed in this nuxt issue. I've created a pull request on nuxt/framework to have it reflected in the documentation. Please see a full explanation below:
Using the query option, you can add search parameters to your query. This option is extended from unjs/ohmyfetch and is using ufo to create the URL. Objects are automatically stringified.
const param1 = ref('value1')
const { data, pending, error, refresh } = await useFetch('https://api.nuxtjs.dev/mountains',{
query: { param1, param2: 'value2' }
})
This results in https://api.nuxtjs.dev/mountains?param1=value1&param2=value2
Nuxt3's useFetch uses caching by default. Use initialCache: false option to disable it:
const getUsers = async (limit, skip) => {
const { data: users } = await useFetch(
`https://dummyjson.com/users?limit=${limit}&skip=${skip}`,
{
initialCache: false,
}
);
//returning fetched value
return users.value.users;
};
But you probably should use plain $fetch instead of useFetch in this scenario to avoid caching:
const getUsers = async (limit, skip) => {
const { users } = await $fetch(
`https://dummyjson.com/users?limit=${limit}&skip=${skip}`
);
//returning fetched value
return users;
};

data not fetching from firestore in react native

i created a collection in firestore called user and added a document and gave it 7 fields, but when i try fetching the data back using onSnapshot it does not return back any data. this is the collection
this is my code:
firestore()
.collection('user')
.doc('LCqSZXM17WUWiWFahyj6')
.onSnapshot((row) => {
console.log(row.data());
});
in the console it logs an empty array
thanks in advance!!!
You need to include more information.
Anyway, do you check your access rule of the Firestore database?
const doc = db.collection('cities').doc('SF');
const observer = doc.onSnapshot(docSnapshot => {
console.log(`Received doc snapshot: ${docSnapshot}`);
// ...
}, err => {
console.log(`Encountered error: ${err}`);
});
The above code is official docs code. Check error callback first.

anchor or completion for logging events

I'm using react native 0.61.5 with react-native-branch
I followed this tutorial as integration guide : https://blog.reactnativecoach.com/how-to-create-a-referall-system-using-branch-io-in-react-native-6f9f924149e0
is there a way when I use the branch.userCompletedAction() to get a completion or an anchor that the process has been done?
I need to fetch the credits as soon as the event has been logged.
this is my code :
export const logBranchEvent = async (
eventName,
eventParams,
) => {
if (loggedToBranch) {
await branch.userCompletedAction(eventName, eventParams);
const res = await branch.loadRewards();
console.log(res.credits)
}
};
I get that there are 0 credits but when I wait and the check the credits I get the proper amount of credits.
thanks
We do not have a callback for branch.userCompletedAction() method. This method is used to log events.
You can try to fetch the rewards when the action is completed. It would not take more than 2 seconds. That would be best possible approach here.
Let me know if you would need further information here.

How to test DatePickerIOS

I need to enter a day using the native IOS DatePicker. How do I spin the control wheel to a certain value that might not be in the view, since this starts out with the current date?
Detox 7.3.x supports interaction with iOS UIPickerView. Match UIPickerView by type, and interact with it.
await expect(element(by.type('UIPickerView'))).toBeVisible();
await element(by.type('UIPickerView')).setColumnToValue(1,"6");
await element(by.type('UIPickerView')).setColumnToValue(2,"34");
Docs are here:
https://github.com/wix/detox/blob/master/docs/APIRef.ActionsOnElement.md#setcolumntovaluecolumnvalue--ios-only
datetimepicker have Detox test you can check code here:
https://github.com/react-native-community/datetimepicker/blob/master/example/e2e/detoxTest.spec.js#L54
if (global.device.getPlatform() === 'ios') {
const testElement = await element(
by.type('UIPickerView').withAncestor(by.id('dateTimePicker')),
);
await testElement.setColumnToValue(0, 'November');
await testElement.setColumnToValue(1, '3');
await testElement.setColumnToValue(2, '1800');
await expect(dateTimeText).toHaveText('11/03/1800');
} else {
// for Android
}

Unable to find element and send keys

So just a brief overview, I'm unable to send keys to a edit text field for android. I've successfully sent keys to this element via browser but in order to test the mobile application fully, I'd like to run e2e tests on a device using Appium.
I've successfully got Appium to click button elements but am having a hard time getting it to send keys to an edit field element.
Am I able to find elements by model when testing with android as I have set in my forgot-pin-page.js?
pin-reset-page.js
var pinResetPage = function() {
describe('The Reset Pin Flow', function () {
forgotPinPage = forgotPinPageBuilder.getForgotPinPage(),
describe('The Forgot Pin Page', function () {
it('should allow the user to enter their MSISDN and continue',
function () {
forgotPinPage.enterMsisdn('123123123');
forgotPinPage.doForgotPin();
expect(securityPage.isOnSecurityPage()).toBe(true);
});
});
}
forgot-pin-page.js
'use strict';
var ForgotPin = function () {
var forgotPinPageContent = element(by.id('forgot')),
msisdnInput = element(by.model('data.msisdn')),
return {
enterMsisdn: function (msisdn) {
return msisdnInput.sendKeys(msisdn);
}
};
module.exports.getForgotPinPage = function () {
return new ForgotPin();
};
The error i'm getting is
? should allow the user to enter their MSISDN and continue
- Error: Timeout - Async callback was not invoked within timeout spe
cified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Not sure if this is the correct solution but it worked for me. I downgraded jasmine2 to jasmine and that seemed to resolved the async timeouts I was having.