1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.deskclock.widget.selector
17 
18 import android.content.Context
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.view.ViewGroup
22 import android.widget.ArrayAdapter
23 import android.widget.TextView
24 
25 import com.android.deskclock.R
26 import com.android.deskclock.data.DataModel
27 import com.android.deskclock.data.Weekdays
28 import com.android.deskclock.provider.Alarm
29 import com.android.deskclock.widget.TextTime
30 
31 import java.util.Calendar
32 
33 class AlarmSelectionAdapter(
34     context: Context,
35     id: Int,
36     alarms: List<AlarmSelection>
37 ) : ArrayAdapter<AlarmSelection?>(context, id, alarms) {
getViewnull38     override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
39         val context = context
40         var row = convertView
41         if (row == null) {
42             val inflater = LayoutInflater.from(context)
43             row = inflater.inflate(R.layout.alarm_row, parent, false)
44         }
45 
46         val selection = getItem(position)
47         val alarm = selection?.alarm
48 
49         val alarmTime = row!!.findViewById<View>(R.id.digital_clock) as TextTime
50         alarmTime.setTime(alarm!!.hour, alarm.minutes)
51 
52         val alarmLabel = row.findViewById<View>(R.id.label) as TextView
53         alarmLabel.text = alarm.label
54 
55         // find days when alarm is firing
56         val daysOfWeek: String
57         daysOfWeek = if (!alarm.daysOfWeek.isRepeating) {
58             if (Alarm.isTomorrow(alarm, Calendar.getInstance())) {
59                 context.resources.getString(R.string.alarm_tomorrow)
60             } else {
61                 context.resources.getString(R.string.alarm_today)
62             }
63         } else {
64             val weekdayOrder: Weekdays.Order = DataModel.dataModel.weekdayOrder
65             alarm.daysOfWeek.toString(context, weekdayOrder)
66         }
67 
68         val daysOfWeekView = row.findViewById<View>(R.id.daysOfWeek) as TextView
69         daysOfWeekView.text = daysOfWeek
70 
71         return row
72     }
73 }