SimplicityApks / ReminderDatePicker

A Google Keep-like Date and Time Picker for reminders
Apache License 2.0
74 stars 26 forks source link

DateSpinner standalone #24

Closed chrisonline closed 9 years ago

chrisonline commented 9 years ago

I use now the DateSpinner standalone and not the ReminderDatePicker with date and time. I splitted it to 2 views. In my XML I have now a DateSpinner and a TimeSpinner.

On inital load I get the correct result and I see the date and time. If I select a date from the date picker it is working great.

But if I set the date via code with "setSelectedDate" I see as "date" the text "Pick a date". This has worked with the "ReminderDatePicker" layout, but since I splitted it into 2 I have this issue.

The same with the time spinner.

It is only working if the date/time I set is in the spinner. Otherwise I see the text "Pick a date" instead of the date.

Bug? Or is it too late to code :-)

SimplicityApks commented 9 years ago

mhhh that is very strange as it shouldn't be different when you use a ReminderDatePicker instead of the 2 spinners, because ReminderDatePicker just passes the setSelectedDate call to the spinner. When exactly do you call setSelectedDate? During onCreate or some activity method or after e.g. a button press? And you're just passing a new Calendar object, right?

I'll try to have a look at it when I get the time...

chrisonline commented 9 years ago

Yes it is very strange. I have seen that it should be the same.

I do it if I edit an entry. So onCreate will init the the date to today and I set the date format and other options.

Afterwards I load the existing data and call setSelectedDate with an calendar object coming from the loaded data.

Br Christian

Sent with my Smartphone📱 Am 14.06.2015 18:26 schrieb "SimplicityApks" notifications@github.com:

mhhh that is very strange as it shouldn't be different when you use a ReminderDatePicker instead of the 2 spinners, because ReminderDatePicker just passes the setSelectedDate call to the spinner. When exactly do you call setSelectedDate? During onCreate or some activity method or after e.g. a button press? And you're just passing a new Calendar object, right?

I'll try to have a look at it when I get the time...

— Reply to this email directly or view it on GitHub https://github.com/SimplicityApks/ReminderDatePicker/issues/24#issuecomment-111845488 .

SimplicityApks commented 9 years ago

Well I finally found your bug... it is (as I expected) some weird behaviour with the spinner selection when the data set changes right before the setSelection call is placed. In detail, it'll happen when you set a flag that changes the spinner items (FLAG_MONTH for instance) and then call setSelectedDate() right away. The library will call setSelectedItem() with the right position (last item position+1, so if we have 4 items in the spinner that is 5). Then getView() should only be called once with the new position, but is instead called once for each item since we changed the dataset and it needs to re-measure everything and once before that with the position of the footer (4 in this case). So my theory is that the system gets a call for setSelection and a dataSetChanged call at the same time and figures hey let's do the setSelection first (for whatever reason), then notices that the selection is kinda out of scope but corrects it to the last item and then processes the data change.

I'll see what I can do about it, but it will be hard to find a good fix. The only idea I have right now that will work is to wait with the setSelection call until the Spinner has worked out the data change. So if you want to have a quick fix, either try to do your setSelection calls before you change the flags, or use a Handler to delay setSelection like so:

new Handler(getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            datePicker.setSelectedDate(calendar);
        }
    }, 1);

I have no idea why it works perfectly fine when using the ReminderDatePicker class as a wrapper, will investigate that...

SimplicityApks commented 9 years ago

Well this was the worst kind of bug you can have... it turns out that the Spinner's getCount() method is returning a value that is updated only after the data set has completely changed while the adapter uses the right value. Most of the time you wouldn't notice that, but well here it made all the difference. Should work fine for you now, gonna push a new version to maven central when it's tested.

chrisonline commented 9 years ago

Great thank you very much!!

chrisonline commented 9 years ago

Sorry the delay.. I have now tested it and it works for the TimeSpinner, but not for the DateSpinner. The same issue.

I have also tested your workaround to set the FLAGS AFTER the setSelectedDate method. If I set it after I see always "Today", if I set it before I see "Pick a Date" :-(

SimplicityApks commented 9 years ago

OK lol well I tested it only with DateSpinner xD. Can you tell me what flags and date you set and when? And by the way, is you app in the Play Store? Would love to try it out and see how you use the lib ;).

I'll try to reproduce it, but for me this worked with FLAG_MONTH and date set to the day after tomorrow (with a custom date format).

Edit: well I tried and right now I can't reproduce this, everything working fine over here ;)

chrisonline commented 9 years ago

Sorry the long delay. I have to recheck this. At the moment I use it without the flags. So working fine here too.

My app is already live since years :-) Search for "COL Reminder". But the current changes, split of date and time is not live. This will be updated with the next big "Material Design" update of my app.

Thanks again for your great support.

SimplicityApks commented 9 years ago

Ok, no problem good I'll leave it open then. Wow really cool app, and almost as if I built this library for it ;). Looking forward to the Material update!

chrisonline commented 9 years ago

You can close this case. I have tested it and it works.