Skip to content

Commit

Permalink
db by Sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
raylemon committed Mar 19, 2016
1 parent f5e67c2 commit 746464a
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 70 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ dependencies {
compile "org.jetbrains.anko:anko-common:$anko_version"
compile "com.google.code.gson:gson:$gson_version"
compile "com.squareup.picasso:picasso:$picasso_version"

compile 'za.co.cporm:CPOrm:2.99'
compile "com.github.satyan:sugar:$sugar_version"
}
24 changes: 12 additions & 12 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".ForecastApp"
android:name="com.orm.SugarApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="CPORM_CONFIG"
android:value="com.github.raylemon.weather.data.db.CPOrmConfig" />
<!--<meta-data
android:name="AUTHORITY"
android:value="com.github.raylemon.weather" />-->

<!--<provider
android:name="com.github.raylemon.weather.provider.ForecastContentProvider"
android:authorities="com.github.raylemon.weather"
android:exported="false"
android:process=":provider" />-->
android:name="DATABASE_NAME"
android:value="forecast" />
<meta-data
android:name="DATABASE_VERSION"
android:value="1" />
<meta-data
android:name="DOMAIN_PACKAGE_NAME"
android:value="com.github.raylemon.weather.data.db" />
<meta-data
android:name="QUERY_LOG"
android:value="true" />

<activity
android:name=".ui.MainAktivity"
Expand Down
16 changes: 0 additions & 16 deletions app/src/main/java/com/github/raylemon/weather/ForecastApp.kt

This file was deleted.

31 changes: 31 additions & 0 deletions app/src/main/java/com/github/raylemon/weather/data/DBServer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.raylemon.weather.data

import com.github.raylemon.weather.data.db.DbForecast
import com.github.raylemon.weather.data.domain.ForecastList
import com.github.raylemon.weather.data.mapper.DBMapper
import com.orm.query.Condition
import com.orm.query.Select

