-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from itszechs/multi-account-support
Multiple account support
- Loading branch information
Showing
61 changed files
with
2,404 additions
and
627 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/src/main/java/zechs/drive/stream/data/local/AccountsDao.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package zechs.drive.stream.data.local | ||
|
||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
import kotlinx.coroutines.flow.Flow | ||
import zechs.drive.stream.data.model.Account | ||
import zechs.drive.stream.data.model.AccountWithClient | ||
import zechs.drive.stream.data.model.Client | ||
|
||
@Dao | ||
interface AccountsDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun addClient(client: Client) | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun addAccount(account: Account) | ||
|
||
@Transaction | ||
@Query("DELETE FROM clients WHERE id = :clientId") | ||
suspend fun deleteClient(clientId: String) | ||
|
||
@Query("SELECT * FROM clients") | ||
fun getClients(): Flow<List<Client>> | ||
|
||
@Transaction | ||
@Query( | ||
"SELECT accounts.*, clients.secret AS clientSecret, clients.redirectUri " + | ||
"FROM accounts JOIN clients ON accounts.clientId = clients.id" | ||
) | ||
fun getAccounts(): Flow<List<AccountWithClient>> | ||
|
||
@Transaction | ||
@Query( | ||
"SELECT accounts.*, clients.secret AS clientSecret, clients.redirectUri " + | ||
"FROM accounts JOIN clients ON accounts.clientId = clients.id " + | ||
"WHERE accounts.name = :accountName" | ||
) | ||
suspend fun getAccount(accountName: String): AccountWithClient? | ||
|
||
@Query("UPDATE accounts SET name = :newName WHERE name = :oldName") | ||
suspend fun updateAccountName(oldName: String, newName: String) | ||
|
||
@Query("DELETE FROM accounts WHERE name = :accountName") | ||
suspend fun deleteAccount(accountName: String) | ||
|
||
@Transaction | ||
@Query("UPDATE clients SET secret = :secret, redirectUri = :redirectUri WHERE id = :clientId") | ||
suspend fun updateClient(clientId: String, secret: String, redirectUri: String) | ||
|
||
@Query("UPDATE accounts SET accessToken = :newToken WHERE name = :accountName") | ||
suspend fun updateAccessToken(accountName: String, newToken: String) | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
app/src/main/java/zechs/drive/stream/data/local/AccountsDatabase.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package zechs.drive.stream.data.local | ||
|
||
import androidx.room.Database | ||
import androidx.room.RoomDatabase | ||
import zechs.drive.stream.data.model.Account | ||
import zechs.drive.stream.data.model.Client | ||
|
||
|
||
@Database( | ||
entities = [Account::class, Client::class], | ||
version = 1, | ||
exportSchema = false | ||
) | ||
abstract class AccountsDatabase : RoomDatabase() { | ||
|
||
abstract fun getAccountsDao(): AccountsDao | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
app/src/main/java/zechs/drive/stream/data/model/AccountWithClient.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package zechs.drive.stream.data.model | ||
|
||
import androidx.annotation.Keep | ||
import androidx.room.Entity | ||
import androidx.room.ForeignKey | ||
import androidx.room.Ignore | ||
import androidx.room.Index | ||
import androidx.room.PrimaryKey | ||
import com.google.gson.Gson | ||
import com.google.gson.reflect.TypeToken | ||
|
||
@Entity(tableName = "clients") | ||
data class Client( | ||
@PrimaryKey val id: String, | ||
val secret: String, | ||
val redirectUri: String | ||
) { | ||
fun isEmpty() = id.isEmpty() || secret.isEmpty() || redirectUri.isEmpty() | ||
} | ||
|
||
@Entity( | ||
tableName = "accounts", | ||
foreignKeys = [ForeignKey( | ||
entity = Client::class, | ||
parentColumns = ["id"], | ||
childColumns = ["clientId"], | ||
onDelete = ForeignKey.CASCADE | ||
)], | ||
indices = [Index("clientId")] | ||
) | ||
data class Account( | ||
@PrimaryKey val name: String, | ||
val refreshToken: String, | ||
val accessToken: String, | ||
val clientId: String | ||
) | ||
|
||
@Keep | ||
data class AccountWithClient( | ||
val name: String, | ||
val clientId: String, | ||
val clientSecret: String, | ||
val redirectUri: String, | ||
val refreshToken: String, | ||
val accessToken: String | ||
) { | ||
|
||
@Ignore | ||
var isDefault: Boolean = false | ||
|
||
fun getDriveClient() = DriveClient( | ||
clientId = clientId, | ||
clientSecret = clientSecret, | ||
redirectUri = redirectUri, | ||
scopes = listOf("https://www.googleapis.com/auth/drive") | ||
) | ||
|
||
fun getAccessTokenResponse(): TokenResponse { | ||
val type = object : TypeToken<TokenResponse>() {}.type | ||
return Gson().fromJson(accessToken, type) | ||
} | ||
|
||
} |
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
8 changes: 8 additions & 0 deletions
8
app/src/main/java/zechs/drive/stream/data/model/TokenRequestBody.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package zechs.drive.stream.data.model | ||
|
||
import com.google.errorprone.annotations.Keep | ||
|
||
@Keep | ||
data class TokenRequestBody( | ||
val token: String | ||
) |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/zechs/drive/stream/data/remote/RevokeTokenApi.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package zechs.drive.stream.data.remote | ||
|
||
import retrofit2.Response | ||
import retrofit2.http.Body | ||
import retrofit2.http.POST | ||
import zechs.drive.stream.data.model.TokenRequestBody | ||
|
||
interface RevokeTokenApi { | ||
|
||
@POST("/revoke") | ||
suspend fun revokeToken( | ||
@Body body: TokenRequestBody | ||
): Response<Unit> | ||
|
||
} |
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
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
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
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
39 changes: 39 additions & 0 deletions
39
app/src/main/java/zechs/drive/stream/ui/add_account/DialogAddAccount.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package zechs.drive.stream.ui.add_account | ||
|
||
import android.app.Dialog | ||
import android.content.Context | ||
import android.os.Bundle | ||
import android.view.Window | ||
import android.widget.Toast | ||
import com.google.android.material.button.MaterialButton | ||
import com.google.android.material.textfield.TextInputLayout | ||
import zechs.drive.stream.R | ||
|
||
class DialogAddAccount( | ||
context: Context, | ||
val onNextClickListener: (String) -> Unit | ||
) : Dialog(context, R.style.ThemeOverlay_Fade_MaterialAlertDialog) { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
requestWindowFeature(Window.FEATURE_NO_TITLE) | ||
setContentView(R.layout.dialog_new_account) | ||
|
||
val etNickname = findViewById<TextInputLayout>(R.id.tf_nickname).editText!! | ||
val nextButton = findViewById<MaterialButton>(R.id.btn_next) | ||
|
||
nextButton.setOnClickListener { | ||
if (etNickname.text.toString().isEmpty()) { | ||
Toast.makeText( | ||
context, | ||
context.getString(R.string.please_enter_a_nickname), | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} else { | ||
onNextClickListener.invoke(etNickname.text.toString()) | ||
dismiss() | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.