package jackall.overlaymulticounter.db import io.realm.RealmResults import jackall.overlaymulticounter.vo.Counter /** * Created by matsumoto_k on 2017/11/08. */ class CounterRealmHelper : BaseRealmHelper<Counter>() { val query by lazy { realm.where(Counter::class.java) } override fun insert(realmObject: Counter) { realm.executeTransaction { realm.copyToRealm(realmObject) } } override fun update(realmObject: Counter) { realm.executeTransaction { realm.copyToRealmOrUpdate(realmObject) } } fun plus(id: Int) { realm.executeTransaction { query.equalTo("id", id).findFirst().apply { count++ } } } fun minus(id: Int) { realm.executeTransaction { query.equalTo("id", id).findFirst().apply { if (count > 0) count-- } } } fun reset(id: Int) { realm.executeTransaction { query.equalTo("id", id).findFirst().apply { count = 0 } } } override fun findAll(): RealmResults<Counter> { return query.findAll() } fun getNextId(): Int { if (query.count() == 0L) return 0 return query.count().toInt() + 1 } }