Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
chjaeggi committed Jul 19, 2019
0 parents commit 1528674
Show file tree
Hide file tree
Showing 30 changed files with 865 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Android Studio
.idea
.gradle
/*/.gitignore
/*/local.properties
local.properties
/*/out
/out
/*/*/build
/*/build
build
*.iml
*.iws
*.ipr
*~
*.swp
.DS_*
58 changes: 58 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.chjaeggi.volkiweather"
minSdkVersion 24
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable('AllowBackup', 'GoogleAppIndexingWarning', 'MissingApplicationIcon')
}
dataBinding {
enabled = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

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

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0-alpha02'

implementation 'org.koin:koin-android:1.0.2'
implementation 'org.koin:koin-android-viewmodel:1.0.2'

implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxkotlin:2.3.0'

implementation 'com.jakewharton.timber:timber:4.7.1'

implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
compileOnly 'com.google.android.things:androidthings:1.0'
}
21 changes: 21 additions & 0 deletions 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,24 @@
package com.chjaeggi.volkiweather

import android.support.test.InstrumentationRegistry
import android.support.test.runner.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getTargetContext()
assertEquals("com.chjaeggi.volkiweather", appContext.packageName)
}
}
28 changes: 28 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.chjaeggi.volkiweather">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>

<application android:label="@string/app_name"
android:name="com.chjaeggi.volkiweather.VolkiWeatherApp">
<uses-library android:name="com.google.android.things"/>

<activity android:name=".MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Make this the first activity that is displayed when the device boots. -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

</manifest>
48 changes: 48 additions & 0 deletions app/src/main/java/com/chjaeggi/volkiweather/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.chjaeggi.volkiweather

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.chjaeggi.volkiweather.databinding.ActivityMainBinding
import org.koin.android.viewmodel.ext.android.viewModel

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private val viewModel by viewModel<MainViewModel>()
private val intentFilter = IntentFilter()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.model = viewModel

binding.lifecycleOwner = this

if (wifiAvailable()) {
viewModel.requestWeatherData()
} else {
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION)
registerReceiver(broadCastReceiver, intentFilter)
}
}

private fun wifiAvailable(): Boolean {
return (getSystemService(Context.WIFI_SERVICE) as WifiManager).connectionInfo.networkId != -1
}

private val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val info = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO)
if (info != null && info.isConnected) {
viewModel.requestWeatherData()
}
}
}
}
37 changes: 37 additions & 0 deletions app/src/main/java/com/chjaeggi/volkiweather/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.chjaeggi.volkiweather

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.chjaeggi.volkiweather.util.AppRxSchedulers
import com.chjaeggi.volkiweather.util.RxAwareViewModel
import com.chjaeggi.volkiweather.util.plusAssign
import com.chjaeggi.volkiweather.weather.WeatherDataSource
import io.reactivex.rxkotlin.subscribeBy
import timber.log.Timber
import java.text.DateFormat
import java.util.*

class MainViewModel(
private val schedulers: AppRxSchedulers,
private val data: WeatherDataSource
) :
RxAwareViewModel() {

private val _dateTime = MutableLiveData<String>()
val dateTime = _dateTime

fun requestWeatherData() {
disposables += data
.getWeatherData()
.subscribeOn(schedulers.io)
.observeOn(schedulers.main)
.subscribeBy(
onNext = {
dateTime.value = "${DateFormat.getDateTimeInstance().format(Date())}\n\n$it"
},
onError = {
Timber.d(it)
}
)
}
}
17 changes: 17 additions & 0 deletions app/src/main/java/com/chjaeggi/volkiweather/VolkiWeatherApp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.chjaeggi.volkiweather
import android.app.Application
import com.chjaeggi.volkiweather.di.appModule
import org.koin.android.ext.android.startKoin
import timber.log.Timber

class VolkiWeatherApp : Application() {

override fun onCreate() {
super.onCreate()
startKoin(this, listOf(appModule))
if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree())
}
}

}
20 changes: 20 additions & 0 deletions app/src/main/java/com/chjaeggi/volkiweather/di/modules.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.chjaeggi.volkiweather.di
import com.chjaeggi.volkiweather.MainViewModel
import com.chjaeggi.volkiweather.weather.WeatherCloudApi
import com.chjaeggi.volkiweather.weather.WeatherDataSource
import com.chjaeggi.volkiweather.weather.WeatherRepository
import com.chjaeggi.volkiweather.util.AppRxSchedulers
import org.koin.android.viewmodel.ext.koin.viewModel
import org.koin.dsl.module.module

val appModule = module(override = true) {

single { AppRxSchedulers() }
single<WeatherDataSource> {
WeatherRepository(
WeatherCloudApi.newInstance()
)
}

viewModel { MainViewModel(get(), get())}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/chjaeggi/volkiweather/domain/WeatherData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.chjaeggi.volkiweather.domain

/**
* Immutable model class for weather data
*/
data class WeatherData(
val id: Int,
val description: String,
val wind: Float,
val temperatureHigh: Float,
val temperatureLow: Float,
val humidity: Float,
val icon: String,
val sunrise: Int,
val sunset: Int,
val idTomorrow: Int,
val temperatureTomorrow: Float,
val humidityTomorrow: Float
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.chjaeggi.volkiweather.util

import io.reactivex.Scheduler
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

data class AppRxSchedulers(
val data: Scheduler = Schedulers.single(),
val io: Scheduler = Schedulers.io(),
val main: Scheduler = AndroidSchedulers.mainThread()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.chjaeggi.volkiweather.util

import androidx.lifecycle.ViewModel
import io.reactivex.disposables.CompositeDisposable

/**
* Simple ViewModel which exposes a CompositeDisposable which is automatically cleared when
* the ViewModel is cleared.
*/
open class RxAwareViewModel : ViewModel() {

val disposables = CompositeDisposable()

override fun onCleared() {
super.onCleared()
disposables.clear()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.chjaeggi.volkiweather.util

import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable

operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
add(disposable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.chjaeggi.volkiweather.util

import androidx.annotation.MainThread
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicBoolean


class SingleLiveEvent<T> : MutableLiveData<T>() {

companion object {
private val TAG = "SingleLiveEvent"
}

private val mPending = AtomicBoolean(false)

@MainThread
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
check(!hasActiveObservers()) { "Multiple observers registered!" }

// Observe the internal MutableLiveData
super.observe(owner, Observer<T> { t ->
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t)
}
})
}

@MainThread
override fun setValue(t: T?) {
mPending.set(true)
super.setValue(t)
}

@MainThread
fun call() {
value = null
}
}
Loading

0 comments on commit 1528674

Please sign in to comment.