How to add date picker dialoge in kotlin - kotlin

calendar = Calendar.getInstance()
y = calendar?.get(Calendar.YEAR)
m = calendar?.get(Calendar.MONTH)
d = calendar?.get(Calendar.DAY_OF_MONTH)
var dialog = DatePickerDialog(applicationContext, DatePickerDialog.OnDateSetListener { picker, i, i2, i3 ->
},y!!,m!!,d!!)

You are missing dialog.show()
It should look like this:
val datePickerDialog = DatePickerDialog(activity, DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth ->
}, year, month, day)
datePickerDialog.show()

Related

How to combine Flux and Mono

Assume we have the following function
fun getAnnualData(tenant: String): Flux<DashboardResponse> {
val year = LocalDate.now().year
val annualExpenses = expenseFinder.sumAllByYearAndTenant(year = year, tenant = tenant)
val warehouseExpenses = expenseFinder.sumWarehouseByYearAndTenant(year = year, tenant = tenant)
val annualRevenues = revenueFinder.sumAllByYearAndTenant(year = year, tenant = tenant)
return annualExpenses.zipWith(annualRevenues)
.filter { it.t1._id?.year == year }
.filter { it.t2._id?.year == year }
.map {
DashboardResponse(
period = Period.ANNUAL,
expenses = it.t1,
revenue = it.t2
)
}
}
and I want to add the warehouseExpenses to the returning value. How would I do that?
Where
annualExpenses = Flux
warehouseExpenses = Mono
annualRevenue = Flux
You can simply use a flatMap to combine Mono:
Flux.just(...).zipWith(Flux.just(...))
.filter(...)
.flatMap(tuple -> Mono.just(true).map(m -> new DashboardResponse(tuple.getT1(), tuple.getT2(), m)));

How do i add all values with the same year in list?

I have a list of strings that looks something like this :
2010 1 11.45
2010 2 09.50
2010 3 15.00
.
.
.
2019 12 11.10
(the list is a list of spends per month between the years of 2010 - 2019)
I have separated the list into 3 lists for each value such as :
for(....){
val part = list.get(i).split(" ")
val year = parts[0]
val month = parts[1]
val spend = parts[2]
yearlist.add(year)
monthlist.add(month)
spendlist.add(spend)
}
Now my issue is that I want to find the year with the highest spend total. How would I add all of the spends for each year?
I have tried the following method however this gives me an IndexOutOfBounds Exception:
var totalspend = 0
for(i in 0..yearlist.size-1){
if(yearlist[i]==yearlist[i+1]){//i get an error here
totalspend = totalspend + spendlift[i]
}
else if(yearlist[i]!=yearlist[i+1]){
totalspend = totalspend + spendlift[i]
spendforyear.add(totals(year[i], totalspend))
totalspend = 0.0
}
}
I assume the error is because i cant compare the final yearlist value with yearlist[i+1] as i+1 is out of bounds.
How would i go about solving this?
I would suggest saving the year, month and spend in a data class and then use collection functions:
data class Report(val year: String, val month: String, val spend: Double)
fun main() {
val reports = listOf(
Report("2010", "1", 11.45),
Report("2010", "2", 09.50),
Report("2010", "3", 15.00),
Report("2019", "12", 11.10)
)
val groupedReports = reports.groupBy { it.year }
val mostSpending = groupedReports.maxBy { it.value.sumByDouble { report -> report.spend } }
println(mostSpending?.key) // year with the most spending
println(mostSpending?.value?.sumByDouble { it.spend }) // the spending on that year
}

How to update date month and year in datepicker andriod studio kotlin

Day,year and month using these given values after being updated.
I want them to use updated values please help.
var day = 1
var month = 1
var year = 1999
val dpd = DatePickerDialog(this,android.R.style.Theme_Holo_Dialog,
DatePickerDialog.OnDateSetListener { datePicker, selyear,
monthOfYear, dayOfMonth ->
day = dayOfMonth
month = monthOfYear
year = selyear
tv.text = "$day - $month - $year" /*printing day month year*/
}, year, month, day
)
dpd.show()
val pday = 30 - day /* day = 1(Not the chosen value)*/
val pmonth = 12 - month
val pyear = 2019 - year
Change the variable declarations from val to var and update them inside the listener:
val c = Calendar.getInstance()
var day = c.get(Calendar.DAY_OF_MONTH)
var month = c.get(Calendar.MONTH)
var year = c.get(Calendar.YEAR)
val dpd = DatePickerDialog(
this,
android.R.style.Theme_Holo_Dialog,
DatePickerDialog.OnDateSetListener { datePicker, selyear, monthOfYear, dayOfMonth ->
day = dayOfMonth
month = monthOfYear + 1
year = selyear
tv.text = "$day - $month - $year"
}, year, month, day
)
dpd.show()

How to select all the days of the week from a given day?

