├── screenshot.png
├── tachyon
├── gradle.properties
├── src
│ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── res
│ │ │ └── values
│ │ │ │ └── attrs.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── linkedin
│ │ │ └── android
│ │ │ └── tachyon
│ │ │ ├── DirectionalRect.java
│ │ │ └── DayView.java
│ └── test
│ │ └── java
│ │ └── com
│ │ └── linkedin
│ │ └── android
│ │ └── tachyon
│ │ └── DayViewTest.java
└── build.gradle
├── settings.gradle
├── .gitignore
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── tachyon-sample
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── integers.xml
│ │ │ ├── colors.xml
│ │ │ ├── styles.xml
│ │ │ ├── dimens.xml
│ │ │ └── strings.xml
│ │ └── layout
│ │ │ ├── hour_label.xml
│ │ │ ├── event.xml
│ │ │ ├── scroll_target_dialog.xml
│ │ │ ├── sample_activity.xml
│ │ │ └── edit_event_dialog.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── linkedin
│ │ └── android
│ │ └── tachyon
│ │ └── sample
│ │ └── SampleActivity.java
├── proguard-rules.pro
└── build.gradle
├── CHANGELOG.md
├── .travis.yml
├── gradle.properties
├── NOTICE
├── README.md
├── RELEASING.md
├── CONTRIBUTING.md
├── LICENSE
├── gradlew.bat
└── gradlew
/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linkedin/Tachyon/HEAD/screenshot.png
--------------------------------------------------------------------------------
/tachyon/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=Tachyon
2 | POM_ARTIFACT_ID=tachyon
3 | POM_PACKAGING=aar
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | rootProject.name = "tachyon-root"
2 | include ':tachyon', ':tachyon-sample'
3 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle
2 | local.properties
3 | .idea/
4 | build/
5 | .DS_Store
6 | *.iml
7 | *.ipr
8 | *.iws
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linkedin/Tachyon/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/tachyon/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/values/integers.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 0
4 | 24
5 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Nov 10 11:59:22 PST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip
7 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | Change Log
2 | ==========
3 |
4 | 1.0.2 *(2019-06-13)*
5 | --------------------------
6 | * Support custom start and end hours
7 |
8 | 1.0.1 *(2019-04-14)*
9 | --------------------------
10 | * Adding top and bottom position methods for hours and events
11 |
12 | 1.0.0 *(2019-03-31)*
13 | --------------------------
14 | * Initial release
15 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | #b0b0b0
9 | #e0e0e0
10 |
11 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: android
2 | jdk: oraclejdk8
3 |
4 | # See https://github.com/travis-ci/travis-ci/issues/5582
5 | sudo: required
6 |
7 | android:
8 | components:
9 | # https://github.com/travis-ci/travis-ci/issues/6040#issuecomment-219367943
10 | - tools
11 | - tools
12 |
13 | - platform-tools
14 | - android-28
15 | - build-tools-28.0.3
16 | - extra-android-m2repository
17 | - extra-android-support
18 |
19 | script: './gradlew clean build'
20 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Library info
2 | VERSION_NAME=1.0.2-SNAPSHOT
3 | GROUP_ID=com.linkedin.android.tachyon
4 | LIBRARY_NAME=Tachyon
5 | LIBRARY_DESCRIPTION=Provides a calendar day view on Android.
6 | LIBRARY_GIT_URL=https://github.com/linkedin/Tachyon
7 | LIBRARY_GIT_CONNECTION=git@github.com:linkedin/Tachyon.git
8 | ARTIFACT=tachyon
9 |
10 | # Gradle settings
11 | org.gradle.daemon=true
12 | org.gradle.configureondemand=false
13 | org.gradle.parallel=true
14 |
15 | #For AndroidX migration
16 | android.useAndroidX=true
17 | android.enableJetifier=true
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
13 |
14 |
--------------------------------------------------------------------------------
/NOTICE:
--------------------------------------------------------------------------------
1 | Copyright 2019 LinkedIn Corporation
2 | All Rights Reserved.
3 |
4 | Licensed under the BSD 2-Clause License (the "License"). See License in the
5 | project root for license information.
6 |
7 | Please note, this project may automatically load third party code from external
8 | repositories. If so, such third party code may be subject to other license terms
9 | than as set forth above. In addition, such third party code may also depend on
10 | and load multiple tiers of dependencies. Please review the applicable licenses
11 | of the additional dependencies.
12 |
13 | This product includes/uses Gradle (http://gradle.org/)
14 | Copyright (C) 2019 Gradle Inc.
15 | License: Apache 2.0
16 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/layout/hour_label.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | 1dp
9 | 5dp
10 | 10dp
11 | 1dp
12 | 50dp
13 | 65dp
14 | 52dp
15 | 85dp
16 | 4dp
17 |
--------------------------------------------------------------------------------
/tachyon-sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | *This project is no longer maintained.*
2 |
3 | Tachyon
4 | ========
5 |
6 | An Android library that provides a customizable calendar day view.
7 |
8 | [Click here for the iOS version.](https://github.com/linkedin/Tachyon-iOS)
9 |
10 | 
11 |
12 | Tachyon is designed to provide a familiar visualization of a calendar day. The rendering is done by the `DayView` class, which takes a list of events and displays them using a custom layout algorithm.
13 |
14 | Usage
15 | -----
16 |
17 | To use Tachyon, you can either directly reference the `DayView` class in your layout files/code, or you can subclass `DayView` to customize the experience.
18 |
19 | Sample App
20 | ----------
21 |
22 | The ''tachyon-sample'' app contains an example of using the library.
23 |
24 | Testing
25 | -------
26 |
27 | We use Mockito for our unit tests. You can run them via the `clean test` Gradle tasks.
28 |
--------------------------------------------------------------------------------
/tachyon-sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 |
7 | defaultConfig {
8 | minSdkVersion rootProject.ext.minSdkVersion
9 | targetSdkVersion rootProject.ext.targetSdkVersion
10 | versionCode 1
11 | versionName VERSION_NAME
12 | }
13 |
14 | compileOptions {
15 | sourceCompatibility rootProject.ext.javaVersion
16 | targetCompatibility rootProject.ext.javaVersion
17 | }
18 |
19 | buildTypes {
20 | release {
21 | minifyEnabled false
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
23 | }
24 | }
25 | }
26 |
27 | dependencies {
28 | implementation project(':tachyon')
29 | implementation "androidx.appcompat:appcompat:1.0.2"
30 | }
--------------------------------------------------------------------------------
/tachyon/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/RELEASING.md:
--------------------------------------------------------------------------------
1 | Releasing
2 | ========
3 |
4 | 1. Change the version in `gradle.properties` to a non-SNAPSHOT version.
5 | 2. Update the `CHANGELOG.md` for the impending release.
6 | 3. Update the `README.md` with the new version.
7 | 4. `git commit -am "Prepare for release X.Y.Z."` (where X.Y.Z is the new version)
8 | 5. `git tag -a X.Y.Z -m "Version X.Y.Z"` (where X.Y.Z is the new version)
9 | 6. `./gradlew clean bintrayUpload`
10 | 7. Update the `gradle.properties` to the next SNAPSHOT version.
11 | 8. `git commit -am "Prepare next development version."`
12 | 9. `git push && git push --tags`
13 |
14 |
15 | Prerequisites
16 | -------------
17 |
18 | First, you will need a Bintray account with access to the [linkedin-tachyon](https://bintray.com/linkedin-tachyon) organization.
19 |
20 | Then, set the following environment variables:
21 |
22 | * `BINTRAY_USER` - Bintray username
23 | * `BINTRAY_KEY` - Bintray API key for the given user account
24 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
8 |
9 |
13 |
14 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Contribution Agreement
2 | ======================
3 |
4 | As a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code, you (and, if applicable, your employer) are licensing the submitted code to LinkedIn and the open source community subject to the BSD 2-Clause license.
5 |
6 | Contributing
7 | ============
8 |
9 | If you would like to contribute to Tachyon you can do so through Github by forking the repository and sending a pull request.
10 |
11 | When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible.
12 |
13 | Bugs
14 | ====
15 |
16 | Please [create an issue](https://github.com/linkedin/tachyon/issues/new) and include enough information to reproduce the issue you are seeing. Be sure to include which Android version you're using as well.
17 |
18 | Feature requests
19 | ================
20 |
21 | Please [create an issue](https://github.com/linkedin/tachyon/issues/new) to describe the feature and why you think it would be useful.
22 |
23 | Reporting security issues
24 | =========================
25 |
26 | Please report security issues to [security@linkedin.com](mailto:security@linkedin.com) with a subject line of this format:
27 |
28 | `GitHub linkedin/tachyon - [summary of issue]`
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 2-CLAUSE LICENSE
2 |
3 | Copyright 2019 LinkedIn Corporation
4 | All Rights Reserved.
5 |
6 | Redistribution and use in source and binary forms, with or
7 | without modification, are permitted provided that the following
8 | conditions are met:
9 |
10 | 1. Redistributions of source code must retain the above copyright
11 | notice, this list of conditions and the following disclaimer.
12 |
13 | 2. Redistributions in binary form must reproduce the above
14 | copyright notice, this list of conditions and the following
15 | disclaimer in the documentation and/or other materials provided
16 | with the distribution.
17 |
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | Tachyon Sample
9 | Previous Day
10 | Next Day
11 | Add Event
12 | Edit Event
13 | Scroll To…
14 | Title
15 | Location
16 | Date
17 | Start Time
18 | End Time
19 | Color
20 | Delete
21 | Time…
22 | First Event Top
23 | First Event Bottom
24 | Last Event Top
25 | Last Event Bottom
26 |
27 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/layout/event.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
14 |
15 |
27 |
28 |
40 |
41 |
--------------------------------------------------------------------------------
/tachyon/src/main/java/com/linkedin/android/tachyon/DirectionalRect.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 LinkedIn Corporation
3 | * All Rights Reserved.
4 | *
5 | * Licensed under the BSD 2-Clause License (the "License"). See License in the project root for
6 | * license information.
7 | */
8 | package com.linkedin.android.tachyon;
9 |
10 | /**
11 | * Similar to {@link android.graphics.Rect} but provides the
12 | * {@link #set(boolean, int, int, int, int, int)} method to handle right-to-left mode.
13 | */
14 | public class DirectionalRect {
15 |
16 | private int left;
17 | private int top;
18 | private int right;
19 | private int bottom;
20 |
21 | /**
22 | * Sets the rect's points but factors in whether or not the device is in right-to-left mode.
23 | *
24 | * @param isRtl whether or not the device is in right-to-left mode
25 | * @param parentWidth the width of the parent view of the rect, this will be used to figure out
26 | * how to translate
27 | * the rect in right-to-left mode
28 | * @param start the start of the rect in left-to-right mode
29 | * @param top the top of the rect, it will not be translated
30 | * @param end the end of the rect in left-to-right mode
31 | * @param bottom the bottom of the rect, it will not be translated
32 | */
33 | public void set(boolean isRtl, int parentWidth, int start, int top, int end, int bottom) {
34 | this.left = isRtl ? parentWidth - end : start;
35 | this.top = top;
36 | this.right = isRtl ? parentWidth - start : end;
37 | this.bottom = bottom;
38 | }
39 |
40 | public int getLeft() {
41 | return left;
42 | }
43 |
44 | public int getTop() {
45 | return top;
46 | }
47 |
48 | public int getRight() {
49 | return right;
50 | }
51 |
52 | public int getBottom() {
53 | return bottom;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/layout/scroll_target_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
13 |
14 |
21 |
22 |
29 |
30 |
37 |
38 |
45 |
46 |
53 |
54 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/tachyon/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'maven-publish'
4 | id 'com.jfrog.bintray' version '1.8.4'
5 | }
6 |
7 | group = GROUP_ID
8 |
9 | android {
10 | compileSdkVersion rootProject.ext.compileSdkVersion
11 | buildToolsVersion rootProject.ext.buildToolsVersion
12 |
13 | defaultConfig {
14 | minSdkVersion rootProject.ext.minSdkVersion
15 | targetSdkVersion rootProject.ext.targetSdkVersion
16 | versionCode 1
17 | versionName VERSION_NAME
18 | }
19 |
20 | compileOptions {
21 | sourceCompatibility rootProject.ext.javaVersion
22 | targetCompatibility rootProject.ext.javaVersion
23 | }
24 |
25 | buildTypes {
26 | release {
27 | minifyEnabled false
28 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
29 | }
30 | }
31 | }
32 |
33 | dependencies {
34 | implementation "androidx.fragment:fragment:1.0.0"
35 |
36 | testImplementation 'junit:junit:4.12'
37 | testImplementation 'org.mockito:mockito-core:2.22.0'
38 | }
39 |
40 | version = VERSION_NAME
41 |
42 | task sourcesJar(type: Jar) {
43 | classifier = 'sources'
44 | from android.sourceSets.main.java.srcDirs
45 | }
46 |
47 | task javadoc(type: Javadoc) {
48 | source = android.sourceSets.main.java.srcDirs
49 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
50 | }
51 |
52 | task javadocJar(type: Jar, dependsOn: javadoc) {
53 | classifier = 'javadoc'
54 | from javadoc.destinationDir
55 | }
56 |
57 | publishing {
58 | publications {
59 | aar(MavenPublication) {
60 |
61 | artifact("$buildDir/outputs/aar/${project.getName()}-release.aar") {
62 | builtBy assemble
63 | }
64 | artifact sourcesJar
65 | artifact javadocJar
66 |
67 | pom.withXml {
68 | asNode().children().last() + {
69 | resolveStrategy = Closure.DELEGATE_FIRST
70 |
71 | description = LIBRARY_DESCRIPTION
72 | url LIBRARY_GIT_URL
73 | scm {
74 | url LIBRARY_GIT_URL
75 | connection LIBRARY_GIT_CONNECTION
76 | developerConnection LIBRARY_GIT_CONNECTION
77 | }
78 | licenses {
79 | license {
80 | name '2-Clause BSD License'
81 | url 'https://opensource.org/licenses/BSD-2-Clause'
82 | distribution 'repo'
83 | }
84 | }
85 |
86 | developers {
87 | developer {
88 | id 'com.linkedin'
89 | name 'LinkedIn Corp.'
90 | email ''
91 | }
92 | }
93 |
94 | dependencies {
95 | configurations.implementation.allDependencies.each { ModuleDependency dp ->
96 | dependency {
97 | resolveStrategy = Closure.DELEGATE_ONLY
98 | groupId dp.group
99 | artifactId dp.name
100 | version dp.version
101 | }
102 | }
103 | }
104 | }
105 | }
106 | }
107 | }
108 | }
109 |
110 | bintray {
111 |
112 | user = System.getenv('BINTRAY_USER') ?: ""
113 | key = System.getenv('BINTRAY_KEY') ?: ""
114 |
115 | pkg {
116 | userOrg = 'linkedin-tachyon'
117 | repo = 'maven'
118 | name = 'tachyon'
119 | desc = LIBRARY_DESCRIPTION
120 | labels = ['android']
121 | licenses = ['2-Clause BSD License']
122 |
123 | websiteUrl = LIBRARY_GIT_URL
124 | vcsUrl = LIBRARY_GIT_URL
125 | issueTrackerUrl = 'https://github.com/linkedin/Tachyon/issues'
126 | githubRepo = 'linkedin/tachyon'
127 |
128 | version {
129 | name = VERSION_NAME
130 | released = new Date()
131 | }
132 |
133 | publish = true
134 | }
135 |
136 | publications = ['aar']
137 | }
138 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/layout/sample_activity.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
23 |
24 |
32 |
33 |
41 |
42 |
51 |
52 |
60 |
61 |
62 |
71 |
72 |
76 |
77 |
83 |
84 |
98 |
99 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/res/layout/edit_event_dialog.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
11 |
12 |
18 |
19 |
23 |
24 |
30 |
31 |
35 |
36 |
42 |
43 |
47 |
48 |
54 |
55 |
59 |
60 |
66 |
67 |
71 |
72 |
78 |
79 |
83 |
84 |
90 |
91 |
97 |
98 |
104 |
105 |
111 |
112 |
118 |
119 |
125 |
126 |
127 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn ( ) {
37 | echo "$*"
38 | }
39 |
40 | die ( ) {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/tachyon/src/test/java/com/linkedin/android/tachyon/DayViewTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 LinkedIn Corporation
3 | * All Rights Reserved.
4 | *
5 | * Licensed under the BSD 2-Clause License (the "License"). See License in the project root for
6 | * license information.
7 | */
8 | package com.linkedin.android.tachyon;
9 |
10 | import android.content.Context;
11 | import android.content.res.TypedArray;
12 | import android.util.AttributeSet;
13 | import android.view.View;
14 |
15 | import org.junit.Before;
16 | import org.junit.Test;
17 | import org.mockito.Mock;
18 | import org.mockito.MockitoAnnotations;
19 |
20 | import java.util.ArrayList;
21 | import java.util.Collections;
22 | import java.util.List;
23 |
24 | import static org.hamcrest.CoreMatchers.is;
25 | import static org.hamcrest.CoreMatchers.notNullValue;
26 | import static org.hamcrest.MatcherAssert.assertThat;
27 | import static org.mockito.Mockito.when;
28 |
29 | public class DayViewTest {
30 | private static final int DIVIDER_HEIGHT = 7;
31 | private static final int HALF_HOUR_HEIGHT = 28;
32 | private static final int HOUR_LABEL_MARGIN_END = 17;
33 | private static final int EVENT_MARGIN = 3;
34 | private static final float MINUTE_HEIGHT = HALF_HOUR_HEIGHT / 30f;
35 | private static final int PARENT_WIDTH = 200;
36 |
37 | @Mock
38 | Context context;
39 | @Mock
40 | AttributeSet attrs;
41 | @Mock
42 | TypedArray array;
43 | @Mock
44 | View hourLabelView;
45 | @Mock
46 | View eventView;
47 |
48 | private DayView dayView;
49 |
50 | @Before
51 | public void setup() {
52 | MockitoAnnotations.initMocks(this);
53 |
54 | when(context.obtainStyledAttributes(attrs, R.styleable.DayView)).thenReturn(array);
55 | when(array.getDimensionPixelSize(R.styleable.DayView_dividerHeight, 0)).thenReturn(
56 | DIVIDER_HEIGHT);
57 | when(array.getDimensionPixelSize(R.styleable.DayView_halfHourHeight, 0)).thenReturn(
58 | HALF_HOUR_HEIGHT);
59 | when(array.getDimensionPixelSize(R.styleable.DayView_hourLabelMarginEnd, 0)).thenReturn(
60 | HOUR_LABEL_MARGIN_END);
61 | when(array.getDimensionPixelSize(R.styleable.DayView_eventMargin, 0)).thenReturn(EVENT_MARGIN);
62 | when(array.getInt(R.styleable.DayView_startHour, DayView.MIN_START_HOUR)).thenReturn(DayView.MIN_START_HOUR);
63 | when(array.getInt(R.styleable.DayView_endHour, DayView.MAX_END_HOUR)).thenReturn(DayView.MAX_END_HOUR);
64 |
65 | when(hourLabelView.getMeasuredWidth()).thenReturn(50);
66 | when(hourLabelView.getMeasuredHeight()).thenReturn(20);
67 |
68 | dayView = new DayView(context, attrs, 0, false);
69 |
70 | List hourLabelViews = new ArrayList<>();
71 | for (int i = dayView.getStartHour(); i <= dayView.getEndHour(); i++) {
72 | hourLabelViews.add(hourLabelView);
73 | }
74 |
75 | List eventTimeRanges = new ArrayList<>();
76 | eventTimeRanges.add(new DayView.EventTimeRange(30, 180));
77 | eventTimeRanges.add(new DayView.EventTimeRange(90, 120));
78 | eventTimeRanges.add(new DayView.EventTimeRange(150, 300));
79 | eventTimeRanges.add(new DayView.EventTimeRange(150, 300));
80 |
81 | List eventViews = new ArrayList<>();
82 | for (int i = 0; i < eventTimeRanges.size(); i++) {
83 | eventViews.add(eventView);
84 | }
85 |
86 | List eventRects = new ArrayList<>();
87 | for (int i = 0; i < eventViews.size(); i++) {
88 | eventRects.add(new DirectionalRect());
89 | }
90 |
91 | dayView.hourLabelViews.addAll(hourLabelViews);
92 | dayView.filteredEventViews.addAll(eventViews);
93 | dayView.filteredEventTimeRanges.addAll(eventTimeRanges);
94 | dayView.eventColumnSpansHelper = new DayView.EventColumnSpansHelper(eventTimeRanges);
95 | dayView.eventRects.addAll(eventRects);
96 | dayView.setParentWidth(PARENT_WIDTH);
97 | }
98 |
99 | @Test
100 | public void setHourLabelRects() {
101 | dayView.setHourLabelRects(25, 75, 90);
102 |
103 | assertThat(dayView.hourLabelRects.get(0).getLeft(), is(25));
104 | assertThat(dayView.hourLabelRects.get(0).getTop(), is(80));
105 | assertThat(dayView.hourLabelRects.get(0).getRight(), is(75));
106 | assertThat(dayView.hourLabelRects.get(0).getBottom(), is(100));
107 |
108 | assertThat(dayView.hourLabelRects.get(6).getLeft(), is(25));
109 | assertThat(dayView.hourLabelRects.get(6).getTop(), is(500));
110 | assertThat(dayView.hourLabelRects.get(6).getRight(), is(75));
111 | assertThat(dayView.hourLabelRects.get(6).getBottom(), is(520));
112 |
113 | assertThat(dayView.hourLabelRects.get(13).getLeft(), is(25));
114 | assertThat(dayView.hourLabelRects.get(13).getTop(), is(990));
115 | assertThat(dayView.hourLabelRects.get(13).getRight(), is(75));
116 | assertThat(dayView.hourLabelRects.get(13).getBottom(), is(1010));
117 |
118 | assertThat(dayView.hourLabelRects.get(21).getLeft(), is(25));
119 | assertThat(dayView.hourLabelRects.get(21).getTop(), is(1550));
120 | assertThat(dayView.hourLabelRects.get(21).getRight(), is(75));
121 | assertThat(dayView.hourLabelRects.get(21).getBottom(), is(1570));
122 | }
123 |
124 | @Test
125 | public void setDividerRects() {
126 | dayView.setDividerRects(10, 5, 195);
127 |
128 | assertThat(dayView.hourDividerRects.get(0).getLeft(), is(5));
129 | assertThat(dayView.hourDividerRects.get(0).getTop(), is(10));
130 | assertThat(dayView.hourDividerRects.get(0).getRight(), is(195));
131 | assertThat(dayView.hourDividerRects.get(0).getBottom(), is(17));
132 |
133 | assertThat(dayView.halfHourDividerRects.get(7).getLeft(), is(5));
134 | assertThat(dayView.halfHourDividerRects.get(7).getTop(), is(535));
135 | assertThat(dayView.halfHourDividerRects.get(7).getRight(), is(195));
136 | assertThat(dayView.halfHourDividerRects.get(7).getBottom(), is(542));
137 |
138 | assertThat(dayView.hourDividerRects.get(19).getLeft(), is(5));
139 | assertThat(dayView.hourDividerRects.get(19).getTop(), is(1340));
140 | assertThat(dayView.hourDividerRects.get(19).getRight(), is(195));
141 | assertThat(dayView.hourDividerRects.get(19).getBottom(), is(1347));
142 |
143 | assertThat(dayView.halfHourDividerRects.get(22).getLeft(), is(5));
144 | assertThat(dayView.halfHourDividerRects.get(22).getTop(), is(1585));
145 | assertThat(dayView.halfHourDividerRects.get(22).getRight(), is(195));
146 | assertThat(dayView.halfHourDividerRects.get(22).getBottom(), is(1592));
147 | }
148 |
149 | @Test
150 | public void setEventRects() {
151 | dayView.setEventRects(10, MINUTE_HEIGHT, 5, 195);
152 |
153 | assertThat(dayView.eventRects, notNullValue());
154 |
155 | assertThat(dayView.eventRects.get(0).getLeft(), is(8));
156 | assertThat(dayView.eventRects.get(0).getTop(), is(48));
157 | assertThat(dayView.eventRects.get(0).getRight(), is(65));
158 | assertThat(dayView.eventRects.get(0).getBottom(), is(175));
159 |
160 | assertThat(dayView.eventRects.get(1).getLeft(), is(71));
161 | assertThat(dayView.eventRects.get(1).getTop(), is(104));
162 | assertThat(dayView.eventRects.get(1).getRight(), is(191));
163 | assertThat(dayView.eventRects.get(1).getBottom(), is(119));
164 |
165 | assertThat(dayView.eventRects.get(2).getLeft(), is(71));
166 | assertThat(dayView.eventRects.get(2).getTop(), is(160));
167 | assertThat(dayView.eventRects.get(2).getRight(), is(128));
168 | assertThat(dayView.eventRects.get(2).getBottom(), is(287));
169 |
170 | assertThat(dayView.eventRects.get(3).getLeft(), is(134));
171 | assertThat(dayView.eventRects.get(3).getTop(), is(160));
172 | assertThat(dayView.eventRects.get(3).getRight(), is(191));
173 | assertThat(dayView.eventRects.get(3).getBottom(), is(287));
174 | }
175 |
176 | @Test
177 | public void setRect() {
178 | DirectionalRect rect = new DirectionalRect();
179 | rect.set(false, 20, 1, 2, 3, 4);
180 |
181 | assertThat(rect.getLeft(), is(1));
182 | assertThat(rect.getTop(), is(2));
183 | assertThat(rect.getRight(), is(3));
184 | assertThat(rect.getBottom(), is(4));
185 |
186 | rect.set(true, 20, 1, 2, 3, 4);
187 |
188 | assertThat(rect.getLeft(), is(17));
189 | assertThat(rect.getTop(), is(2));
190 | assertThat(rect.getRight(), is(19));
191 | assertThat(rect.getBottom(), is(4));
192 | }
193 |
194 | @Test
195 | public void timeRanges() {
196 | DayView.EventTimeRange range = new DayView.EventTimeRange(20, 40);
197 |
198 | assertThat(range.conflicts(new DayView.EventTimeRange(5, 15)), is(false));
199 | assertThat(range.conflicts(new DayView.EventTimeRange(50, 90)), is(false));
200 | assertThat(range.conflicts(new DayView.EventTimeRange(5, 20)), is(false));
201 | assertThat(range.conflicts(new DayView.EventTimeRange(40, 90)), is(false));
202 |
203 | assertThat(range.conflicts(new DayView.EventTimeRange(20, 40)), is(true));
204 | assertThat(range.conflicts(new DayView.EventTimeRange(10, 60)), is(true));
205 | assertThat(range.conflicts(new DayView.EventTimeRange(25, 35)), is(true));
206 | assertThat(range.conflicts(new DayView.EventTimeRange(10, 35)), is(true));
207 | assertThat(range.conflicts(new DayView.EventTimeRange(25, 50)), is(true));
208 | }
209 |
210 | @Test
211 | public void singleEventColumnSpan() {
212 | List timeRanges =
213 | Collections.singletonList(new DayView.EventTimeRange(55, 133));
214 |
215 | DayView.EventColumnSpansHelper columnSpansHelper =
216 | new DayView.EventColumnSpansHelper(timeRanges);
217 |
218 | assertThat(columnSpansHelper.columnSpans.get(0).startColumn, is(0));
219 | assertThat(columnSpansHelper.columnSpans.get(0).endColumn, is(1));
220 |
221 | assertThat(columnSpansHelper.columnCount, is(1));
222 | }
223 |
224 | @Test
225 | public void multipleEventColumnSpans() {
226 | List timeRanges = new ArrayList<>();
227 | timeRanges.add(new DayView.EventTimeRange(30, 180));
228 | timeRanges.add(new DayView.EventTimeRange(90, 120));
229 | timeRanges.add(new DayView.EventTimeRange(150, 300));
230 | timeRanges.add(new DayView.EventTimeRange(150, 300));
231 |
232 | DayView.EventColumnSpansHelper columnSpansHelper =
233 | new DayView.EventColumnSpansHelper(timeRanges);
234 |
235 | assertThat(columnSpansHelper.columnSpans.get(0).startColumn, is(0));
236 | assertThat(columnSpansHelper.columnSpans.get(0).endColumn, is(1));
237 |
238 | assertThat(columnSpansHelper.columnSpans.get(1).startColumn, is(1));
239 | assertThat(columnSpansHelper.columnSpans.get(1).endColumn, is(3));
240 |
241 | assertThat(columnSpansHelper.columnSpans.get(2).startColumn, is(1));
242 | assertThat(columnSpansHelper.columnSpans.get(2).endColumn, is(2));
243 |
244 | assertThat(columnSpansHelper.columnSpans.get(3).startColumn, is(2));
245 | assertThat(columnSpansHelper.columnSpans.get(3).endColumn, is(3));
246 |
247 | assertThat(columnSpansHelper.columnCount, is(3));
248 | }
249 | }
250 |
--------------------------------------------------------------------------------
/tachyon-sample/src/main/java/com/linkedin/android/tachyon/sample/SampleActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 LinkedIn Corporation
3 | * All Rights Reserved.
4 | *
5 | * Licensed under the BSD 2-Clause License (the "License"). See License in the project root for
6 | * license information.
7 | */
8 | package com.linkedin.android.tachyon.sample;
9 |
10 | import android.app.DatePickerDialog;
11 | import android.app.TimePickerDialog;
12 | import android.content.DialogInterface;
13 | import android.os.Bundle;
14 | import android.view.View;
15 | import android.view.ViewGroup;
16 | import android.widget.Button;
17 | import android.widget.DatePicker;
18 | import android.widget.RadioButton;
19 | import android.widget.ScrollView;
20 | import android.widget.TextView;
21 | import android.widget.TimePicker;
22 |
23 | import com.linkedin.android.tachyon.DayView;
24 |
25 | import java.text.DateFormat;
26 | import java.util.ArrayList;
27 | import java.util.Arrays;
28 | import java.util.Calendar;
29 | import java.util.Collections;
30 | import java.util.Comparator;
31 | import java.util.List;
32 | import java.util.Locale;
33 |
34 | import androidx.annotation.ColorRes;
35 | import androidx.annotation.NonNull;
36 | import androidx.annotation.Nullable;
37 | import androidx.appcompat.app.AlertDialog;
38 | import androidx.appcompat.app.AppCompatActivity;
39 | import androidx.collection.LongSparseArray;
40 |
41 | /**
42 | * This sample activity demonstrates how to populate the day view with events.
43 | */
44 | public class SampleActivity extends AppCompatActivity {
45 |
46 | /**
47 | * Some examples to demonstrate how the day view renders multiple events that are in close
48 | * proximity to each other.
49 | */
50 | @NonNull
51 | private static final Event[] INITIAL_EVENTS = {
52 | new Event("Walk the dog", "Park", 0, 0, 30, android.R.color.holo_red_dark),
53 | new Event("Meeting", "Office", 1, 30, 90, android.R.color.holo_purple),
54 | new Event("Phone call", "555-5555", 2, 0, 45, android.R.color.holo_orange_dark),
55 | new Event("Lunch", "Cafeteria", 2, 30, 30, android.R.color.holo_green_dark),
56 | new Event("Dinner", "Home", 18, 0, 30, android.R.color.holo_green_dark)};
57 |
58 | private Calendar day;
59 | private LongSparseArray> allEvents;
60 | private DateFormat dateFormat;
61 | private DateFormat timeFormat;
62 | private Calendar editEventDate;
63 | private Calendar editEventStartTime;
64 | private Calendar editEventEndTime;
65 | private Event editEventDraft;
66 |
67 | private ViewGroup content;
68 | private TextView dateTextView;
69 | private ScrollView scrollView;
70 | private DayView dayView;
71 |
72 | @Override
73 | protected void onCreate(Bundle savedInstanceState) {
74 | super.onCreate(savedInstanceState);
75 |
76 | // Create a new calendar object set to the start of today
77 | day = Calendar.getInstance();
78 | day.set(Calendar.HOUR_OF_DAY, 0);
79 | day.set(Calendar.MINUTE, 0);
80 | day.set(Calendar.SECOND, 0);
81 | day.set(Calendar.MILLISECOND, 0);
82 |
83 | // Populate today's entry in the map with a list of example events
84 | allEvents = new LongSparseArray<>();
85 | allEvents.put(day.getTimeInMillis(), new ArrayList<>(Arrays.asList(INITIAL_EVENTS)));
86 |
87 | dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault());
88 | timeFormat = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
89 |
90 | setContentView(R.layout.sample_activity);
91 |
92 | content = findViewById(R.id.sample_content);
93 | dateTextView = findViewById(R.id.sample_date);
94 | scrollView = findViewById(R.id.sample_scroll);
95 | dayView = findViewById(R.id.sample_day);
96 |
97 | // Inflate a label view for each hour the day view will display
98 | Calendar hour = (Calendar) day.clone();
99 | List hourLabelViews = new ArrayList<>();
100 | for (int i = dayView.getStartHour(); i <= dayView.getEndHour(); i++) {
101 | hour.set(Calendar.HOUR_OF_DAY, i);
102 |
103 | TextView hourLabelView = (TextView) getLayoutInflater().inflate(R.layout.hour_label, dayView, false);
104 | hourLabelView.setText(timeFormat.format(hour.getTime()));
105 | hourLabelViews.add(hourLabelView);
106 | }
107 | dayView.setHourLabelViews(hourLabelViews);
108 |
109 | onDayChange();
110 | }
111 |
112 | public void onPreviousClick(View v) {
113 | day.add(Calendar.DAY_OF_YEAR, -1);
114 | onDayChange();
115 | }
116 |
117 | public void onNextClick(View v) {
118 | day.add(Calendar.DAY_OF_YEAR, 1);
119 | onDayChange();
120 | }
121 |
122 | public void onAddEventClick(View v) {
123 | editEventDate = (Calendar) day.clone();
124 |
125 | editEventStartTime = (Calendar) day.clone();
126 |
127 | editEventEndTime = (Calendar) day.clone();
128 | editEventEndTime.add(Calendar.MINUTE, 30);
129 |
130 | showEditEventDialog(false, null, null, android.R.color.holo_red_dark);
131 | }
132 |
133 | public void onScrollClick(View v) {
134 | showScrollTargetDialog();
135 | }
136 |
137 | private void onDayChange() {
138 | dateTextView.setText(dateFormat.format(day.getTime()));
139 | onEventsChange();
140 | }
141 |
142 | private void onEventsChange() {
143 | // The day view needs a list of event views and a corresponding list of event time ranges
144 | List eventViews = null;
145 | List eventTimeRanges = null;
146 | List events = allEvents.get(day.getTimeInMillis());
147 |
148 | if (events != null) {
149 | // Sort the events by start time so the layout happens in correct order
150 | Collections.sort(events, new Comparator() {
151 | @Override
152 | public int compare(Event o1, Event o2) {
153 | return o1.hour < o2.hour ? -1 : (o1.hour == o2.hour ? (o1.minute < o2.minute ? -1 : (o1.minute == o2.minute ? 0 : 1)) : 1);
154 | }
155 | });
156 |
157 | eventViews = new ArrayList<>();
158 | eventTimeRanges = new ArrayList<>();
159 |
160 | // Reclaim all of the existing event views so we can reuse them if needed, this process
161 | // can be useful if your day view is hosted in a recycler view for example
162 | List recycled = dayView.removeEventViews();
163 | int remaining = recycled != null ? recycled.size() : 0;
164 |
165 | for (final Event event : events) {
166 | // Try to recycle an existing event view if there are enough left, otherwise inflate
167 | // a new one
168 | View eventView = remaining > 0 ? recycled.get(--remaining) : getLayoutInflater().inflate(R.layout.event, dayView, false);
169 |
170 | ((TextView) eventView.findViewById(R.id.event_title)).setText(event.title);
171 | ((TextView) eventView.findViewById(R.id.event_location)).setText(event.location);
172 | eventView.setBackgroundColor(getResources().getColor(event.color));
173 |
174 | // When an event is clicked, start a new draft event and show the edit event dialog
175 | eventView.setOnClickListener(new View.OnClickListener() {
176 | @Override
177 | public void onClick(View v) {
178 | editEventDraft = event;
179 |
180 | editEventDate = (Calendar) day.clone();
181 |
182 | editEventStartTime = Calendar.getInstance();
183 | editEventStartTime.set(Calendar.HOUR_OF_DAY, editEventDraft.hour);
184 | editEventStartTime.set(Calendar.MINUTE, editEventDraft.minute);
185 | editEventStartTime.set(Calendar.SECOND, 0);
186 | editEventStartTime.set(Calendar.MILLISECOND, 0);
187 |
188 | editEventEndTime = (Calendar) editEventStartTime.clone();
189 | editEventEndTime.add(Calendar.MINUTE, editEventDraft.duration);
190 |
191 | showEditEventDialog(true, editEventDraft.title, editEventDraft.location, editEventDraft.color);
192 | }
193 | });
194 |
195 | eventViews.add(eventView);
196 |
197 | // The day view needs the event time ranges in the start minute/end minute format,
198 | // so calculate those here
199 | int startMinute = 60 * event.hour + event.minute;
200 | int endMinute = startMinute + event.duration;
201 | eventTimeRanges.add(new DayView.EventTimeRange(startMinute, endMinute));
202 | }
203 | }
204 |
205 | // Update the day view with the new events
206 | dayView.setEventViews(eventViews, eventTimeRanges);
207 | }
208 |
209 | private void showEditEventDialog(boolean eventExists, @Nullable String eventTitle, @Nullable String eventLocation, @ColorRes int eventColor) {
210 | View view = getLayoutInflater().inflate(R.layout.edit_event_dialog, content, false);
211 | final TextView titleTextView = view.findViewById(R.id.edit_event_title);
212 | final TextView locationTextView = view.findViewById(R.id.edit_event_location);
213 | final Button dateButton = view.findViewById(R.id.edit_event_date);
214 | final Button startTimeButton = view.findViewById(R.id.edit_event_start_time);
215 | final Button endTimeButton = view.findViewById(R.id.edit_event_end_time);
216 | final RadioButton redRadioButton = view.findViewById(R.id.edit_event_red);
217 | final RadioButton blueRadioButton = view.findViewById(R.id.edit_event_blue);
218 | final RadioButton orangeRadioButton = view.findViewById(R.id.edit_event_orange);
219 | final RadioButton greenRadioButton = view.findViewById(R.id.edit_event_green);
220 | final RadioButton purpleRadioButton = view.findViewById(R.id.edit_event_purple);
221 |
222 | titleTextView.setText(eventTitle);
223 | locationTextView.setText(eventLocation);
224 |
225 | dateButton.setText(dateFormat.format(editEventDate.getTime()));
226 | dateButton.setOnClickListener(new View.OnClickListener() {
227 | @Override
228 | public void onClick(View v) {
229 | DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
230 | @Override
231 | public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
232 | editEventDate.set(Calendar.YEAR, year);
233 | editEventDate.set(Calendar.MONTH, month);
234 | editEventDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
235 |
236 | dateButton.setText(dateFormat.format(editEventDate.getTime()));
237 | }
238 | };
239 |
240 | new DatePickerDialog(SampleActivity.this, listener, day.get(Calendar.YEAR), day.get(Calendar.MONTH), day.get(Calendar.DAY_OF_MONTH)).show();
241 |
242 | }
243 | });
244 |
245 | startTimeButton.setText(timeFormat.format(editEventStartTime.getTime()));
246 | startTimeButton.setOnClickListener(new View.OnClickListener() {
247 | @Override
248 | public void onClick(View v) {
249 | TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
250 | @Override
251 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
252 | editEventStartTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
253 | editEventStartTime.set(Calendar.MINUTE, minute);
254 |
255 | startTimeButton.setText(timeFormat.format(editEventStartTime.getTime()));
256 |
257 | if (!editEventEndTime.after(editEventStartTime)) {
258 | editEventEndTime = (Calendar) editEventStartTime.clone();
259 | editEventEndTime.add(Calendar.MINUTE, 30);
260 |
261 | endTimeButton.setText(timeFormat.format(editEventEndTime.getTime()));
262 | }
263 | }
264 | };
265 |
266 | new TimePickerDialog(SampleActivity.this, listener, editEventStartTime.get(Calendar.HOUR_OF_DAY), editEventStartTime.get(Calendar.MINUTE), android.text.format.DateFormat.is24HourFormat(SampleActivity.this)).show();
267 |
268 | }
269 | });
270 |
271 | endTimeButton.setText(timeFormat.format(editEventEndTime.getTime()));
272 | endTimeButton.setOnClickListener(new View.OnClickListener() {
273 | @Override
274 | public void onClick(View v) {
275 | TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
276 | @Override
277 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
278 | editEventEndTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
279 | editEventEndTime.set(Calendar.MINUTE, minute);
280 |
281 | if (!editEventEndTime.after(editEventStartTime)) {
282 | editEventEndTime = (Calendar) editEventStartTime.clone();
283 | editEventEndTime.add(Calendar.MINUTE, 30);
284 | }
285 |
286 | endTimeButton.setText(timeFormat.format(editEventEndTime.getTime()));
287 | }
288 | };
289 |
290 | new TimePickerDialog(SampleActivity.this, listener, editEventEndTime.get(Calendar.HOUR_OF_DAY), editEventEndTime.get(Calendar.MINUTE), android.text.format.DateFormat.is24HourFormat(SampleActivity.this)).show();
291 |
292 | }
293 | });
294 |
295 | if (eventColor == android.R.color.holo_blue_dark) {
296 | blueRadioButton.setChecked(true);
297 | } else if (eventColor == android.R.color.holo_orange_dark) {
298 | orangeRadioButton.setChecked(true);
299 | } else if (eventColor == android.R.color.holo_green_dark) {
300 | greenRadioButton.setChecked(true);
301 | } else if (eventColor == android.R.color.holo_purple) {
302 | purpleRadioButton.setChecked(true);
303 | } else {
304 | redRadioButton.setChecked(true);
305 | }
306 |
307 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
308 |
309 | // If the event already exists, we are editing it, otherwise we are adding a new event
310 | builder.setTitle(eventExists ? R.string.edit_event : R.string.add_event);
311 |
312 | // When the event changes are confirmed, read the new values from the dialog and then add
313 | // this event to the list
314 | builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
315 | @Override
316 | public void onClick(DialogInterface dialog, int which) {
317 | List events = allEvents.get(editEventDate.getTimeInMillis());
318 | if (events == null) {
319 | events = new ArrayList<>();
320 | allEvents.put(editEventDate.getTimeInMillis(), events);
321 | }
322 |
323 | String title = titleTextView.getText().toString();
324 | String location = locationTextView.getText().toString();
325 | int hour = editEventStartTime.get(Calendar.HOUR_OF_DAY);
326 | int minute = editEventStartTime.get(Calendar.MINUTE);
327 | int duration = (int) (editEventEndTime.getTimeInMillis() - editEventStartTime.getTimeInMillis()) / 60000;
328 |
329 | @ColorRes int color;
330 | if (blueRadioButton.isChecked()) {
331 | color = android.R.color.holo_blue_dark;
332 | } else if (orangeRadioButton.isChecked()) {
333 | color = android.R.color.holo_orange_dark;
334 | } else if (greenRadioButton.isChecked()) {
335 | color = android.R.color.holo_green_dark;
336 | } else if (purpleRadioButton.isChecked()) {
337 | color = android.R.color.holo_purple;
338 | } else {
339 | color = android.R.color.holo_red_dark;
340 | }
341 |
342 | events.add(new Event(title, location, hour, minute, duration, color));
343 |
344 | onEditEventDismiss(true);
345 | }
346 | });
347 |
348 | builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
349 | @Override
350 | public void onClick(DialogInterface dialog, int which) {
351 | onEditEventDismiss(false);
352 | }
353 | });
354 |
355 | // If the event already exists, provide a delete option
356 | if (eventExists) {
357 | builder.setNeutralButton(R.string.edit_event_delete, new DialogInterface.OnClickListener() {
358 | @Override
359 | public void onClick(DialogInterface dialog, int which) {
360 | onEditEventDismiss(true);
361 | }
362 | });
363 | }
364 |
365 | builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
366 | @Override
367 | public void onCancel(DialogInterface dialog) {
368 | onEditEventDismiss(false);
369 | }
370 | });
371 | builder.setView(view);
372 | builder.show();
373 | }
374 |
375 | private void showScrollTargetDialog() {
376 | View view = getLayoutInflater().inflate(R.layout.scroll_target_dialog, content, false);
377 | final Button timeButton = view.findViewById(R.id.scroll_target_time);
378 | final Button firstEventTopButton = view.findViewById(R.id.scroll_target_first_event_top);
379 | final Button firstEventBottomButton = view.findViewById(R.id.scroll_target_first_event_bottom);
380 | final Button lastEventTopButton = view.findViewById(R.id.scroll_target_last_event_top);
381 | final Button lastEventBottomButton = view.findViewById(R.id.scroll_target_last_event_bottom);
382 |
383 | AlertDialog.Builder builder = new AlertDialog.Builder(this);
384 | builder.setTitle(R.string.scroll_to);
385 | builder.setNegativeButton(android.R.string.cancel, null);
386 | builder.setView(view);
387 |
388 | final AlertDialog dialog = builder.show();
389 |
390 | timeButton.setOnClickListener(new View.OnClickListener() {
391 | @Override
392 | public void onClick(View v) {
393 | TimePickerDialog.OnTimeSetListener listener = new TimePickerDialog.OnTimeSetListener() {
394 | @Override
395 | public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
396 | int top = dayView.getHourTop(hourOfDay);
397 | int bottom = dayView.getHourBottom(hourOfDay);
398 | int y = top + (bottom - top) * minute / 60;
399 | scrollView.smoothScrollTo(0, y);
400 | dialog.dismiss();
401 | }
402 | };
403 |
404 | new TimePickerDialog(SampleActivity.this, listener, 0, 0, android.text.format.DateFormat.is24HourFormat(SampleActivity.this)).show();
405 |
406 | }
407 | });
408 |
409 | firstEventTopButton.setOnClickListener(new View.OnClickListener() {
410 | @Override
411 | public void onClick(View v) {
412 | scrollView.smoothScrollTo(0, dayView.getFirstEventTop());
413 | dialog.dismiss();
414 | }
415 | });
416 |
417 | firstEventBottomButton.setOnClickListener(new View.OnClickListener() {
418 | @Override
419 | public void onClick(View v) {
420 | scrollView.smoothScrollTo(0, dayView.getFirstEventBottom());
421 | dialog.dismiss();
422 | }
423 | });
424 |
425 | lastEventTopButton.setOnClickListener(new View.OnClickListener() {
426 | @Override
427 | public void onClick(View v) {
428 | scrollView.smoothScrollTo(0, dayView.getLastEventTop());
429 | dialog.dismiss();
430 | }
431 | });
432 |
433 | lastEventBottomButton.setOnClickListener(new View.OnClickListener() {
434 | @Override
435 | public void onClick(View v) {
436 | scrollView.smoothScrollTo(0, dayView.getLastEventBottom());
437 | dialog.dismiss();
438 |
439 | }
440 | });
441 | }
442 |
443 | private void onEditEventDismiss(boolean modified) {
444 | if (modified && editEventDraft != null) {
445 | List events = allEvents.get(day.getTimeInMillis());
446 | if (events != null) {
447 | events.remove(editEventDraft);
448 | }
449 | }
450 | editEventDraft = null;
451 |
452 | onEventsChange();
453 | }
454 |
455 | /**
456 | * A data class used to represent an event on the calendar.
457 | */
458 | private static class Event {
459 | @Nullable
460 | private final String title;
461 | @Nullable
462 | private final String location;
463 | private final int hour;
464 | private final int minute;
465 | private final int duration;
466 | @ColorRes
467 | private final int color;
468 |
469 | private Event(@Nullable String title, @Nullable String location, int hour, int minute, int duration, @ColorRes int color) {
470 | this.title = title;
471 | this.location = location;
472 | this.hour = hour;
473 | this.minute = minute;
474 | this.duration = duration;
475 | this.color = color;
476 | }
477 | }
478 | }
479 |
--------------------------------------------------------------------------------
/tachyon/src/main/java/com/linkedin/android/tachyon/DayView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2019 LinkedIn Corporation
3 | * All Rights Reserved.
4 | *
5 | * Licensed under the BSD 2-Clause License (the "License"). See License in the project root for
6 | * license information.
7 | */
8 | package com.linkedin.android.tachyon;
9 |
10 | import android.content.Context;
11 | import android.content.res.TypedArray;
12 | import android.graphics.Canvas;
13 | import android.graphics.Paint;
14 | import android.util.AttributeSet;
15 | import android.view.View;
16 | import android.view.ViewGroup;
17 |
18 | import java.util.ArrayList;
19 | import java.util.List;
20 |
21 | import androidx.annotation.CallSuper;
22 | import androidx.annotation.NonNull;
23 | import androidx.annotation.Nullable;
24 | import androidx.annotation.VisibleForTesting;
25 | import androidx.core.view.ViewCompat;
26 |
27 | public class DayView extends ViewGroup {
28 |
29 | /**
30 | * Because of daylight saving time, some days are shorter or longer than 24 hours. Most calendar
31 | * apps assume there are 24 hours in each day, and then to handle events that span a daylight
32 | * saving time switch those events are adjusted. For example, when daylight saving time begins,
33 | * an event from 1:00 AM to 3:00 AM would only last an hour since the switch happens at 2:00 AM.
34 | * This means for events that span the beginning of daylight saving time, they will be drawn
35 | * with an extra hour. For events that span the end of daylight saving time, they'll be drawn at
36 | * the minimum height for an event if the event's duration is roughly an hour or less.
37 | */
38 | @VisibleForTesting
39 | static final int MIN_START_HOUR = 0;
40 | @VisibleForTesting
41 | static final int MAX_END_HOUR = 24;
42 |
43 | private static final int MINUTES_PER_HOUR = 60;
44 | private static final int MIN_DURATION_MINUTES = 15;
45 |
46 | @NonNull
47 | @VisibleForTesting
48 | final List hourLabelRects;
49 | @NonNull
50 | @VisibleForTesting
51 | final List hourDividerRects;
52 | @NonNull
53 | @VisibleForTesting
54 | final List halfHourDividerRects;
55 | @NonNull
56 | @VisibleForTesting
57 | final List hourLabelViews;
58 | @NonNull
59 | @VisibleForTesting
60 | final List eventViews;
61 | @NonNull
62 | @VisibleForTesting
63 | final List filteredEventViews;
64 | @NonNull
65 | @VisibleForTesting
66 | final List filteredEventTimeRanges;
67 | @NonNull
68 | @VisibleForTesting
69 | final List eventRects;
70 |
71 | @Nullable
72 | @VisibleForTesting
73 | EventColumnSpansHelper eventColumnSpansHelper;
74 |
75 | private final int startHour;
76 | private final int startMinute;
77 | private final int endHour;
78 | private final int endMinute;
79 | private final int minuteCount;
80 | private final int hourLabelsCount;
81 | private final int hourDividersCount;
82 | private final int halfHourDividersCount;
83 |
84 | @NonNull
85 | private final Paint hourDividerPaint;
86 | @NonNull
87 | private final Paint halfHourDividerPaint;
88 | private final int dividerHeight;
89 |
90 | private final int usableHalfHourHeight;
91 | private final int hourLabelWidth;
92 | private final int hourLabelMarginEnd;
93 | private final int eventMargin;
94 |
95 | private boolean isRtl;
96 | private int parentWidth;
97 | private float minuteHeight;
98 |
99 | public DayView(@NonNull Context context) {
100 | this(context, null);
101 | }
102 |
103 | public DayView(@NonNull Context context, @Nullable AttributeSet attrs) {
104 | this(context, attrs, 0);
105 | }
106 |
107 | public DayView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
108 | this(context, attrs, defStyleAttr, true);
109 | }
110 |
111 | @VisibleForTesting
112 | DayView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, boolean enableDrawing) {
113 | super(context, attrs, defStyleAttr);
114 |
115 | TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.DayView);
116 |
117 | // The total number of usable minutes in this day
118 | startHour = Math.max(array.getInt(R.styleable.DayView_startHour, MIN_START_HOUR), MIN_START_HOUR);
119 | startMinute = startHour * MINUTES_PER_HOUR;
120 | endHour = Math.min(array.getInt(R.styleable.DayView_endHour, MAX_END_HOUR), MAX_END_HOUR);
121 | endMinute = endHour * MINUTES_PER_HOUR;
122 | int hourCount = endHour - startHour;
123 | minuteCount = hourCount * MINUTES_PER_HOUR;
124 |
125 | // The hour labels and dividers count here is one more than the hours count so we can
126 | // include the start of the midnight hour of the next day, setHourLabelViews() expects
127 | // exactly this many labels
128 | hourLabelsCount = hourCount + 1;
129 | hourDividersCount = hourCount + 1;
130 | halfHourDividersCount = hourCount;
131 |
132 | hourDividerRects = new ArrayList<>(hourDividersCount);
133 | for (int i = 0; i < hourDividersCount; i++) {
134 | hourDividerRects.add(new DirectionalRect());
135 | }
136 |
137 | halfHourDividerRects = new ArrayList<>(halfHourDividersCount);
138 | for (int i = 0; i < halfHourDividersCount; i++) {
139 | halfHourDividerRects.add(new DirectionalRect());
140 | }
141 |
142 | hourLabelRects = new ArrayList<>(hourLabelsCount);
143 | for (int i = 0; i < hourLabelsCount; i++) {
144 | hourLabelRects.add(new DirectionalRect());
145 | }
146 |
147 | hourLabelViews = new ArrayList<>();
148 | eventViews = new ArrayList<>();
149 | filteredEventViews = new ArrayList<>();
150 | filteredEventTimeRanges = new ArrayList<>();
151 | eventRects = new ArrayList<>();
152 |
153 | dividerHeight = array.getDimensionPixelSize(R.styleable.DayView_dividerHeight, 0);
154 | usableHalfHourHeight =
155 | dividerHeight + array.getDimensionPixelSize(R.styleable.DayView_halfHourHeight, 0);
156 |
157 | hourDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
158 | halfHourDividerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
159 |
160 | // This view draws its hour and half hour dividers directly
161 | if (enableDrawing) {
162 | setWillNotDraw(false);
163 | hourDividerPaint.setColor(array.getColor(R.styleable.DayView_hourDividerColor, 0));
164 | halfHourDividerPaint.setColor(array.getColor(R.styleable.DayView_halfHourDividerColor, 0));
165 | }
166 |
167 | hourLabelWidth = array.getDimensionPixelSize(R.styleable.DayView_hourLabelWidth, 0);
168 | hourLabelMarginEnd = array.getDimensionPixelSize(R.styleable.DayView_hourLabelMarginEnd, 0);
169 | eventMargin = array.getDimensionPixelSize(R.styleable.DayView_eventMargin, 0);
170 | array.recycle();
171 | }
172 |
173 | /**
174 | * @param hourLabelViews the list of views to show as labels for each hour, this list must not
175 | * be null and its length must be {@link #hourLabelsCount}
176 | */
177 | public void setHourLabelViews(@NonNull List hourLabelViews) {
178 | for (View view : this.hourLabelViews) {
179 | removeView(view);
180 | }
181 |
182 | this.hourLabelViews.clear();
183 | this.hourLabelViews.addAll(hourLabelViews);
184 |
185 | for (View view : this.hourLabelViews) {
186 | addView(view);
187 | }
188 | }
189 |
190 | /**
191 | * @param eventViews the list of event views to display
192 | * @param eventTimeRanges the list of event params that describe each event view's start/end
193 | * times, this list must be equal in length to the list of event views,
194 | * or both should be null
195 | */
196 | public void setEventViews(@Nullable List eventViews,
197 | @Nullable List eventTimeRanges) {
198 | for (View view : this.filteredEventViews) {
199 | removeView(view);
200 | }
201 |
202 | this.eventViews.clear();
203 | this.filteredEventViews.clear();
204 | this.filteredEventTimeRanges.clear();
205 | eventRects.clear();
206 | eventColumnSpansHelper = null;
207 |
208 | if (eventViews != null && eventTimeRanges != null) {
209 | this.eventViews.addAll(eventViews);
210 | for (int i = 0; i < eventTimeRanges.size(); i++) {
211 | EventTimeRange eventTimeRange = eventTimeRanges.get(i);
212 | if (eventTimeRange.endMinute > startMinute && eventTimeRange.startMinute < endMinute) {
213 | this.filteredEventViews.add(this.eventViews.get(i));
214 | this.filteredEventTimeRanges.add(eventTimeRange);
215 | }
216 | }
217 | }
218 |
219 | if (!this.filteredEventViews.isEmpty() && !this.filteredEventTimeRanges.isEmpty()) {
220 | eventColumnSpansHelper = new EventColumnSpansHelper(this.filteredEventTimeRanges);
221 | for (View view : this.filteredEventViews) {
222 | addView(view);
223 | eventRects.add(new DirectionalRect());
224 | }
225 | }
226 | }
227 |
228 | /**
229 | * Removes all of the existing event views.
230 | *
231 | * @return the event views that have been removed, they are safe to recycle and reuse at this
232 | * point
233 | */
234 | @Nullable
235 | public List removeEventViews() {
236 | List eventViews = this.eventViews;
237 | setEventViews(null, null);
238 |
239 | return eventViews;
240 | }
241 |
242 | public int getStartHour() {
243 | return startHour;
244 | }
245 |
246 | public int getEndHour() {
247 | return endHour;
248 | }
249 |
250 | /**
251 | * Useful if this view is hosted in a scroll view, the y coordinate returned can be used to
252 | * scroll to the top of the given hour.
253 | *
254 | * @param hour the hour of the day, should be between 0 (12:00 AM of the current day) and 24
255 | * (12:00 AM of the next day)
256 | * @return the vertical offset of the top of the given hour in pixels
257 | */
258 | public int getHourTop(int hour) {
259 | if (hour < 0 || hour >= hourLabelsCount) {
260 | throw new IllegalStateException("Hour must be between 0 and " + hourLabelsCount);
261 | }
262 |
263 | return hourDividerRects.get(hour).getBottom();
264 | }
265 |
266 | /**
267 | * Useful if this view is hosted in a scroll view, the y coordinate returned can be used to
268 | * scroll to the bottom of the given hour.
269 | *
270 | * @param hour the hour of the day, should be between 0 (12:00 AM of the current day) and 24
271 | * (12:00 AM of the next day)
272 | * @return the vertical offset of the bottom of the given hour in pixels
273 | */
274 | public int getHourBottom(int hour) {
275 | if (hour < 0 || hour >= hourLabelsCount) {
276 | throw new IllegalStateException("Hour must be between 0 and " + hourLabelsCount);
277 | }
278 |
279 | if (hour == hourLabelsCount - 1) {
280 | return hourDividerRects.get(hour).getBottom();
281 | }
282 |
283 | return hourDividerRects.get(hour + 1).getTop();
284 | }
285 |
286 | /**
287 | * Useful if this view is hosted in a scroll view, the y coordinate returned can be used to
288 | * scroll to the top of the first event.
289 | *
290 | * @return the vertical offset of the top of the first event in pixels, or zero if there are no
291 | * events
292 | */
293 | public int getFirstEventTop() {
294 | return !eventRects.isEmpty() ? eventRects.get(0).getTop() : 0;
295 | }
296 |
297 | /**
298 | * Useful if this view is hosted in a scroll view, the y coordinate returned can be used to
299 | * scroll to the bottom of the first event.
300 | *
301 | * @return the vertical offset of the bottom of the first event in pixels, or zero if there are
302 | * no events
303 | */
304 | public int getFirstEventBottom() {
305 | return !eventRects.isEmpty() ? eventRects.get(0).getBottom() : 0;
306 | }
307 |
308 | /**
309 | * Useful if this view is hosted in a scroll view, the y coordinate returned can be used to
310 | * scroll to the top of the last event.
311 | *
312 | * @return the vertical offset of the top of the last event in pixels, or zero if there are no
313 | * events
314 | */
315 | public int getLastEventTop() {
316 | return !eventRects.isEmpty() ? eventRects.get(eventRects.size() - 1).getTop() : 0;
317 | }
318 |
319 | /**
320 | * Useful if this view is hosted in a scroll view, the y coordinate returned can be used to
321 | * scroll to the bottom of the last event.
322 | *
323 | * @return the vertical offset of the bottom of the last event in pixels, or zero if there are
324 | * no events
325 | */
326 | public int getLastEventBottom() {
327 | return !eventRects.isEmpty() ? eventRects.get(eventRects.size() - 1).getBottom() : 0;
328 | }
329 |
330 | @Override
331 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
332 | for (int i = 0; i < hourLabelViews.size(); i++) {
333 | View view = hourLabelViews.get(i);
334 | DirectionalRect rect = hourLabelRects.get(i);
335 | view.layout(rect.getLeft(), rect.getTop(), rect.getRight(), rect.getBottom());
336 | }
337 |
338 | for (int i = 0; i < filteredEventViews.size(); i++) {
339 | View view = filteredEventViews.get(i);
340 | DirectionalRect rect = eventRects.get(i);
341 | view.layout(rect.getLeft(), rect.getTop(), rect.getRight(), rect.getBottom());
342 | }
343 | }
344 |
345 | @Override
346 | public boolean shouldDelayChildPressedState() {
347 | return false;
348 | }
349 |
350 | @Override
351 | protected void onDraw(@NonNull Canvas canvas) {
352 | super.onDraw(canvas);
353 |
354 | // Draw the hour and half-hour divider lines directly onto the canvas
355 | for (DirectionalRect rect : hourDividerRects) {
356 | canvas.drawRect(rect.getLeft(),
357 | rect.getTop(),
358 | rect.getRight(),
359 | rect.getBottom(),
360 | hourDividerPaint);
361 | }
362 |
363 | for (DirectionalRect rect : halfHourDividerRects) {
364 | canvas.drawRect(rect.getLeft(),
365 | rect.getTop(),
366 | rect.getRight(),
367 | rect.getBottom(),
368 | halfHourDividerPaint);
369 | }
370 | }
371 |
372 | @Override
373 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
374 | validateChildViews();
375 |
376 | isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
377 |
378 | // Start with the default measured dimension
379 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
380 |
381 | parentWidth = getMeasuredWidth();
382 |
383 | // Measure the hour labels using two passes, this first pass is only to figure out the
384 | // heights
385 | int hourLabelStart = isRtl ? getPaddingRight() : getPaddingLeft();
386 | int hourLabelEnd = hourLabelStart + hourLabelWidth;
387 | int firstDividerTop = 0;
388 | int lastDividerMarginBottom = 0;
389 | int hourLabelViewsSize = hourLabelViews.size();
390 | for (int i = 0; i < hourLabelViewsSize; i++) {
391 | View view = hourLabelViews.get(i);
392 | measureChild(view, widthMeasureSpec, heightMeasureSpec);
393 |
394 | if (i == 0) {
395 | firstDividerTop = view.getMeasuredHeight() / 2;
396 | } else if (i == hourLabelViewsSize - 1) {
397 | lastDividerMarginBottom = view.getMeasuredHeight() / 2;
398 | }
399 | }
400 |
401 | // Calculate the measured height
402 | int usableHeight = (hourDividerRects.size() + halfHourDividerRects.size() - 1) * usableHalfHourHeight;
403 | minuteHeight = (float) usableHeight / minuteCount;
404 | firstDividerTop += getPaddingTop();
405 | int verticalPadding = firstDividerTop + lastDividerMarginBottom + getPaddingBottom() + dividerHeight;
406 | int measuredHeight = usableHeight + verticalPadding;
407 |
408 | // Calculate the horizontal positions of the dividers
409 | int dividerStart = hourLabelEnd + hourLabelMarginEnd;
410 | int dividerEnd = getMeasuredWidth() - (isRtl ? getPaddingLeft() : getPaddingRight());
411 |
412 | // Set the rects for hour labels, dividers, and events
413 | setHourLabelRects(hourLabelStart, hourLabelEnd, firstDividerTop);
414 | setDividerRects(firstDividerTop, dividerStart, dividerEnd);
415 | setEventRects(firstDividerTop, minuteHeight, dividerStart, dividerEnd);
416 |
417 | // Measure the hour labels and events for a final time
418 | measureHourLabels();
419 | measureEvents();
420 |
421 | setMeasuredDimension(widthMeasureSpec, measuredHeight);
422 | }
423 |
424 | protected void measureExactly(@NonNull View view, @NonNull DirectionalRect rect) {
425 | view.measure(MeasureSpec.makeMeasureSpec(rect.getRight() - rect.getLeft(), MeasureSpec.EXACTLY),
426 | MeasureSpec.makeMeasureSpec(rect.getBottom() - rect.getTop(), MeasureSpec.EXACTLY));
427 | }
428 |
429 | /**
430 | * Sets the dimensions of a rect while factoring in whether or not right-to-left mode is on.
431 | *
432 | * @param rect the rect to update
433 | * @param start the start of the rect in left-to-right mode
434 | * @param top the top of the rect, it will not be translated
435 | * @param end the end of the rect in left-to-right mode
436 | * @param bottom the bottom of the rect, it will not be translated
437 | */
438 | protected void setRect(@NonNull DirectionalRect rect, int start, int top, int end, int bottom) {
439 | rect.set(isRtl, parentWidth, start, top, end, bottom);
440 | }
441 |
442 | /**
443 | * @return the height in pixels taken up by each hour and half-hour divider
444 | */
445 | protected int getDividerHeight() {
446 | return dividerHeight;
447 | }
448 |
449 | /**
450 | * @return the height in pixels taken up by each minute
451 | */
452 | protected float getMinuteHeight() {
453 | return minuteHeight;
454 | }
455 |
456 | @VisibleForTesting
457 | void setHourLabelRects(int hourLabelStart, int hourLabelEnd, int firstDividerTop) {
458 | for (int i = 0; i < hourLabelViews.size(); i++) {
459 | View view = hourLabelViews.get(i);
460 |
461 | int height = view.getMeasuredHeight();
462 |
463 | int top = firstDividerTop + usableHalfHourHeight * i * 2 - height / 2;
464 | int bottom = top + height;
465 |
466 | setRect(hourLabelRects.get(i), hourLabelStart, top, hourLabelEnd, bottom);
467 | }
468 | }
469 |
470 | @VisibleForTesting
471 | void setDividerRects(int firstDividerTop, int dividerStart, int dividerEnd) {
472 | for (int i = 0; i < hourDividerRects.size(); i++) {
473 | int top = firstDividerTop + i * 2 * usableHalfHourHeight;
474 | int bottom = top + dividerHeight;
475 |
476 | setRect(hourDividerRects.get(i), dividerStart, top, dividerEnd, bottom);
477 | }
478 |
479 | for (int i = 0; i < halfHourDividerRects.size(); i++) {
480 | int top = firstDividerTop + (i * 2 + 1) * usableHalfHourHeight;
481 | int bottom = top + dividerHeight;
482 |
483 | setRect(halfHourDividerRects.get(i), dividerStart, top, dividerEnd, bottom);
484 | }
485 | }
486 |
487 | @VisibleForTesting
488 | void setEventRects(int firstDividerTop, float minuteHeight, int dividerStart, int dividerEnd) {
489 | if (eventColumnSpansHelper == null) {
490 | return;
491 | }
492 |
493 | int eventColumnWidth = eventColumnSpansHelper.columnCount > 0
494 | ? (dividerEnd - dividerStart) / eventColumnSpansHelper.columnCount
495 | : 0;
496 |
497 | for (int i = 0; i < filteredEventViews.size(); i++) {
498 | EventTimeRange timeRange = filteredEventTimeRanges.get(i);
499 | EventColumnSpan columnSpan = eventColumnSpansHelper.columnSpans.get(i);
500 |
501 | int filteredStartMinute = Math.max(startMinute, timeRange.startMinute);
502 | int duration = Math.min(endMinute, timeRange.endMinute) - filteredStartMinute;
503 | if (duration < MIN_DURATION_MINUTES) {
504 | duration = MIN_DURATION_MINUTES;
505 | filteredStartMinute = endMinute - duration;
506 | }
507 |
508 | int start = columnSpan.startColumn * eventColumnWidth + dividerStart + eventMargin;
509 | int end = start + (columnSpan.endColumn - columnSpan.startColumn) * eventColumnWidth - eventMargin * 2;
510 |
511 | int topOffset = (int) ((filteredStartMinute - startMinute) * minuteHeight);
512 |
513 | int top = firstDividerTop + topOffset + dividerHeight + eventMargin;
514 | int bottom = top + (int) (duration * minuteHeight) - eventMargin * 2 - dividerHeight;
515 |
516 | setRect(eventRects.get(i), start, top, end, bottom);
517 | }
518 | }
519 |
520 | /**
521 | * Validates the state of the child views during {@link #onMeasure(int, int)}.
522 | *
523 | * @throws IllegalStateException thrown when one or more of the child views are not in a valid
524 | * state
525 | */
526 | @CallSuper
527 | protected void validateChildViews() throws IllegalStateException {
528 | if (hourLabelViews.size() == 0) {
529 | throw new IllegalStateException("No hour label views, setHourLabelViews() must be called before this view is rendered");
530 | } else if (hourLabelViews.size() != hourLabelsCount) {
531 | throw new IllegalStateException("Inconsistent number of hour label views, there should be " + hourLabelsCount + " but " + hourLabelViews.size() + " were found");
532 | } else if (filteredEventViews.size() != filteredEventTimeRanges.size()) {
533 | throw new IllegalStateException("Inconsistent number of event views or event time ranges, they should either be equal in length or both should be null");
534 | }
535 | }
536 |
537 | /**
538 | * Can be used by subclasses that need to layout child views relative to the hour dividers.
539 | *
540 | * @return a list of hour divider rects
541 | */
542 | @NonNull
543 | protected List getHourDividerRects() {
544 | return hourDividerRects;
545 | }
546 |
547 | /**
548 | * Can be used by subclasses that need to layout child views relative to the half-hour dividers.
549 | *
550 | * @return a list of half-hour divider rects
551 | */
552 | @NonNull
553 | protected List getHalfHourDividerRects() {
554 | return halfHourDividerRects;
555 | }
556 |
557 | @VisibleForTesting
558 | void setParentWidth(int parentWidth) {
559 | this.parentWidth = parentWidth;
560 | }
561 |
562 | private void measureHourLabels() {
563 | for (int i = 0; i < hourLabelViews.size(); i++) {
564 | measureExactly(hourLabelViews.get(i), hourLabelRects.get(i));
565 | }
566 | }
567 |
568 | private void measureEvents() {
569 | for (int i = 0; i < filteredEventViews.size(); i++) {
570 | measureExactly(filteredEventViews.get(i), eventRects.get(i));
571 | }
572 | }
573 |
574 | /**
575 | * Represents the start and end time of a calendar event. Both times are in minutes since the
576 | * start of the day.
577 | */
578 | public static class EventTimeRange {
579 |
580 | private final int startMinute;
581 | private final int endMinute;
582 |
583 | public EventTimeRange(int startMinute, int endMinute) {
584 | this.startMinute = startMinute;
585 | this.endMinute = endMinute;
586 | }
587 |
588 | /**
589 | * @param range the time range to compare
590 | * @return true if the time range to compare overlaps in any way with this time range
591 | */
592 | @VisibleForTesting
593 | boolean conflicts(@NonNull EventTimeRange range) {
594 | return startMinute >= range.startMinute && startMinute < range.endMinute
595 | || endMinute > range.startMinute && endMinute <= range.endMinute
596 | || range.startMinute >= startMinute && range.startMinute < endMinute
597 | || range.endMinute > startMinute && range.endMinute <= endMinute;
598 | }
599 | }
600 |
601 | /**
602 | * Represents the start and end columns a calendar event should span between.
603 | */
604 | @VisibleForTesting
605 | static class EventColumnSpan {
606 |
607 | int startColumn = -1;
608 | int endColumn = -1;
609 | }
610 |
611 | /**
612 | * Helps calculate the start and end columns for a collection of calendar events.
613 | */
614 | @VisibleForTesting
615 | static class EventColumnSpansHelper {
616 |
617 | @NonNull
618 | final List columnSpans;
619 | int columnCount;
620 |
621 | @NonNull
622 | private final List timeRanges;
623 |
624 | @VisibleForTesting
625 | EventColumnSpansHelper(@NonNull List timeRanges) {
626 | this.timeRanges = timeRanges;
627 | this.columnSpans = new ArrayList<>(timeRanges.size());
628 |
629 | // Find the start and end columns for each event
630 | for (int i = 0; i < this.timeRanges.size(); i++) {
631 | findStartColumn(i);
632 | }
633 |
634 | for (int i = 0; i < this.timeRanges.size(); i++) {
635 | findEndColumn(i);
636 | }
637 | }
638 |
639 | private void findStartColumn(int position) {
640 | for (int i = 0; i < timeRanges.size(); i++) {
641 | if (isColumnEmpty(i, position)) {
642 | EventColumnSpan columnSpan = new EventColumnSpan();
643 | columnSpan.startColumn = i;
644 | columnSpan.endColumn = i + 1;
645 | columnSpans.add(columnSpan);
646 |
647 | columnCount = Math.max(columnCount, i + 1);
648 |
649 | break;
650 | }
651 | }
652 | }
653 |
654 | private void findEndColumn(int position) {
655 | EventColumnSpan columnSpan = columnSpans.get(position);
656 | for (int i = columnSpan.endColumn; i < columnCount; i++) {
657 | if (!isColumnEmpty(i, position)) {
658 | break;
659 | }
660 |
661 | columnSpan.endColumn++;
662 | }
663 | }
664 |
665 | private boolean isColumnEmpty(int column, int position) {
666 | EventTimeRange timeRange = timeRanges.get(position);
667 | for (int i = 0; i < columnSpans.size(); i++) {
668 | if (position == i) {
669 | continue;
670 | }
671 |
672 | EventTimeRange compareTimeRange = timeRanges.get(i);
673 | EventColumnSpan compareColumnSpan = columnSpans.get(i);
674 | if (compareColumnSpan.startColumn == column && compareTimeRange.conflicts(timeRange)) {
675 | return false;
676 | }
677 | }
678 |
679 | return true;
680 | }
681 | }
682 | }
683 |
--------------------------------------------------------------------------------