Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial drafts support #844

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions app/src/main/java/com/jerboa/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ import com.jerboa.db.AppDB
import com.jerboa.db.AppSettingsRepository
import com.jerboa.db.AppSettingsViewModel
import com.jerboa.db.AppSettingsViewModelFactory
import com.jerboa.db.DraftsRepository
import com.jerboa.db.DraftsViewModel
import com.jerboa.db.DraftsViewModelFactory
import com.jerboa.ui.components.comment.edit.CommentEditActivity
import com.jerboa.ui.components.comment.edit.CommentEditViewModel
import com.jerboa.ui.components.comment.reply.CommentReplyActivity
Expand Down Expand Up @@ -81,6 +84,7 @@ class JerboaApplication : Application() {
private val database by lazy { AppDB.getDatabase(this) }
val accountRepository by lazy { AccountRepository(database.accountDao()) }
val appSettingsRepository by lazy { AppSettingsRepository(database.appSettingsDao()) }
val draftsRepository by lazy { DraftsRepository(database.draftsDao()) }
}

class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -108,6 +112,9 @@ class MainActivity : ComponentActivity() {
private val appSettingsViewModel: AppSettingsViewModel by viewModels {
AppSettingsViewModelFactory((application as JerboaApplication).appSettingsRepository)
}
private val draftsViewModel: DraftsViewModel by viewModels {
DraftsViewModelFactory((application as JerboaApplication).draftsRepository)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -521,6 +528,7 @@ class MainActivity : ComponentActivity() {
postViewModel = postViewModel,
accountViewModel = accountViewModel,
personProfileViewModel = personProfileViewModel,
draftsViewModel = draftsViewModel,
navController = navController,
siteViewModel = siteViewModel,
isModerator = isModerator,
Expand Down Expand Up @@ -551,6 +559,7 @@ class MainActivity : ComponentActivity() {
navController = navController,
personProfileViewModel = personProfileViewModel,
postViewModel = postViewModel,
draftsViewModel = draftsViewModel,
)
}
composable(
Expand Down
70 changes: 68 additions & 2 deletions app/src/main/java/com/jerboa/db/AppDB.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ data class AppSettings(
val secureWindow: Boolean,
)

@Entity
data class Draft(
@PrimaryKey(autoGenerate = true) val id: Int,
val message: String,
)

@Dao
interface AccountDao {
@Query("SELECT * FROM account")
Expand Down Expand Up @@ -159,6 +165,21 @@ interface AppSettingsDao {
suspend fun updatePostViewMode(postViewMode: Int)
}

@Dao
interface DraftsDao {
@Query("SELECT * FROM Draft")
fun getAll(): LiveData<List<Draft>>

@Query("SELECT * FROM Draft")
fun getAllSync(): List<Draft>

@Insert
fun insert(draft: Draft)

@Delete
fun delete(draft: Draft)
}

// Declares the DAO as a private property in the constructor. Pass in the DAO
// instead of the whole database, because you only need access to the DAO
class AccountRepository(private val accountDao: AccountDao) {
Expand Down Expand Up @@ -248,6 +269,16 @@ class AppSettingsRepository(
}
}

class DraftsRepository(private val draftsDao: DraftsDao) {
val allDrafts = draftsDao.getAll()

fun getAllSync(): List<Draft> = draftsDao.getAllSync()

fun delete(draft: Draft) = draftsDao.delete(draft)

fun insert(draft: Draft) = draftsDao.insert(draft)
}

val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(
Expand Down Expand Up @@ -436,14 +467,29 @@ val MIGRATION_15_16 = object : Migration(15, 16) {
}
}

val MIGRATION_16_17 = object : Migration(16, 17) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL(UPDATE_APP_CHANGELOG_UNVIEWED)
database.execSQL(
"""
CREATE TABLE IF NOT EXISTS Draft(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
message TEXT NOT NULL
)
"""
)
}
}

@Database(
version = 16,
entities = [Account::class, AppSettings::class],
version = 17,
entities = [Account::class, AppSettings::class, Draft::class],
exportSchema = true,
)
abstract class AppDB : RoomDatabase() {
abstract fun accountDao(): AccountDao
abstract fun appSettingsDao(): AppSettingsDao
abstract fun draftsDao(): DraftsDao

companion object {
@Volatile
Expand Down Expand Up @@ -477,6 +523,7 @@ abstract class AppDB : RoomDatabase() {
MIGRATION_13_14,
MIGRATION_14_15,
MIGRATION_15_16,
MIGRATION_16_17,
)
// Necessary because it can't insert data on creation
.addCallback(object : Callback() {
Expand Down Expand Up @@ -567,3 +614,22 @@ class AppSettingsViewModelFactory(private val repository: AppSettingsRepository)
throw IllegalArgumentException("Unknown ViewModel class")
}
}

class DraftsViewModel(private val repository: DraftsRepository): ViewModel() {
val drafts = repository.allDrafts

fun delete(draft: Draft) = viewModelScope.launch { repository.delete(draft) }

fun insert(message: String) = viewModelScope.launch { repository.insert(Draft(0, message)) }
}

class DraftsViewModelFactory(private val repository: DraftsRepository) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(DraftsViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return DraftsViewModel(repository) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.jerboa.R
import com.jerboa.db.Account
import com.jerboa.db.DraftsViewModel
import com.jerboa.ui.components.common.MarkdownTextField

@OptIn(ExperimentalMaterial3Api::class)
Expand Down Expand Up @@ -72,6 +73,7 @@ fun CommentEdit(
onContentChange: (TextFieldValue) -> Unit,
account: Account?,
padding: PaddingValues,
draftsViewModel: DraftsViewModel,
) {
val scrollState = rememberScrollState()

Expand All @@ -87,6 +89,7 @@ fun CommentEdit(
account = account,
modifier = Modifier.fillMaxWidth(),
placeholder = stringResource(R.string.comment_edit_type_your_comment),
draftsViewModel = draftsViewModel,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.navigation.NavController
import com.jerboa.api.ApiState
import com.jerboa.db.AccountViewModel
import com.jerboa.db.DraftsViewModel
import com.jerboa.ui.components.common.getCurrentAccount
import com.jerboa.ui.components.person.PersonProfileViewModel
import com.jerboa.ui.components.post.PostViewModel
Expand All @@ -24,6 +25,7 @@ fun CommentEditActivity(
commentEditViewModel: CommentEditViewModel,
personProfileViewModel: PersonProfileViewModel,
postViewModel: PostViewModel,
draftsViewModel: DraftsViewModel,
) {
Log.d("jerboa", "got to comment edit activity")

Expand Down Expand Up @@ -64,6 +66,7 @@ fun CommentEditActivity(
account = account,
onContentChange = { content = it },
padding = padding,
draftsViewModel = draftsViewModel,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.jerboa.datatypes.types.CommentView
import com.jerboa.datatypes.types.PersonMentionView
import com.jerboa.datatypes.types.PostView
import com.jerboa.db.Account
import com.jerboa.db.DraftsViewModel
import com.jerboa.ui.components.comment.CommentNodeHeader
import com.jerboa.ui.components.comment.mentionnode.CommentMentionNodeHeader
import com.jerboa.ui.components.comment.replynode.CommentReplyNodeHeader
Expand Down Expand Up @@ -279,6 +280,7 @@ fun PostReply(
onPersonClick: (personId: Int) -> Unit,
isModerator: Boolean,
account: Account?,
draftsViewModel: DraftsViewModel,
modifier: Modifier = Modifier,
) {
val scrollState = rememberScrollState()
Expand All @@ -298,6 +300,7 @@ fun PostReply(
account = account,
modifier = Modifier.fillMaxWidth(),
placeholder = stringResource(R.string.comment_reply_type_your_comment),
draftsViewModel = draftsViewModel,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.ui.text.input.TextFieldValue
import androidx.navigation.NavController
import com.jerboa.api.ApiState
import com.jerboa.db.AccountViewModel
import com.jerboa.db.DraftsViewModel
import com.jerboa.isModerator
import com.jerboa.ui.components.common.LoadingBar
import com.jerboa.ui.components.common.getCurrentAccount
Expand All @@ -29,6 +30,7 @@ fun CommentReplyActivity(
personProfileViewModel: PersonProfileViewModel,
postViewModel: PostViewModel,
siteViewModel: SiteViewModel,
draftsViewModel: DraftsViewModel,
navController: NavController,
isModerator: Boolean,
) {
Expand Down Expand Up @@ -98,6 +100,7 @@ fun CommentReplyActivity(
navController.navigate(route = "profile/$personId")
},
isModerator = isModerator,
draftsViewModel = draftsViewModel,
modifier = Modifier
.padding(padding)
.imePadding(),
Expand Down
Loading