-
-
Save AfzalivE/03f1034c42a39f1563f60be25d112bdb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DatabaseProvider(private val app: InkNotesApplication) : IDatabaseProvider { | |
private lateinit var _database: NotesDatabase | |
override val database: NotesDatabase | |
get() = _database | |
internal fun initRoomDatabase(passphrase: String): NotesDatabase { | |
val passphraseBytes = SQLiteDatabase.getBytes(passphrase.toCharArray()) | |
val sqlCipherFactory = SupportFactory(passphraseBytes) | |
_database = Room.databaseBuilder( | |
app, | |
NotesDatabase::class.java, | |
DATABASE_FILE_NAME | |
) | |
.fallbackToDestructiveMigrationFrom(1) | |
.openHelperFactory(sqlCipherFactory) | |
.build() | |
return database | |
} | |
// called from MainActivity#onStop() | |
internal fun closeDatabase() { | |
if (this::_database.isInitialized) { | |
_database.close() | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Composable | |
fun HomeScreen( | |
notesViewModel: NotesViewModel, | |
onNoteClicked: (Long) -> Unit | |
) { | |
val notes by notesViewModel.notesFlow.collectAsState(initial = listOf()) | |
HomeScreenContent(notes, onNoteClicked) // Contains a LazyColumn that just shows notes | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SuppressLint("UnusedCrossfadeTargetStateParameter") | |
@OptIn(ExperimentalAnimatedInsets::class) | |
@Composable | |
private fun AppContent() { | |
val authVm: AuthViewModel = viewModel() | |
val navController = rememberNavController() | |
var savedNavState by remember { mutableStateOf<Bundle?>(null) } | |
val authenticated by authVm.authState.observeAsState(AuthState.Empty) | |
Timber.d("Authenticated: $authenticated") | |
if (authenticated !is AuthState.Authenticated) { | |
// Allow restoring navigation state after authentication | |
savedNavState = navController.saveState() | |
} | |
Crossfade(targetState = authenticated) { | |
when { | |
it == AuthState.Unauthenticated -> PasswordScreen(authVm) | |
it == AuthState.Authenticated -> NavScreens( | |
navController, | |
savedState = savedNavState | |
) | |
} | |
} | |
} | |
@Composable | |
fun NavScreens(navController: NavHostController, savedState: Bundle?) { | |
val syncVm: SyncViewModel = viewModel() | |
if (savedState != null) { | |
navController.restoreState(savedState) | |
} | |
NavHost( | |
navController = navController, | |
startDestination = Screen.Home.route | |
) { | |
composable(Screen.Home.route) { | |
HomeScreen( | |
notesViewModel = viewModel(), | |
onNoteClicked = { noteId -> | |
navController.navigate(Screen.EditNote.route(noteId)) | |
}, | |
) | |
// ... Other route composables here | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotesViewModel( | |
notesRepository: NotesRepository = Injector.notesRepository | |
) : ViewModel() { | |
val notesFlow = notesRepository.getNotes() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoomNotesRepository(private val databaseProvider: DatabaseProvider) : NotesRepository { | |
override fun getNotes(): Flow<List<Note>> { | |
return noteDb.noteDao().getAll() | |
} | |
override fun getNote(noteId: Long): Flow<Note?> { | |
return noteDb.noteDao().getById(noteId) | |
} | |
override suspend fun addNote(note: Note) { | |
noteDb.noteDao().insert(note) | |
} | |
private val noteDb: NotesDatabase | |
get() = databaseProvider.database | |
} | |
interface NotesRepository { | |
fun getNotes(): Flow<List<Note>> | |
fun getNote(noteId: Long): Flow<Note?> | |
suspend fun addNote(note: Note) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment