37 lines
1.7 KiB
Kotlin
37 lines
1.7 KiB
Kotlin
package kr.co.itosm.dutycalendar
|
|
|
|
import android.content.Context
|
|
|
|
/**
|
|
* 관리자 '공지'에서 켠 공지 띠. 홈페이지 맨 위와 같은 글을 앱 첫 화면 맨 위에 한 줄로 보인다.
|
|
* 서버는 버전 확인(api/version)에 notice를 함께 준다. 마지막으로 받은 공지를 기기에 두어 연결이 없어도 보인다.
|
|
* 예전 서버처럼 notice가 없는 응답이면 보관한 공지를 바꾸지 않는다. 회원 정보는 보내지 않는다.
|
|
*/
|
|
data class Notice(val text: String, val warn: Boolean) {
|
|
val shown: Boolean get() = text.isNotEmpty()
|
|
|
|
companion object {
|
|
const val MAX = 200
|
|
private const val PREFS = "site_notice"
|
|
val NONE = Notice("", false)
|
|
|
|
/** 서버 값을 화면에 쓸 모양으로 정리한다. 줄바꿈·제어 문자는 빈칸 하나로 바꾸고 200자까지 쓴다. */
|
|
fun of(text: String?, tone: String?): Notice {
|
|
val clean = text.orEmpty().replace(Regex("[\\p{Cc}\\s]+"), " ").trim().take(MAX)
|
|
return if (clean.isEmpty()) NONE else Notice(clean, tone == "warn")
|
|
}
|
|
|
|
fun load(context: Context): Notice {
|
|
val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
|
|
return of(prefs.getString("text", ""), if (prefs.getBoolean("warn", false)) "warn" else "info")
|
|
}
|
|
|
|
/** 보관한 공지와 다를 때만 저장하고 true를 돌려준다. */
|
|
fun save(context: Context, notice: Notice): Boolean {
|
|
if (load(context) == notice) return false
|
|
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit().putString("text", notice.text).putBoolean("warn", notice.warn).apply()
|
|
return true
|
|
}
|
|
}
|
|
}
|