/**
* Created by big04 on 17-03-16.
*/
class DBServer : ForecastSource {
private val DAY_IN_MILLIS = 1000 * 24 * 60 * 60

private fun todayInMilis() = System.currentTimeMillis() * DAY_IN_MILLIS / DAY_IN_MILLIS

override fun getForecast(cnt: Int, city: String): ForecastList? {
val list = Select.from(DbForecast::class.java)
.where("dt>= ?", arrayOf(todayInMilis().toString()))
.and(Condition("city").like(city))
.limit(cnt.toString())
.list()
if (list.size == 0) return null
else return DBMapper.convertToDomain(list)
}

fun save(forecastList: ForecastList) {
DBMapper.convertFromDomain(forecastList).forEach { it.save() }
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.raylemon.weather.data

import com.github.raylemon.weather.data.domain.ForecastList
import org.jetbrains.anko.AnkoLogger
import org.jetbrains.anko.info
import java.util.*

/**
* Created by big04 on 17-03-16.
*/
object ForecastProvider : ForecastSource, AnkoLogger {
val sources = arrayOf(DBServer(), JsonServer())
override fun getForecast(cnt: Int, city: String): ForecastList {
for (source in sources) {
val fore = source.getForecast(cnt, city)
if (fore != null) {
info("data sent from $source")
return fore
}
}
throw NoSuchElementException("No element found !")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.raylemon.weather.data

import com.github.raylemon.weather.data.domain.ForecastList

/**
* Created by big04 on 17-03-16.
*/
interface ForecastSource {
fun getForecast(cnt: Int, city: String): ForecastList?
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import java.net.URL
/**
* Created by big04 on 06-03-16.
*/
class JsonServer {
class JsonServer : ForecastSource {
val APP_ID = "f9d37616d477b96758d88a16d3a7c347"
val DAILY_URL = "http://api.openweathermap.org/data/2.5/forecast/daily?lang=fr&units=metric&cnt=%d&APPID=$APP_ID&q=%s"

fun getForecast(cnt: Int, city: String): ForecastList {
override fun getForecast(cnt: Int, city: String): ForecastList {
val jsonStr = URL(DAILY_URL.format(cnt, city)).readText()
val result = Gson().fromJson(jsonStr, ForecastResult::class.java)
return JsonMapper.convertToDomain(result)
val domain = JsonMapper.convertToDomain(result)
DBServer().save(domain)
return domain
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package com.github.raylemon.weather.data.db

import za.co.cporm.model.CPDefaultRecord
import com.orm.SugarRecord
import com.orm.dsl.Unique


/**
* Created by big04 on 17-03-16.
*/
class ForecastList(var city: String = "", var country: String = "", var weather: List<Forecast> = emptyList()) : CPDefaultRecord<ForecastList>()
class Forecast(var dt: Long = 0L, var desc: String = "", var icon: String = "", var temp: Temperatures = Temperatures(), var pressure: Float = 0f, var humidity: Int = 0) : CPDefaultRecord<Forecast>()
class Temperatures(var day: Float = 0f, var min: Float = 0f, var max: Float = 0f, var night: Float = 0f, var morn: Float = 0f, var eve: Float = 0f) : CPDefaultRecord<Temperatures>()

class DbForecast(
@Unique var dt: Long = 0L,
var city: String = "",
var country: String = "",
var desc: String = "",
var icon: String = "",
var pressure: Float = 0f,
var humidity: Int = 0,
var day: Float = 0f,
var min: Float = 0f,
var max: Float = 0f,
var night: Float = 0f,
var morn: Float = 0f,
var eve: Float = 0f) : SugarRecord()
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package com.github.raylemon.weather.data.mapper

import com.github.raylemon.weather.data.db.Forecast
import com.github.raylemon.weather.data.db.ForecastList
import com.github.raylemon.weather.data.db.Temperatures
import com.github.raylemon.weather.data.domain.Forecast as DFore
import com.github.raylemon.weather.data.domain.ForecastList as DFList
import com.github.raylemon.weather.data.domain.Temperatures as DTemp
import com.github.raylemon.weather.data.db.DbForecast
import com.github.raylemon.weather.data.domain.Forecast
import com.github.raylemon.weather.data.domain.ForecastList
import com.github.raylemon.weather.data.domain.Temperatures

/**
* Created by big04 on 17-03-16.
*/
object DBMapper {
fun convertToDomain(forecastList: ForecastList) = with(forecastList) { DFList(city, country, convertListToDomain(weather)) }
private fun convertListToDomain(list: List<Forecast>) = list.map { convertItemListToDomain(it) }
private fun convertItemListToDomain(forecast: Forecast) = with(forecast) { DFore(dt, desc, icon, convertTempToDomain(temp), pressure, humidity) }
private fun convertTempToDomain(temperatures: Temperatures) = with(temperatures) { DTemp(day, min, max, night, eve, morn) }
fun convertToDomain(weather: List<DbForecast>) = with(weather) { ForecastList(first().city, first().city, convertListToDomain(this)) }
private fun convertListToDomain(list: List<DbForecast>) = list.map { convertItemListToDomain(it) }
private fun convertItemListToDomain(dbForecast: DbForecast) = with(dbForecast) { Forecast(dt, desc, icon, Temperatures(day, min, max, night, eve, morn), pressure, humidity) }

fun convertFromDomain(forecastList: DFList) = with(forecastList) { ForecastList(city, country, convertListFromDomain(weather)) }
private fun convertListFromDomain(list: List<DFore>) = list.map { convertItemListFromDomain(it) }
private fun convertItemListFromDomain(forecast: DFore) = with(forecast) { Forecast(dt, desc, icon, convertTempFromDomain(temp), pressure, humidity) }
private fun convertTempFromDomain(temperatures: DTemp) = with(temperatures) { Temperatures(day, min, max, night, eve, morn) }
fun convertFromDomain(forecastList: ForecastList) = with(forecastList) {
convertListFromDomain(weather, city, country)
}

private fun convertListFromDomain(list: List<Forecast>, city: String, country: String) = list.map { convertItemListFromDomain(it, city, country) }
private fun convertItemListFromDomain(forecast: Forecast, city: String, country: String) = with(forecast) { DbForecast(dt, city, country, desc, icon, pressure, humidity, temp.day, temp.min, temp.max, temp.night, temp.morn, temp.eve) }

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import android.support.design.widget.CollapsingToolbarLayout
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import com.github.raylemon.weather.R
import com.github.raylemon.weather.data.JsonServer
import com.github.raylemon.weather.data.ForecastProvider
import com.github.raylemon.weather.ui.adapter.ForecastAdapter
import com.github.raylemon.weather.ui.toolbar.ToolbarManager
import kotlinx.android.synthetic.main.forecast_list.*
Expand Down Expand Up @@ -38,7 +38,7 @@ class MainAktivity : AppCompatActivity(), ToolbarManager {
override fun onResume() {
super.onResume()
async() {
val items = JsonServer().getForecast(cnt, city)
val items = ForecastProvider.getForecast(cnt, city)
uiThread {
vForecastList.adapter = ForecastAdapter(items) {
val twoPane = resources.getBoolean(R.bool.twoPane)
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ buildscript {
ext.anko_version = '0.8.3'
ext.picasso_version = '2.5.2'
ext.gson_version = '2.6.2'
ext.sugar_version = '1.5'

repositories {
jcenter()
Expand Down

0 comments on commit 746464a

Please sign in to comment.