Skip to content

Commit 7b94b95

Browse files
committed
feature: add removeAllViews method on WeekView
1 parent 47caba5 commit 7b94b95

File tree

4 files changed

+35
-32
lines changed

4 files changed

+35
-32
lines changed

app/src/main/java/de/tobiasschuerg/weekview/sample/SampleActivity.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.threeten.bp.temporal.ChronoUnit
2222

2323
class SampleActivity : AppCompatActivity() {
2424

25-
private val weekView: WeekView by lazy { findViewById<WeekView>(R.id.week_view) }
25+
private val weekView: WeekView by lazy { findViewById(R.id.week_view) }
2626

2727
override fun onCreate(savedInstanceState: Bundle?) {
2828
AndroidThreeTen.init(this)
@@ -31,6 +31,7 @@ class SampleActivity : AppCompatActivity() {
3131

3232
val config = EventConfig(showSubtitle = false, showTimeEnd = false)
3333
weekView.eventConfig = config
34+
weekView.setShowNowIndicator(true)
3435

3536
// set up the WeekView with the data
3637
weekView.addEvents(EventCreator.weekData)
@@ -87,23 +88,18 @@ class SampleActivity : AppCompatActivity() {
8788

8889
override fun onOptionsItemSelected(item: MenuItem): Boolean {
8990
when (item.title) {
90-
"Add" -> addRandomItem()
91-
"Clear" -> removeAllEvents()
91+
"Add" -> {
92+
Log.i(TAG, "add option clicked")
93+
weekView.addEvent(EventCreator.createRandomEvent())
94+
}
95+
"Clear" -> {
96+
Log.i(TAG, "clear option clicked")
97+
weekView.removeAllEvents()
98+
}
9299
}
93100
return true
94101
}
95102

96-
private fun removeAllEvents() {
97-
Log.i(TAG, "removeAllEvents()")
98-
weekView.removeViews(1, weekView.childCount - 1)
99-
}
100-
101-
private fun addRandomItem() {
102-
Log.i(TAG, "addRandomItem()")
103-
val newEvent = EventCreator.createRandomEvent()
104-
weekView.addEvent(newEvent)
105-
}
106-
107103
companion object {
108104
private const val TAG = "SampleActivity"
109105
}

library/src/main/java/de/tobiasschuerg/weekview/view/EventView.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import de.tobiasschuerg.weekview.util.toLocalString
2626
class EventView(
2727
context: Context,
2828
val event: Event.Single,
29-
val config: EventConfig,
29+
private val config: EventConfig,
3030
var scalingFactor: Float = 1f
3131

3232
) : View(context) {
@@ -36,7 +36,7 @@ class EventView(
3636

3737
private val textPaint: Paint by lazy { Paint().apply { isAntiAlias = true } }
3838

39-
private val subjectName: String by lazy { if (config.useShortNames) event.shortTitle else event.title }
39+
private val eventName: String by lazy { if (config.useShortNames) event.shortTitle else event.title }
4040

4141
private val textBounds: Rect = Rect()
4242

@@ -83,15 +83,15 @@ class EventView(
8383
}
8484

8585
// title
86-
val maxTextSize = TextHelper.fitText(subjectName, textPaint.textSize * 3, width - (paddingLeft + paddingRight), height / 4)
86+
val maxTextSize = TextHelper.fitText(eventName, textPaint.textSize * 3, width - (paddingLeft + paddingRight), height / 4)
8787
textPaint.textSize = maxTextSize
88-
textPaint.getTextBounds(subjectName, 0, subjectName.length, textBounds)
88+
textPaint.getTextBounds(eventName, 0, eventName.length, textBounds)
8989
var weight = weightStartTime + weightUpperText
9090
if (weight == 0) {
9191
weight++
9292
}
9393
val subjectY = getY(weight, weightTitle, textBounds)
94-
canvas.drawText(subjectName, (width / 2 - textBounds.centerX()).toFloat(), subjectY.toFloat(), textPaint)
94+
canvas.drawText(eventName, (width / 2 - textBounds.centerX()).toFloat(), subjectY.toFloat(), textPaint)
9595

9696
textPaint.textSize = TextHelper.fitText(
9797
"123456", maxTextSize, width / 2,

library/src/main/java/de/tobiasschuerg/weekview/view/WeekBackgroundView.kt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
4343
}
4444
}
4545

46-
private var isInScreenshotMode = false
46+
var showNowIndicator = false
4747

4848
val topOffsetPx: Int = context.dipToPixelI(32f)
4949
private val leftOffset: Int = context.dipToPixelI(48f)
@@ -83,8 +83,8 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
8383
canvas.drawHorizontalDividers()
8484
canvas.drawColumnsWithHeaders()
8585

86-
Log.d(TAG, "Screenshot mode? $isInScreenshotMode")
87-
if (!isInScreenshotMode && !isInEditMode) {
86+
Log.d(TAG, "Show now indicator? $showNowIndicator")
87+
if (showNowIndicator && !isInEditMode) {
8888
drawNowIndicator(canvas)
8989
}
9090
Log.d(TAG, "Drawing background completed.")
@@ -215,10 +215,6 @@ internal class WeekBackgroundView constructor(context: Context) : View(context)
215215
super.onMeasure(widthMeasureSpec, heightMeasureSpec2)
216216
}
217217

218-
fun setScreenshotMode(screenshotMode: Boolean) {
219-
isInScreenshotMode = screenshotMode
220-
}
221-
222218
fun updateTimes(timeSpan: TimeSpan) {
223219
var timesHaveChanged = false
224220
if (timeSpan.start.isBefore(startTime)) {

library/src/main/java/de/tobiasschuerg/weekview/view/WeekView.kt

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
3232
private val backgroundView: WeekBackgroundView
3333
private val overlapsWith = ArrayList<EventView>()
3434

35-
private var isInScreenshotMode = false
3635
private var layoutCount = 0
3736

3837
private var clickListener: ((view: EventView) -> Unit)? = null
@@ -190,9 +189,6 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
190189
Log.v(TAG, "Laying out timetable for the ${++layoutCount} time.")
191190
Log.v(TAG, "l: $l, t: $t, r: $r, b: $b")
192191
super.onLayout(true, l, t, r, b)
193-
if (isInScreenshotMode) {
194-
backgroundView.setScreenshotMode(true)
195-
}
196192

197193
val saturdayEnabled = backgroundView.days.contains(DayOfWeek.SATURDAY)
198194
val sundayEnabled = backgroundView.days.contains(DayOfWeek.SUNDAY)
@@ -259,8 +255,8 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
259255
}
260256
}
261257

262-
fun setScreenshotModeEnabled(enabled: Boolean) {
263-
isInScreenshotMode = enabled
258+
fun setShowNowIndicator(enabled: Boolean) {
259+
backgroundView.showNowIndicator = enabled
264260
}
265261

266262
private fun overlaps(left: EventView, right: EventView): Boolean {
@@ -279,6 +275,21 @@ class WeekView(context: Context, attributeSet: AttributeSet) :
279275
return lessonStartsWithing || lessonEndsWithing || lessonWithin
280276
}
281277

278+
/**
279+
* Removed all [EventView]s.
280+
*/
281+
fun removeAllEvents() = removeViews(1, childCount - 1)
282+
283+
/**
284+
* Removes ALL [View]s - also the background.
285+
* This is probably not what you want.
286+
*/
287+
@Deprecated(
288+
message = "Better use removeAllEvents in order to keep the background!",
289+
replaceWith = ReplaceWith("removeAllEvents()")
290+
)
291+
override fun removeAllViews() = super.removeAllViews()
292+
282293
companion object {
283294
private const val TAG = "WeekView"
284295
}

0 commit comments

Comments
 (0)