Skip to content

Commit

Permalink
Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
azzumw committed Mar 5, 2022
1 parent 22a2c23 commit 5cde740
Show file tree
Hide file tree
Showing 36 changed files with 981 additions and 0 deletions.
1 change: 1 addition & 0 deletions WaterMeProject/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
52 changes: 52 additions & 0 deletions WaterMeProject/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}

android {
compileSdk 32

defaultConfig {
applicationId "com.example.watermeproject"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures{
dataBinding true
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation('androidx.fragment:fragment-ktx:1.4.1')
implementation "androidx.activity:activity-ktx:1.4.0"
implementation('androidx.work:work-runtime-ktx:2.7.1')


testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}
21 changes: 21 additions & 0 deletions WaterMeProject/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.example.watermeproject

import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class NotificationTest {

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class NotificationTest {

private val timeout: Long = 6000

private val plantName = "Carrot"

@get:Rule
var activityRule: ActivityScenarioRule<MainActivity>
= ActivityScenarioRule(MainActivity::class.java)

private lateinit var uiDevice: UiDevice

@Before
fun setup() {
uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
Espresso.onView(ViewMatchers.withText("Carrot")).perform(ViewActions.longClick())
Espresso.onView(ViewMatchers.withText("5 seconds")).perform(ViewActions.click())
uiDevice.pressHome()
uiDevice.openNotification()
uiDevice.wait(Until.hasObject(By.textContains(plantName)), timeout)
}

@Test
fun notification_scheduled() {
val notification = uiDevice.findObject(UiSelector().textContains(plantName)).exists()
Assert.assertTrue("Could not find text 'Carrot'", notification)
uiDevice.pressHome()
}

@Test
fun notification_click() {
val notification = uiDevice.findObject(UiSelector().textContains("Carrot"))
notification.click()
uiDevice.wait(Until.hasObject(By.pkg("com.example.waterme")
.depth(0)), 1000)
val pkg = uiDevice.findObject(UiSelector().packageName("com.example.waterme"))
.exists()
Assert.assertTrue("Could not find package", pkg)
}
}
}
24 changes: 24 additions & 0 deletions WaterMeProject/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.watermeproject">

<application
android:name=".BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WaterMeProject">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2021 The Android Open Source Project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.watermeproject

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build

class BaseApplication : Application() {

override fun onCreate() {
super.onCreate()

//https://developer.android.com/training/notify-user/build-notification
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}

companion object {
const val CHANNEL_ID = "water_reminder_id"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.watermeproject

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.RecyclerView
import com.example.watermeproject.adapter.PlantAdapter
import com.example.watermeproject.adapter.PlantListener
import com.example.watermeproject.ui.ReminderDialogFragment
import com.example.watermeproject.viewmodel.PlantViewModel
import com.example.watermeproject.viewmodel.PlantViewModelFactory

class MainActivity : AppCompatActivity() {

private val viewModel: PlantViewModel by viewModels {
PlantViewModelFactory(application)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val adapter = PlantAdapter(PlantListener { plant ->
val dialog = ReminderDialogFragment(plant.name)
dialog.show(supportFragmentManager,"WaterReminderDialogFragment")
true
})
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
recyclerView.adapter = adapter
val data = viewModel.plants
adapter.submitList(data)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.example.watermeproject.adapter


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.watermeproject.databinding.ListItemBinding
import com.example.watermeproject.model.Plant

class PlantAdapter(private val longClickListener: PlantListener) :
ListAdapter<Plant, PlantAdapter.PlantViewHolder>(DiffCallback) {

class PlantViewHolder(
private val binding: ListItemBinding
) : RecyclerView.ViewHolder(binding.root) {

fun bind(
longClickListener: PlantListener,
plant: Plant
) {
binding.plant = plant
binding.longClickListner = longClickListener
binding.executePendingBindings()
}
}

companion object DiffCallback: DiffUtil.ItemCallback<Plant>() {
override fun areItemsTheSame(oldItem: Plant, newItem: Plant): Boolean {
return oldItem == newItem
}

override fun areContentsTheSame(oldItem: Plant, newItem: Plant): Boolean {
return oldItem.name == newItem.name
}

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PlantViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return PlantViewHolder(
ListItemBinding.inflate(layoutInflater, parent, false)
)
}

override fun onBindViewHolder(holder: PlantViewHolder, position: Int) {
val plant = getItem(position)
holder.bind(longClickListener, plant)
}
}

class PlantListener(val longClickListener: (plant: Plant) -> Boolean) {
fun onLongClick(plant: Plant) = longClickListener(plant)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.example.watermeproject.data

import com.example.watermeproject.model.Plant

object DataSource {
val plants = listOf(
Plant(
name = "Lithop",
schedule = "monthly",
type = "Succulent",
description = "Stone mimicking succulent"
),
Plant(
name = "Carrot",
schedule = "daily",
type = "Root",
description = "Hardy root vegetable"
),
Plant(
name = "Peony",
schedule = "weekly",
type = "Flower",
description = "Spring blooming flower"
),
Plant(
name = "Pothos",
schedule = "weekly",
type = "Houseplant",
description = "Indoor vine"
),
Plant(
name = "Fiddle Leaf Fig",
schedule = "weekly",
type = "Broadleaf evergreen",
description = "Ornamental fig"
),
Plant(
name = "Strawberry",
schedule = "daily",
type = "Fruit",
description = "Delicious 'multiple fruit'"
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.watermeproject.model

data class Plant(val name : String,
val schedule : String,
val type:String,
val description:String)
Loading

0 comments on commit 5cde740

Please sign in to comment.