This is my data::
0 = {DailyGraphData@12509} "DailyGraphData(id=122, appName=GoRestApp, packageName=com.anil.gorestapp, totalAppTimeInForeground=162000, lastTimeUsed=2021-12-04 01:07:01, beginningTime=2021-12-04 01:04:19, endTime=2021-12-04 01:07:01, isChild=true, category=other)"
1 = {DailyGraphData@12510} "DailyGraphData(id=123, appName=Pixel Launcher, packageName=com.google.android.apps.nexuslauncher, totalAppTimeInForeground=18000, lastTimeUsed=2021-12-04 01:04:22, beginningTime=2021-12-04 01:04:20, endTime=2021-12-04 01:04:22, isChild=true, category=other)"
2 = {DailyGraphData@12511} "DailyGraphData(id=1, appName=Pixel Launcher, packageName=com.google.android.apps.nexuslauncher, totalAppTimeInForeground=14036000, lastTimeUsed=2021-12-04 02:02:57, beginningTime=2021-12-04 01:00:00, endTime=2021-12-04 02:00:00, isChild=false, category=other)"
3 = {DailyGraphData@12512} "DailyGraphData(id=4, appName=GoRestApp, packageName=com.anil.gorestapp, totalAppTimeInForeground=46406000, lastTimeUsed=2021-12-04 00:52:26, beginningTime=2021-12-04 00:50:55, endTime=2021-12-04 00:52:26, isChild=false, category=other)"
4 = {DailyGraphData@12514} "DailyGraphData(id=19, appName=Package installer, packageName=com.google.android.packageinstaller, totalAppTimeInForeground=18000, lastTimeUsed=2021-12-04 00:54:18, beginningTime=2021-12-04 00:54:17, endTime=2021-12-04 00:54:18, isChild=false, category=other)"
5 = {DailyGraphData@12515} "DailyGraphData(id=215, appName=Certificate Installer, packageName=com.android.certinstaller, totalAppTimeInForeground=14000, lastTimeUsed=2021-12-04 00:36:12, beginningTime=2021-12-04 00:36:05, endTime=2021-12-04 00:36:12, isChild=false, category=other)"
6 = {DailyGraphData@12516} "DailyGraphData(id=299, appName=Usage Stats, packageName=at.ciit.usagestats, totalAppTimeInForeground=7810000, lastTimeUsed=2021-12-04 04:05:59, beginningTime=2021-12-04 04:05:33, endTime=2021-12-04 04:05:59, isChild=false, category=other)" I am saving this data in parentChildResult
val parentChildResult :ArrayList<DailyGraphData>
I want to iterate this list and i want save this data in another child list data in such manner so that if isChild is true then totalAppTimeInForeground should go to childUsageDuration and if !isChild then it should go parentUsageDuration and it should return Single item and should add in Single list
data class Monitor(
var appName: String,
var packageName: String,
var category: String,
var parentUsageDuration: Long? = 0L,
var childUsageDuration: Long? = 0L
)
I am trying in this way
var listOfMonitor: ArrayList<Monitor> = ArrayList()
var parentTime: Long? = 0L
var childTime: Long? = 0L
for (item in value) {
if (item.isChild){
childTime = item.totalAppTimeInForeground
}
else{
parentTime = item.totalAppTimeInForeground
}
listOfMonitor.add(Monitor(appName = item.appName,packageName = item.packageName,category= item.category,parentUsageDuration = parentTime,childUsageDuration = childTime))
}
but i am unable to make this as group by app wise please help me in this
CodePudding user response:
As you've written in your question, you want to group by app
.
Coincidental the Kotlin standard library contains a function called groupBy
which sound quite fitting.
val listOfMonitor: List<Monitor> = parentChildResult
.groupBy { it.appName }
.map { (appName, appGraphData) ->
val sampleGraphData = appGraphData.first()
val (childDataPoints, parentDataPoints) = appGraphData.partition { it.isChild }
Monitor(
appName = appName,
sampleGraphData.packageName,
sampleGraphData.category,
parentDataPoints.sumOf { it.totalAppTimeInForeground },
childDataPoints.sumOf { it.totalAppTimeInForeground },
)
}
val arrayListOfMonitor: ArrayList<Monitor> = ArrayList(listOfMonitor)
The above first creates groups based on the apps name.
For each such group, the list of corresponding DailyGraphData
objects is split in two, based on whether a data point is for a child or not.
Both parentUsageDuration
and childUsageDuration
then is the sumOf
the totalAppTimeInForeground
of the corresponding list items.
I based my answer on the following data.
data class DailyGraphData(
val id: Int,
val appName: String,
val packageName: String,
val totalAppTimeInForeground: Long,
val lastTimeUsed: String,
val beginningTime: String,
val endTime: String,
val isChild: Boolean,
val category: String,
)
val parentChildResult = listOf(
DailyGraphData(
id = 122,
appName = "GoRestApp",
packageName = "com.anil.gorestapp",
totalAppTimeInForeground = 162000,
lastTimeUsed = "2021-12-04 01:07:01",
beginningTime = "2021-12-04 01:04:19",
endTime = "2021-12-04 01:07:01",
isChild = true,
category = "other"
),
DailyGraphData(
id = 123,
appName = "Pixel Launcher",
packageName = "com.google.android.apps.nexuslauncher",
totalAppTimeInForeground = 18000,
lastTimeUsed = "2021-12-04 01:04:22",
beginningTime = "2021-12-04 01:04:20",
endTime = "2021-12-04 01:04:22",
isChild = true,
category = "other"
),
DailyGraphData(
id = 1,
appName = "Pixel Launcher",
packageName = "com.google.android.apps.nexuslauncher",
totalAppTimeInForeground = 14036000,
lastTimeUsed = "2021-12-04 02:02:57",
beginningTime = "2021-12-04 01:00:00",
endTime = "2021-12-04 02:00:00",
isChild = false,
category = "other"
),
DailyGraphData(
id = 4,
appName = "GoRestApp",
packageName = "com.anil.gorestapp",
totalAppTimeInForeground = 46406000,
lastTimeUsed = "2021-12-04 00:52:26",
beginningTime = "2021-12-04 00:50:55",
endTime = "2021-12-04 00:52:26",
isChild = false,
category = "other"
),
DailyGraphData(
id = 19,
appName = "Package installer",
packageName = "com.google.android.packageinstaller",
totalAppTimeInForeground = 18000,
lastTimeUsed = "2021-12-04 00:54:18",
beginningTime = "2021-12-04 00:54:17",
endTime = "2021-12-04 00:54:18",
isChild = false,
category = "other"
),
DailyGraphData(
id = 215,
appName = "Certificate Installer",
packageName = "com.android.certinstaller",
totalAppTimeInForeground = 14000,
lastTimeUsed = "2021-12-04 00:36:12",
beginningTime = "2021-12-04 00:36:05",
endTime = "2021-12-04 00:36:12",
isChild = false,
category = "other"
),
DailyGraphData(
id = 299,
appName = "Usage Stats",
packageName = "at.ciit.usagestats",
totalAppTimeInForeground = 7810000,
lastTimeUsed = "2021-12-04 04:05:59",
beginningTime = "2021-12-04 04:05:33",
endTime = "2021-12-04 04:05:59",
isChild = false,
category = "other"
),
)