Deleted table data manager classes, as they are redundant with Room

This commit is contained in:
michael-bailey 2025-04-14 23:15:04 +01:00
parent 46dbd20e73
commit 6f04b26b84
13 changed files with 0 additions and 429 deletions

View File

@ -1,37 +0,0 @@
package org.british_information_technologies.gym_log_book.data_manager
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
@Deprecated("This is an old api, DON'T USE")
@Module
@InstallIn(SingletonComponent::class)
class DataManagerProvider {
//
// @Provides
// @Singleton
// fun provideExerciseTypeDataManager(
// @ApplicationContext context: Context,
// table: ExerciseTypeTable
// ): ExerciseTypeDataManager {
// return ExerciseTypeDataManager(context, table)
// }
//
// @Provides
// @Singleton
// fun provideExerciseDataManager(
// table: ExerciseTable
// ): ExerciseDataManager {
// return ExerciseDataManager(table)
// }
//
// @Provides
// @Singleton
// fun provideWeightDataManager(
// @ApplicationContext context: Context,
// table: WeightTable
// ): WeightDataManager {
// return WeightDataManager(context, table)
// }
}

View File

@ -1,11 +0,0 @@
package org.british_information_technologies.gym_log_book.data_manager
import org.british_information_technologies.gym_library.data_type.ExerciseItem
import org.british_information_technologies.gym_log_book.lib.data_manager.BaseDataManager
import org.british_information_technologies.gym_log_book.table.ExerciseTable
import javax.inject.Inject
@Deprecated("This is an old api, DON'T USE")
class ExerciseDataManager @Inject constructor(
table: ExerciseTable
) : BaseDataManager<ExerciseItem>(table)

View File

@ -1,13 +0,0 @@
package org.british_information_technologies.gym_log_book.data_manager
import android.content.Context
import org.british_information_technologies.gym_library.data_type.ExerciseType
import org.british_information_technologies.gym_log_book.lib.data_manager.BaseDataManager
import org.british_information_technologies.gym_log_book.table.ExerciseTypeTable
import javax.inject.Inject
@Deprecated("This is an old api, DON'T USE")
class ExerciseTypeDataManager @Inject constructor(
private val context: Context,
table: ExerciseTypeTable
) : BaseDataManager<ExerciseType>(table)

View File

@ -1,13 +0,0 @@
package org.british_information_technologies.gym_log_book.data_manager
import android.content.Context
import org.british_information_technologies.gym_library.data_type.WeightItem
import org.british_information_technologies.gym_log_book.lib.data_manager.BaseDataManager
import org.british_information_technologies.gym_log_book.table.WeightTable
import javax.inject.Inject
@Deprecated("This is an old api, DON'T USE")
class WeightDataManager @Inject constructor(
private val context: Context,
table: WeightTable
) : BaseDataManager<WeightItem>(table)

View File

@ -1,46 +0,0 @@
package org.british_information_technologies.gym_log_book.lib.data_manager
import androidx.lifecycle.MediatorLiveData
import org.british_information_technologies.gym_log_book.lib.table.ITable
/**
* base data manager that prevents direct data manipulation.
*
* This is because i managed to erase my actual data from my device :(
*/
abstract class BaseDataManager<T>(
protected val table: ITable<T>
) {
val liveData: MediatorLiveData<List<T>> = MediatorLiveData<List<T>>()
val rawData = table.getRows().toList()
init {
liveData.apply {
addSource(table.liveData) {
this.value = it
}
value = table.liveData.value
}
}
/**
* Appends a new item and notifies observers
*/
fun append(
factory: (Int) -> T
) {
table.addRow(factory(table.getRowCount()))
}
fun update(
id: Int,
updater: T.() -> Unit
) {
table.updateRow(id, updater)
}
fun delete(id: Int) {
table.removeRow(id)
}
}

View File