Given a date I want to get all the other days of that same week, where in the week starts and ends on Saturday and Friday.
Model
public TimeModel
{
public int ID
public DateTime Day
}
What I'm currently doing
public Contrller{
private db = new ModelContext();
public AddDates(DateTime Date)
{
List<Model> list = new List<Model>();
int n = 0;
while(Date.DayofWeek != DayofWeek.Sauturday)
Date = Date.AddDats(-1) // keep subracting the date until I reach Saturday
while(Date.DayofWeek != DayofWeek.Friday
{
list.Add(Find(Date));
//Simply put for each date not Friday
// I find the corresponding model (the one with the same date)
//and add it to the list
Date = Date.AddDays(1)
}
list.Add(Find(Date)); // To add the Friday date to list
}
Note: Not exactly my code, just a simplification of my problem.
To summarize my solution:
a) Subtract given date until Saturday
b) Find model which corresponds to Date
c) Repeat until I reach Friday
d) Add to list once more to include Friday
Is it possible to create a linq/sql statement to simpyly select the needed models (with regards to Date)?
You can find a sample implementation that gets the current week.
List<TimeModel> list = new List<TimeModel>();
int n = 0;
for (int i = 0; i < 200; i++)
list.Add(new TimeModel{ID = i, Day = DateTime.Now.AddDays(-i)});
var currentDay = new TimeModel() {ID = 0, Day = DateTime.Now};
var previousSaturday = currentDay.Day.AddDays(-(int)currentDay.Day.DayOfWeek - 1);
var nextFriday = previousSaturday.AddDays(6);
var currentWeek = list.Where(p => p.Day.DayOfYear >= previousSaturday.DayOfYear && p.Day.DayOfYear <= nextFriday.DayOfYear).OrderBy(p => p.Day.DayOfYear).ToList();

Refreshing view panel from dojo widget

Got this code from #MichaelSaiz and altered it slightly, and the calendar widget looks/works great with the Calendar.css that comes with dojo 1.5.x on the Domino server.
However, I need to refresh a Calendar view when the user clicks on a date, and although fields are being refreshed OK, the view is behaving strangely, and I can't see why?
XSP.addOnLoad(function(){
dojo.require("dojox.widget.Calendar");
dojo.require("dojo.date","dijit.registry");
dojo.require("dojo.date.locale");
dojo.require("dijit.Calendar");
dojo.ready(function(){
// create the Calendar:
var selectedDate = null;
var calendar_body = new dojox.widget.Calendar({
value: new Date(),
onValueSelected: function(date){calendarDateClicked(date);
}
}, "calendar_body");
//create Click action
function calendarDateClicked(date){
var d = new Date(date);
var month = '' + (d.getMonth() + 1);
var day = '' + d.getDate();
var year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
var dateString = [day,month,year].join("/");
var y = dojo.date.locale.format(d, {datePattern:"dd/MM/yyyy", selector: 'date'});
//dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = dateString
dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = y;
dojo.byId('#{id:calDate}').value = y;
//dojo.byId('#{id:calDate}').value = dateString;
XSP.partialRefreshGet("#{id:dayPanel1}",{});//Post Value to server
}
});
});
This fires when the user clicks the calendar and it updates a field (calDate) and then updates the viewPanel (dayPanel1).
The view is filtered based on a calDate field which clicking on the calendar sets.
Any ideas how I can get the view refreshed when the user clicks on the calendar? Seems basic but it's driving me nuts!!
Graeme
The date format was incorrect (as #MichaelSaiz surmised). Got it sorted now.
XSP.addOnLoad(function(){
dojo.require("dojox.widget.Calendar");
dojo.require("dojo.date","dijit.registry");
dojo.require("dojo.date.locale");
dojo.require("dijit.Calendar");
dojo.ready(function(){
// create the Calendar:
var selectedDate = null;
var calendar_body = new dojox.widget.Calendar({
value: new Date(),
onValueSelected: function(date){calendarDateClicked(date);
}
}, "calendar_body");
//Set month in correct format
function setMonth(month){
switch(month)
{
case 1:
month = "Jan";
break;
case 2:
month = "Feb";
break;
}
return month;
}
//create Click action
function calendarDateClicked(date){
var d = new Date(date);
var month = (d.getMonth() + 1);
month = setMonth(month);
var day = '' + d.getDate() + ",";
var year = d.getFullYear();
//if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
var dateString = [month,day,year].join(" ");
//var y = dojo.date.locale.format(d, {datePattern:"dd/MM/yyyy", selector: 'date'});
dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = dateString
//dojo.byId('#{id:hiddenCalWidgetSelectedDate}').value = y;
//dojo.byId('#{id:calDate}').value = y;
dojo.byId('#{id:calDate}').value = dateString;
XSP.partialRefreshPost("#{id:mainPanel}",{});//Post Value to server
}
});
});