From f9304a52e2a9664d24907e6782a4b1f37b5b280e Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 12 Oct 2019 01:28:12 -0400 Subject: [PATCH] Working on saving the api information to the database and then saving the token. After that work on fetching the songs from icarus and viewing them. --- app/src/main/cpp/APIRepository.cpp | 95 +++++++++++++++++++ app/src/main/cpp/APIRepository.h | 30 ++++++ app/src/main/cpp/CMakeLists.txt | 3 + app/src/main/cpp/Demo.cpp | 23 +++++ app/src/main/cpp/UserRepository.h | 1 - app/src/main/cpp/model/APIInfo.h | 20 ++++ .../example/mear/activities/LoginActivity.kt | 62 +++++++----- .../java/com/example/mear/models/APIInfo.kt | 4 + .../mear/repositories/APIRepository.kt | 35 +++++++ 9 files changed, 248 insertions(+), 25 deletions(-) create mode 100644 app/src/main/cpp/APIRepository.cpp create mode 100644 app/src/main/cpp/APIRepository.h create mode 100644 app/src/main/cpp/model/APIInfo.h create mode 100644 app/src/main/java/com/example/mear/models/APIInfo.kt create mode 100644 app/src/main/java/com/example/mear/repositories/APIRepository.kt diff --git a/app/src/main/cpp/APIRepository.cpp b/app/src/main/cpp/APIRepository.cpp new file mode 100644 index 0000000..44d76e1 --- /dev/null +++ b/app/src/main/cpp/APIRepository.cpp @@ -0,0 +1,95 @@ +// +// Created by brahmix on 10/12/19. +// + +#include "APIRepository.h" + +#include + +namespace repository { namespace local { + APIRepository::APIRepository() + { + m_tableName = apiInfoTable(); + } + + + model::APIInfo APIRepository::retrieveAPIInfo(const std::string& path) + { + model::APIInfo apiInfo; + try { + const auto dbPath = pathOfDatabase(path); + SQLite::Database db(dbPath, SQLite::OPEN_READONLY); + + std::string queryString("SELECT * FROM "); + queryString.append(m_tableName); + queryString.append(" LIMIT 1"); + + SQLite::Statement query(db, queryString); + + auto result = query.executeStep(); + apiInfo.uri = query.getColumn(1).getString(); + apiInfo.endpoint = query.getColumn(2).getString(); + apiInfo.version = query.getColumn(3).getInt(); + + return apiInfo; + } catch (std::exception ex) { + auto msg = ex.what(); + } + + return apiInfo; + } + + + void APIRepository::createAPiInfoTable(const std::string& path) + { + try { + const auto dbPath = pathOfDatabase(path); + SQLite::Database db(dbPath, SQLite::OPEN_READWRITE); + + std::string queryString("CREATE TABLE "); + queryString.append(m_tableName); + queryString.append(" (Id INTEGER PRIMARY KEY, Uri TEXT, Endpoint TEXT, Version INT"); + db.exec(queryString); + } catch (std::exception& ex) { + auto msg = ex.what(); + } + } + + void APIRepository::deleteAPIInfo(const model::APIInfo& apiInfo, const std::string& path) + { + try { + const auto dbPath = pathOfDatabase(path); + SQLite::Database db(dbPath, SQLite::OPEN_READWRITE); + + std::string queryString("DELETE FROM "); + queryString.append(m_tableName); + + db.exec(queryString); + } catch (std::exception& ex) { + auto msg = ex.what(); + } + } + + void APIRepository::saveAPIInfo(const model::APIInfo& apiInfo, const std::string& path) + { + try { + const auto dbPath = pathOfDatabase(path); + SQLite::Database db(dbPath, SQLite::OPEN_READWRITE); + std::string queryString("INSERT INTO "); + queryString.append(m_tableName); + queryString.append(" (Uri, Endpoint, Version) VALUES (?, ?, ?)"); + + SQLite::Statement query(db, queryString); + query.bind(1, apiInfo.uri); + query.bind(2, apiInfo.endpoint); + query.bind(3, apiInfo.version); + + query.exec(); + } catch (std::exception& ex) { + auto msg = ex.what(); + } + } + + + std::string APIRepository::apiInfoTable() noexcept { return "APIInfo"; } +}} diff --git a/app/src/main/cpp/APIRepository.h b/app/src/main/cpp/APIRepository.h new file mode 100644 index 0000000..4699e2e --- /dev/null +++ b/app/src/main/cpp/APIRepository.h @@ -0,0 +1,30 @@ +// +// Created by brahmix on 10/12/19. +// + +#ifndef MEAR_APIREPOSITORY_H +#define MEAR_APIREPOSITORY_H + +#include + +#include "BaseRepository.h" +#include "model/APIInfo.h" + +namespace repository { namespace local { + class APIRepository: public BaseRepository { + public: + APIRepository(); + + model::APIInfo retrieveAPIInfo(const std::string&); + + void createAPiInfoTable(const std::string&); + void deleteAPIInfo(const model::APIInfo&, const std::string&); + void saveAPIInfo(const model::APIInfo&, const std::string&); + private: + //std::string apiInfoTable(); + std::string apiInfoTable() noexcept; + }; +}} + + +#endif //MEAR_APIREPOSITORY_H diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 6e92002..acfa83c 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -std=c++17 -DNDEBUG") set (SOURCES + APIRepository.cpp BaseRepository.cpp Demo.cpp SongRepository.cpp @@ -28,7 +29,9 @@ set (SOURCES ) set (HEADERS + APIRepository.h BaseRepository.h + model/APIInfo.h model/Song.h model/Token.h model/User.h diff --git a/app/src/main/cpp/Demo.cpp b/app/src/main/cpp/Demo.cpp index d5ada1f..3016778 100644 --- a/app/src/main/cpp/Demo.cpp +++ b/app/src/main/cpp/Demo.cpp @@ -23,9 +23,11 @@ #include #include +#include "model/APIInfo.h" #include "model/Song.h" #include "model/Token.h" #include "model/User.h" +#include "APIRepository.h" #include "SongRepository.h" #include "Tok.h" #include "UserRepository.h" @@ -44,6 +46,7 @@ model::User retrieveCredentials(const std::string&); std::string fetchToken(const std::string&, const std::string&, const std::string&); bool doesDatabaseExist(const std::string&); +bool apiInformationExist(const std::string&); bool userCredentialExist(const std::string&); void saveCredentials(const model::User&, const std::string&); @@ -150,6 +153,13 @@ bool doesDatabaseExist(const std::string& dataPath) return result; } +bool apiInformationExist(const std::string& dataPath) +{ + repository::local::APIRepository apiRepo; + + return apiRepo.isTableEmpty(dataPath); +} + bool userCredentialExist(const std::string& dataPath) { repository::local::UserRepository userRepo; @@ -381,6 +391,19 @@ Java_com_example_mear_activities_LoginActivity_logUser( } +extern "C" +JNIEXPORT jboolean +JNICALL +Java_com_example_mear_repositories_APIRepository_isAPIInfoTableEmpty( + JNIEnv *env, + jobject thisObj, + jstring pathStr + ) { + const std::string datapath = env->GetStringUTFChars(pathStr, nullptr); + + return apiInformationExist(datapath); +} + extern "C" JNIEXPORT jboolean JNICALL diff --git a/app/src/main/cpp/UserRepository.h b/app/src/main/cpp/UserRepository.h index df33540..666ed16 100644 --- a/app/src/main/cpp/UserRepository.h +++ b/app/src/main/cpp/UserRepository.h @@ -19,7 +19,6 @@ namespace repository { namespace local { model::User retrieveUserCredentials(const std::string&); - bool isEmpty(const std::string&); bool doesUserTableExist(const std::string&); void createUserTable(const std::string&); diff --git a/app/src/main/cpp/model/APIInfo.h b/app/src/main/cpp/model/APIInfo.h new file mode 100644 index 0000000..5790156 --- /dev/null +++ b/app/src/main/cpp/model/APIInfo.h @@ -0,0 +1,20 @@ +// +// Created by brahmix on 10/12/19. +// + +#ifndef MEAR_APIINFO_H +#define MEAR_APIINFO_H + +#include + +namespace model { + class APIInfo + { + public: + std::string uri; + std::string endpoint; + int version; + }; +} + +#endif //MEAR_APIINFO_H diff --git a/app/src/main/java/com/example/mear/activities/LoginActivity.kt b/app/src/main/java/com/example/mear/activities/LoginActivity.kt index 5a69509..d5f5faa 100644 --- a/app/src/main/java/com/example/mear/activities/LoginActivity.kt +++ b/app/src/main/java/com/example/mear/activities/LoginActivity.kt @@ -5,17 +5,17 @@ import android.os.Bundle import android.os.Environment import android.support.design.widget.Snackbar import com.example.mear.R +import com.example.mear.models.* import kotlinx.android.synthetic.main.activity_login.* import kotlinx.android.synthetic.main.content_login.* import org.jetbrains.anko.toast -import com.example.mear.models.Song -import com.example.mear.models.Token -import com.example.mear.models.Track -import com.example.mear.models.User -import com.example.mear.repositories.TrackRepository -import com.example.mear.repositories.UserRepository +import com.example.mear.repositories.* +import mear.com.example.mear.repositories.APIRepository + +//import com.example.mear.repositories.TrackRepository +//import com.example.mear.repositories.UserRepository class LoginActivity : BaseServiceActivity() { @@ -24,19 +24,7 @@ class LoginActivity : BaseServiceActivity() { setContentView(R.layout.activity_login) setSupportActionBar(toolbar) - val pa = appDirectory() - val usrRepo = UserRepository() - if (usrRepo.databaseExist(pa) && !usrRepo.isTableEmpty(pa)) { - val usr = usrRepo.retrieveCredentials(pa) - username.setText(usr.username) - password.setText(usr.password) - } - - doBindService() - - demoStream.setOnClickListener { - toast("vacant").show() - } + loadElements() login.setOnClickListener { loginButton() @@ -48,6 +36,7 @@ class LoginActivity : BaseServiceActivity() { } } + private fun loginButton() { if (!validFields()) { toast("Fields are invalid").show() @@ -55,22 +44,27 @@ class LoginActivity : BaseServiceActivity() { } val saveCred = saveUserCred.isChecked - var apiUriStr = apiUri.text.toString() + var apiInfo = APIInfo(apiUri.text.toString(), 1) + //var apiUriStr = apiUri.text.toString() val usr = User(username.text.toString(), password.text.toString()) - if (apiUriStr.isEmpty()) { - apiUriStr = "" + if (apiInfo.uri.isEmpty()) { + apiInfo.uri = "" } val usrRepo = UserRepository() val trackRepo = TrackRepository() - val myToken = usrRepo.fetchToken(usr, apiUriStr) + val myToken = usrRepo.fetchToken(usr, apiInfo.uri) try { val pa = appDirectory() val so = Song(5) - val song = trackRepo.fetchSong(myToken, so, apiUriStr) + val song = trackRepo.fetchSong(myToken, so, apiInfo.uri) if (saveCred && usrRepo.isTableEmpty(pa)) { + val api = APIRepository() + if (api.isTableEmpty(pa)) { + api.SaveRecord(apiInfo, pa) + } usrRepo.saveCredentials(usr, pa) } //startActivity(Intent(this, IcarusSongActivity::class.java)) @@ -80,6 +74,26 @@ class LoginActivity : BaseServiceActivity() { } } + private fun loadElements() { + val pa = appDirectory() + val usrRepo = UserRepository() + val apiRepo = APIRepository() + if (!usrRepo.databaseExist(pa)) { + return + } + if (!usrRepo.isTableEmpty(pa)) { + val usr = usrRepo.retrieveCredentials(pa) + username.setText(usr.username) + password.setText(usr.password) + } + if (!apiRepo.isTableEmpty(pa)) { + val api = apiRepo.retrieveRecord(pa) + val s: String = "${api.uri}" + "${api.version} ${api.endpoint}" + apiUri.setText(s) + } + + doBindService() + } private fun appDirectory(): String { return Environment.getDataDirectory().toString() + "/data/" + diff --git a/app/src/main/java/com/example/mear/models/APIInfo.kt b/app/src/main/java/com/example/mear/models/APIInfo.kt new file mode 100644 index 0000000..77101f2 --- /dev/null +++ b/app/src/main/java/com/example/mear/models/APIInfo.kt @@ -0,0 +1,4 @@ +package com.example.mear.models + +class APIInfo(var uri: String = "", var version: Int = 1, + var endpoint: String = "") diff --git a/app/src/main/java/com/example/mear/repositories/APIRepository.kt b/app/src/main/java/com/example/mear/repositories/APIRepository.kt new file mode 100644 index 0000000..05f0f4a --- /dev/null +++ b/app/src/main/java/com/example/mear/repositories/APIRepository.kt @@ -0,0 +1,35 @@ +package mear.com.example.mear.repositories + +import com.example.mear.models.APIInfo +import com.example.mear.repositories.BaseRepository + +class APIRepository: BaseRepository() { + + companion object { + init { + System.loadLibrary("native-lib") + } + } + + + private external fun retrieveAPIInfoRecord(path: String): APIInfo + + private external fun isAPIInfoTableEmpty(path: String): Boolean + + private external fun saveAPIInfoRecord(api: APIInfo, path: String) + + + fun retrieveRecord(path: String): APIInfo { + return retrieveAPIInfoRecord(path) + } + + + fun isTableEmpty(path: String): Boolean { + return isAPIInfoTableEmpty(path) + } + + + fun SaveRecord(api: APIInfo, path: String) { + return saveAPIInfoRecord(api, path) + } +} \ No newline at end of file