@ -1,38 +0,0 @@
package org.british_information_technologies.gym_log_book.lib.table
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import io.github.michael_bailey.android_chat_kit.extension.any.log
import org.british_information_technologies.gym_library.data_type.Identifiable
abstract class BaseTable<T : Identifiable> : ITable<T> {
val _liveData: MutableLiveData<List<T>> = MutableLiveData(listOf())
override val liveData: LiveData<List<T>> get() = _liveData
override fun getRowCount(): Int = _liveData.value!!.count()
override fun getRows(): List<T> = liveData.value!!.toList()
override fun getRow(index: Int): T = liveData.value!![index]
override fun addRow(item: T) {
log("adding row")
_liveData.value = (getRows() + item)
save()
}
override fun updateRow(id: Int, update: T.() -> Unit) {
log("updating row")
_liveData.value = (getRows().map {
if (it.id == id) {
it.update()
}
it
})
save()
}
override fun removeRow(id: Int) {
log("removing row")
_liveData.value = (getRows().filter { it.id != id })
save()
}
}

View File

@ -1,65 +0,0 @@
package org.british_information_technologies.gym_log_book.lib.table
import android.content.Context
import io.github.michael_bailey.android_chat_kit.extension.any.log
import org.british_information_technologies.gym_library.data_type.Identifiable
import java.io.FileNotFoundException
abstract class CSVTable<T : Identifiable>(
private val ctx: Context
) : BaseTable<T>() {
protected abstract fun formatHeader(): String
protected abstract fun formatEntry(entry: T): String
protected abstract fun decodeEntry(entry: String): T
fun nextId(): Int = runCatching {
liveData.value!!
.sortedBy { it.id }.last().id + 1
}.getOrNull() ?: 0
init {
load()
}
override fun save() {
log("Saving file")
liveData.value?.let {
log("List isn't null")
var out = ctx.openFileOutput("$tableName.table.csv", Context.MODE_PRIVATE)
log("file exists")
out.write("${formatHeader()}\n".toByteArray())
log("wrote header")
out.use { o ->
it.forEach {
o.write("${formatEntry(it)}\n".toByteArray())
}
log("wrote items")
}
out.flush()
log("flushed")
}
}
override fun load() {
log("loading file")
try {
val reader = ctx.openFileInput("$tableName.table.csv").bufferedReader()
log("opened reader")
val nonEmpty = reader.lineSequence()
.filter { it.isNotBlank() }
.toList()
log("filtered items")
val tmpStore = nonEmpty.withIndex().map {
if (it.index == 0) return@map null
decodeEntry(it.value)
}.filterNotNull().sortedBy { it.id }.toMutableList()
log("sorted into mutable list")
_liveData.postValue(tmpStore)
log("assigned to stores")
} catch (e: FileNotFoundException) {
log("file not found when loading")
_liveData.postValue(mutableListOf())
log("assigned empty list to stores")
}
}
}

View File

@ -1,25 +0,0 @@
package org.british_information_technologies.gym_log_book.lib.table
import androidx.lifecycle.LiveData
/**
* An Interface that describes a table
*/
interface ITable<T> {
val tableName: String
val liveData: LiveData<List<T>>
fun getRowCount(): Int
fun getRows(): List<T>
fun getRow(index: Int): T
fun addRow(item: T)
fun updateRow(id: Int, update: T.() -> Unit)
fun removeRow(id: Int)
fun save()
fun load()
}

View File

@ -1,37 +0,0 @@
package org.british_information_technologies.gym_log_book.table
import android.content.Context
import org.british_information_technologies.gym_library.data_type.ExerciseItem
import org.british_information_technologies.gym_log_book.lib.table.CSVTable
import java.time.LocalDate
class ExerciseTable(val context: Context) : CSVTable<ExerciseItem>(context) {
override val tableName: String
get() = "exercise"
override fun formatHeader(): String =
"'ID', 'Date', 'Exercise', 'SetNumber', 'Weight', 'Reps'"
override fun decodeEntry(entry: String): ExerciseItem {
val (id, date, exercise, setNumber, Weight, Reps) = entry.split(
", ",
ignoreCase = false,
limit = 6
)
return ExerciseItem(
id.toInt(),
LocalDate.parse(date),
exercise,
setNumber.toInt(),
Weight.toDouble(),
Reps.toInt()
)
}
override fun formatEntry(entry: ExerciseItem): String {
return entry.let { "${it.id}, ${it.date}, ${it.exercise}, ${it.setNumber}, ${it.weight}, ${it.reps}" }
}
}
private operator fun <E : Any> List<E>.component6() = get(5)

