Skip to content

Commit

Permalink
Replace com.jakewharton.threetenabp with desugaring.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasschuerg committed Feb 2, 2024
1 parent a68613b commit 800528f
Show file tree
Hide file tree
Showing 14 changed files with 114 additions and 86 deletions.
11 changes: 9 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ android {
targetSdkVersion 34
versionCode 1
versionName "${lib_version}"
multiDexEnabled true
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
coreLibraryDesugaringEnabled true

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}

buildTypes {
Expand All @@ -27,6 +33,7 @@ android {

dependencies {
implementation project(path: ':library')
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.4"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import android.graphics.Color
import de.tobiasschuerg.weekview.data.Event
import de.tobiasschuerg.weekview.data.WeekData
import de.tobiasschuerg.weekview.util.TimeSpan
import org.threeten.bp.DayOfWeek
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalTime
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.LocalTime
import java.util.Random

object EventCreator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,21 @@ import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.jakewharton.threetenabp.AndroidThreeTen
import de.tobiasschuerg.weekview.data.Event
import de.tobiasschuerg.weekview.data.EventConfig
import de.tobiasschuerg.weekview.util.TimeSpan
import de.tobiasschuerg.weekview.view.EventView
import de.tobiasschuerg.weekview.view.WeekView
import org.threeten.bp.Duration
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalTime
import org.threeten.bp.temporal.ChronoUnit
import java.time.Duration
import java.time.LocalDate
import java.time.LocalTime
import java.time.temporal.ChronoUnit

class SampleActivity : AppCompatActivity() {

private val weekView: WeekView by lazy { findViewById(R.id.week_view) }

override fun onCreate(savedInstanceState: Bundle?) {
AndroidThreeTen.init(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sample)

Expand Down Expand Up @@ -62,6 +60,7 @@ class SampleActivity : AppCompatActivity() {
Log.d("Scroll", "1-pointer touch")
v.parent.requestDisallowInterceptTouchEvent(false)
}

2 -> {
Log.d("Zoom", "2-pointer touch")
v.parent.requestDisallowInterceptTouchEvent(true)
Expand Down Expand Up @@ -92,6 +91,7 @@ class SampleActivity : AppCompatActivity() {
Log.i(TAG, "add option clicked")
weekView.addEvent(EventCreator.createRandomEvent())
}

"Clear" -> {
Log.i(TAG, "clear option clicked")
weekView.removeAllEvents()
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
20 changes: 15 additions & 5 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,27 @@ plugins {
}

android {
compileSdk 34

defaultConfig {
minSdkVersion 16
compileSdk 34
targetSdkVersion 34
multiDexEnabled = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
coreLibraryDesugaringEnabled true

sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}

buildFeatures {
buildConfig true
}

buildTypes {
Expand All @@ -28,8 +38,8 @@ android {
}

dependencies {
implementation 'androidx.annotation:annotation:1.7.0'
api 'com.jakewharton.threetenabp:threetenabp:1.4.6'
implementation 'androidx.annotation:annotation:1.7.1'
coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:2.0.4"

testImplementation 'junit:junit:4.13.2'
}
Expand Down
4 changes: 2 additions & 2 deletions library/src/main/java/de/tobiasschuerg/weekview/data/Event.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.tobiasschuerg.weekview.data

import de.tobiasschuerg.weekview.util.TimeSpan
import org.threeten.bp.Duration
import org.threeten.bp.LocalDate
import java.time.Duration
import java.time.LocalDate

sealed class Event {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.tobiasschuerg.weekview.data

import de.tobiasschuerg.weekview.util.TimeSpan
import org.threeten.bp.LocalTime
import java.time.LocalTime

class WeekData {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
package de.tobiasschuerg.weekview.util

import org.threeten.bp.DayOfWeek
import org.threeten.bp.temporal.WeekFields
import java.time.DayOfWeek
import java.time.temporal.WeekFields
import java.util.Locale

/**
* Utility class for working with DayOfWeek.
*/
internal object DayOfWeekUtil {

/**
* Creates a list of all week days with a given start day.
* Creates a list of all week days starting from the provided [firstDay].
*
* @param firstDay The first day of the week.
* @return A list of DayOfWeek starting from the given first day.
*/
fun createList(firstDay: DayOfWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek): List<DayOfWeek> {
return (0..6L).toList().map { firstDay.plus(it) }
}

/**
* Maps a [DayOfWeek] to the corresponding column.
* Considers the first day of the week for the given [Locale],
* as well as if a day is 'enabled' and skips it.
* Maps a [DayOfWeek] to the corresponding column index in a week view.
*
* @param day The DayOfWeek to map.
* @param saturdayEnabled Indicates if Saturday should be enabled.
* @param sundayEnabled Indicates if Sunday should be enabled.
* @return The column index corresponding to the day.
* @throws IllegalStateException if the provided first day of the week is not supported.
* @throws IllegalStateException if Saturday or Sunday is passed but not enabled.
*/
fun mapDayToColumn(day: DayOfWeek, saturdayEnabled: Boolean, sundayEnabled: Boolean): Int {
fun mapDayToColumn(
day: DayOfWeek,
saturdayEnabled: Boolean,
sundayEnabled: Boolean
): Int {
val firstDayOfTheWeek = WeekFields.of(Locale.getDefault()).firstDayOfWeek

if (day == DayOfWeek.SATURDAY && !saturdayEnabled) {
throw java.lang.IllegalStateException("Passed saturday although it is disabled")
}

if (day == DayOfWeek.SUNDAY && !sundayEnabled) {
throw java.lang.IllegalStateException("Passed sunday although it is disabled")
}

when (firstDayOfTheWeek) {
DayOfWeek.MONDAY -> {
// mo: 0, fr:4, su:6
// mo: 0, fr: 4, su: 6
val column = day.value
return if (!saturdayEnabled && day == DayOfWeek.SUNDAY) {
5
Expand All @@ -42,45 +49,53 @@ internal object DayOfWeekUtil {

DayOfWeek.SATURDAY -> {
// sa: 0, su: 1, fr: 6,
if (saturdayEnabled) {
return if (sundayEnabled) {
if (day == DayOfWeek.SATURDAY && !saturdayEnabled) {
throw IllegalStateException("Passed Saturday although it is disabled")
}

if (day == DayOfWeek.SUNDAY && !sundayEnabled) {
throw IllegalStateException("Passed Sunday although it is disabled")
}

return if (saturdayEnabled) {
if (sundayEnabled) {
when (day) {
DayOfWeek.SATURDAY -> 0
DayOfWeek.SUNDAY -> 1
else -> day.value + 1
}
} else {
return when (day) {
when (day) {
DayOfWeek.SATURDAY -> 0
else -> day.value
}
}
} else {
return if (sundayEnabled) {
if (sundayEnabled) {
when (day) {
DayOfWeek.SUNDAY -> 0
else -> day.value
}
} else {
return day.value - 1
day.value - 1
}
}
}

DayOfWeek.SUNDAY -> {
return if (sundayEnabled) {
// su: 0, mo: 1 fr: 5, sa: 6
// su: 0, mo: 1, fr: 5, sa: 6
if (day == DayOfWeek.SUNDAY) {
0
} else {
day.value
}
} else {
// mo: 0 fr: 4, sa: 5, su: -1
// mo: 0, fr: 4, sa: 5, su: -1
day.value - 1
}
}
else -> throw IllegalStateException("$firstDayOfTheWeek das is not supported as start day")
else -> throw IllegalStateException("$firstDayOfTheWeek is not supported as start day")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package de.tobiasschuerg.weekview.util

import org.threeten.bp.LocalTime
import org.threeten.bp.format.DateTimeFormatter
import org.threeten.bp.format.FormatStyle
import java.time.LocalTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle

private val localTimeFormat: DateTimeFormatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.tobiasschuerg.weekview.util

import org.threeten.bp.Duration
import org.threeten.bp.LocalTime
import java.time.Duration
import java.time.LocalTime

/**
* Holds a duration of time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import de.tobiasschuerg.weekview.util.TimeSpan
import de.tobiasschuerg.weekview.util.dipToPixelF
import de.tobiasschuerg.weekview.util.dipToPixelI
import de.tobiasschuerg.weekview.util.toLocalString
import org.threeten.bp.DayOfWeek
import org.threeten.bp.Duration
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalTime
import org.threeten.bp.format.TextStyle
import org.threeten.bp.temporal.ChronoUnit
import java.time.DayOfWeek
import java.time.Duration
import java.time.LocalDate
import java.time.LocalTime
import java.time.format.TextStyle
import java.time.temporal.ChronoUnit
import java.util.Locale
import kotlin.math.roundToInt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import de.tobiasschuerg.weekview.util.Animation
import de.tobiasschuerg.weekview.util.DayOfWeekUtil
import de.tobiasschuerg.weekview.util.TimeSpan
import de.tobiasschuerg.weekview.util.dipToPixelF
import org.threeten.bp.DayOfWeek
import org.threeten.bp.Duration
import org.threeten.bp.LocalDate
import org.threeten.bp.LocalTime
import java.time.DayOfWeek
import java.time.Duration
import java.time.LocalDate
import java.time.LocalTime
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
Expand Down
Loading

0 comments on commit 800528f

Please sign in to comment.