View File

@ -1,32 +0,0 @@
package org.british_information_technologies.gym_log_book.table
import android.content.Context
import org.british_information_technologies.gym_library.data_type.ExerciseType
import org.british_information_technologies.gym_log_book.lib.table.CSVTable
class ExerciseTypeTable(val context: Context) :
CSVTable<ExerciseType>(context) {
override val tableName: String
get() = "exerciseType"
override fun formatHeader(): String =
"'ID', 'Name', 'Is Using User Weight'"
override fun decodeEntry(entry: String): ExerciseType {
val (id, name, isUsingUserWeight) = entry.split(
", ",
ignoreCase = false,
limit = 3
)
return ExerciseType(
id.toInt(),
name,
isUsingUserWeight.toBoolean()
)
}
override fun formatEntry(entry: ExerciseType): String {
return entry.let { "${it.id}, ${it.name}, ${it.isUsingUserWeight}" }
}
}

View File

@ -1,45 +0,0 @@
package org.british_information_technologies.gym_log_book.table
import android.content.Context
import org.british_information_technologies.gym_library.data_type.NumbersExerciseItem
import org.british_information_technologies.gym_log_book.lib.table.CSVTable
import java.time.LocalDate
class NumbersExerciseTable(
private val context: Context,
override val tableName: String
) : CSVTable<NumbersExerciseItem>(context) {
private var currentID = 0
override fun formatHeader(): String {
return "Date, Exercise, Set number, Weight, Reps"
}
override fun decodeEntry(entry: String): NumbersExerciseItem {
val (
date,
exercise,
set,
weight,
reps
) = entry.split(
", ",
ignoreCase = false,
limit = 5
)
return NumbersExerciseItem(
exercise = exercise,
setNumber = set.toInt(),
date = LocalDate.parse(date),
reps = reps.toInt(),
weight = weight.toDouble(),
id = currentID++
)
}
override fun formatEntry(entry: NumbersExerciseItem): String {
return "${entry.date}, ${entry.exercise}, ${entry.setNumber}, ${entry.weight}, ${entry.reps}"
}
}

View File

@ -1,38 +0,0 @@
package org.british_information_technologies.gym_log_book.table
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
class TableProvider {
@Provides
@Singleton
fun provideExerciseTypeTable(
@ApplicationContext context: Context,
): ExerciseTypeTable {
return ExerciseTypeTable(context)
}
@Provides
@Singleton
fun provideExerciseTable(
@ApplicationContext context: Context,
): ExerciseTable {
return ExerciseTable(context)
}
@Provides
@Singleton
fun provideWeightTable(
@ApplicationContext context: Context,
): WeightTable {
return WeightTable(context)
}
}

View File

@ -1,29 +0,0 @@
package org.british_information_technologies.gym_log_book.table
import android.content.Context
import io.github.michael_bailey.android_chat_kit.extension.any.log
import org.british_information_technologies.gym_library.data_type.WeightItem
import org.british_information_technologies.gym_log_book.lib.table.CSVTable
import java.time.LocalDate
class WeightTable(val context: Context) : CSVTable<WeightItem>(context) {
override val tableName: String
get() = "Weight"
override fun formatHeader(): String {
log("called format header")
return "'ID', 'Date', 'Weight'"
}
override fun decodeEntry(entry: String): WeightItem = entry.let {
log("decoding: $it ")
val (id, date, Weight) = it.split(", ", ignoreCase = false, limit = 3)
WeightItem(id.toInt(), LocalDate.parse(date), Weight.toDouble())
}
override fun formatEntry(entry: WeightItem): String {
log("Formatting entry")
return "${entry.id}, ${entry.date}, ${entry.weight}"
}
}